在32位系统下,往Android Studio中导入一个已有的项目后,如果运行时报错如下:
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Invalid maximum heap size: -Xmx4g
The specified size exceeds the maximum representable size.
Error:Execution failed for task ':MyProject:dexDebug'.
原因可能是该项目中有JVM的定义超出了32位系统所能支持的最大内存,在项目的各级build.gradle中查找javaMaxHeapSize关键字,你将会看到:
dexOptions {
javaMaxHeapSize "4g"
}
只要将4g改为1g:
dexOptions {
javaMaxHeapSize "1g"
}
或者直接删除此3行。
保存后,重新运行即可。
往Android Studio中导入一个已有的项目后,如果同步时报错如下:
Error:Configuration with name 'default' not found.
可能原因是gradle没有找到相应的模块,请查看settings.gradle文件中的配置是否正确,以及目录是否存在。
如果出现如下错误:
Error:Unable to load class 'org.codehaus.groovy.runtime.StringGroovyMethods'.
Possible causes for this unexpected error include:- You are using JDK version 'java version "1.7.0_71"'. Some versions of JDK 1.7 (e.g. 1.7.0_10) may cause class loading errors in Gradle.
Please update to a newer version (e.g. 1.7.0_67).
Open JDK Settings
- Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
- The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
- Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
原因可能是项目根目录的gradle/wrapper/gradle-wrapper.properties文件中的distributionUrl定义与jdk版本不符。
比如原先定义如下:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.12-all.zip
修改为:
distributionUrl=http\://services.gradle.org/distributions/gradle-2.4-all.zip
运行时出现如下错误:
Error:Execution failed for task ':UplusTV:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'D:\Program Files\Java\jdk1.7.0_71\bin\java.exe'' finished with non-zero exit value 2
可能原因是项目引入了重复的jar包,或者因为引入的jar包中包含相同的文件。解决方法是删除冗余的jar包或者删除jar包中冗余的文件。
运行时出现如下错误:
Error:Execution failed for task ':Uplus:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files (x86)\Java\jdk1.7.0\bin\java.exe'' finished with non-zero exit value 1
可能原因是在64位系统上使用了32位的jdk,换用64位的jdk可能会解决此问题。
编译时,如果出现如下错误:
java.lang.OutOfMemoryError: GC overhead limit exceeded
原因可能是内存不够,可以在build.gradle中的android{}添加如下内容就可以顺利编译了
dexOptions {
javaMaxHeapSize "4g"
}
运行时出现如下错误弹窗:
ADB not responding. If you'd like to retry, then please manually kill "adb.exe" and click 'Restart'
可能原因是系统中的其他程序占用了5037端口,在cmd中,运行:netstat -ano | findstr "5037",就可以看到类似如下的信息:
TCP 127.0.0.1:5037 0.0.0.0:0 LISTENING 5252
这说明PID为5252的程序占用了5037端口,所以在cmd中,运行:tasklist,即可看到类似如下的信息:
映像名称 PID 会话名 会话# 内存使用
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 24 K
System 4 Services 0 4,572 K
smss.exe 280 Services 0 900 K
csrss.exe 412 Services 0 4,344 K
adb.exe 5252 Console 1 5,988 K
我们看到此处PID为5252的进程是一个adb.exe,这说明另外一个adb实例占用了5037端口,所以运行:taskkill /im adb.exe /f,会看到类似如下的信息:
成功: 已终止进程 "adb.exe",其 PID 为 5252。
至此,已经将PID为5252的进程关闭,此时可以点击'Restart'按钮重新启动Android Studio中的adb了。
当执行:Build->Generate Signed APK...,进行签名打包时报错:
Error:org.gradle.api.internal.changedetection.state.FileCollectionSnapshotImpl cannot be cast to org.gradle.api.internal.changedetection.state.OutputFilesCollectionSnapshotter$OutputFilesSnapshot
Possible causes for this unexpected error include:- Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
- The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
- Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
可能原因是gradle的问题。解决方法是清空项目根目录下.gradle目录下的所有文件和目录,然后重新编译、打包即可。
当执行:Build->Generate Signed APK...,进行签名打包时报错:
Error:Execution failed for task ':UplusTV:lintVitalRelease'.
> Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
...
Error:(1) Error: In Gradle projects, always use http://schemas.android.com/apk/res-auto for custom attributes [ResAuto]
可能原因是代码不规范,但是如果修改代码的话,可能需要修改很多地方,费时费力。比较简单的解决方法是在build.gradle文件中添加:
lintOptions {
checkReleaseBuilds false
abortOnError false
}
编译运行时出现如下错误:
Error:FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':MyModule:transformClassesAndResourcesWithPrepareIntermediateJarsForDebug'.
> java.io.IOException: Failed to delete E:\workspace\Pro-test\MyModule\build\intermediates\intermediate-jars\debug\classes.jar
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 3s
原因不明,解决方法可以试试:
依次选择菜单:Build->Rebuild Project,然后重新编译运行。
如果上述操作不起作用,可以重启Android Studio。
编译运行时出现如下错误:
Error:No such property: GradleVersion for class: JetGradlePlugin
可能原因是Android Studio版本(GradlePlugin)和Gradle版本不匹配。
解决方法有2种:
1.编辑项目中的文件:gradle/wrapper/gradle-wrapper.properties
修改这一行:
distributionUrl=https\://services.gradle.org/distributions/gradle-5.5.1-all.zip
改成当前支持的版本(具体版本根据Android Studio的版本而定):
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
2.如果上述方法不行,就要考虑更换Android Studio版本了。