开此文章,主要是为了记录在使用虐我千百遍,我还待他如初恋的AS的过程中所遇到的各种坑,一来是希望做个记录,方便查找,防止再次踏入同样的坑;二来也希望能帮助到有遇到有跟我相同问题的人。
1,首先每次导入一个新的AS工程的时候,肯定会有各种gradle配置问题,只要把build.gradle改为与自己系统相匹配即可,如果报
Error:(1, 0) Plugin is too old, please update to a more recent version, or set ANDROID_DAILY_OVERRIDE environment variable to "f3cec24ce64772515843ff5f88a0005c31cdbe71"
把project的build.gradle里面的2.0.0-alpha3改为 1.5.0 即可
E:\android-studio-2.0-preview4\android-studio\gradle\m2repository\com\android\tools\build\gradle
这个目录下面有当前安装的版本,都可以试试
再把其他build.gradle里面的 compileSdkVersion 和 buildToolsVersion 和 依赖的support包版本改为自己常用的版本一般就可正常调试了。
2,如果在下载程序的时候,报了以下错误
Error:Could not create the Java Virtual Machine. Invalid maximum heap size:4g
这个错误我找了很久,最后还是在万能的stackoverflow里找到了答案,其实就是项目的build.gradle被配置了
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
把4g 改为1024m问题解决
3,快速将一个项目转换为lib项目:
① 修改build.gradle,把 applyplugin:'com.android.application'改为applyplugin:'com.android.library'
然后去掉 defaultConfig里面的applicationId
② 清空AndroidManifest里面的内容留下最外层manifest即可,如下:
4,AS项目的包名是在build.gradle文件里面的applicationId配置的,在mainfest配置将不生效。
5,eclipse项目转为android studio : 在eclipse中Export,导出gradle项目,选择需要导出的项目及libProject,随便找个正常的as工程的gradle文件夹,覆盖刚刚导出项目的gradle文件夹,然后根据 1 进行配置即可。(注:导出的项目会覆盖原工程使得此项目可以同时在eclipse和android studio中使用,as比ec检测严格,会检测到所有重复的资源,权限,假冒的.9,jar,以及中文乱码,根据对应提示进行删除)
6,大坑:
Warning:Ignoring InnerClasses attribute for an anonymous inner class
(com.tencent.mm.sdk.b.b) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
Warning:Ignoring InnerClasses attribute for an anonymous inner class
(com.tencent.mm.sdk.openapi.WXApiImplV10$ActivityLifecycleCb$2) that doesn't come with an
associated EnclosingMethod attribute. This class was probably produced by a
compiler that did not target the modern .class file format. The recommended
solution is to recompile the class from source, using an up-to-date compiler
and without specifying any "-target" type options. The consequence of ignoring
this warning is that reflective operations on this class will incorrectly
indicate that it is *not* an inner class.
UNEXPECTED TOP-LEVEL ERROR:
Error:Execution failed for task ':DLNA_DMC:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_73\bin\java.exe'' finished with non-zero exit value 3
出现类型警告和错误,那么jar重复,没有用到的jar,jar里面的包名与xxx的包名重复,或者是jar版本太老等,反正是jar里面的class文件转为dex的时候出错,解决办法是删除jar包,网上找对应的源码,或者用工具把jar转为源码形式来调用解决掉所有警告,然后加上multidex处理65536的问题,因为方法越界也会出现类似此错误,完了即可编译通过。。
7,类似如下错误
> Duplicate files copied in APK META-INF/NOTICE.txt
File 1: F:\android_studio_project\nizaoma\app\libs\httpmime-4.2.4.jar
File 2: F:\android_studio_project\nizaoma\app\libs\httpmime-4.2.4.jar
Duplicate files copied in APK META-INF/NOTICE.txt
File 1: F:\android_studio_project\nizaoma\app\libs\httpmime-4.2.4.jar
File 2: F:\android_studio_project\nizaoma\app\libs\httpmime-4.2.4.jar
build.gradle android{}里添加:
packagingOptions {
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/dependencies.txt'
exclude 'META-INF/LGPL2.1'
}
8,出现-Xmx128m -Xmx512m -Xmx1024m类似字样的错误,在.gradle的gradle.properties添加一项 org.gradle.jvmargs=-Xmx512m,若无此文件,则新建
9,as正确导入和删除module方式:
① 导入:File→New→import module 选中module所在目录,可改名确定即可,打开项目目录可以看到已经自动把module复制到目录中,所以无需手动复制module到项目目录,否则导入时会报已经存在一个同名的project的错误
② 删除:File→Project Structure 选中需要删除的module 点击“—”,确定回到项目目录找到要删除的module右键删除即可
10.报错 :/lib/arm64, /vendor/lib64, /system/lib64]]] couldn't find "libgotyeapi.so"
原因是在64位手机上会自动寻找jni里面arm64-v8a文件夹里面的so文件,如果找不到则报这个错误,如果把32位的so放进去则报位数不匹配的错误,解决方法是放入64位的so或者删除该文件夹,只留armeabi,然后在配置
如果在添加“abiFilter”之后Android Studio出现以下提示:
NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.
则在项目根目录的gradle.properties文件中添加:
android.useDeprecatedNdk=true
11. debug - unaligned .apk 无法删除,as每次编译的时候需要更新build文件,如果无法删除build里面的文件,那么编译将会报错,遇到这种情况,其实不管你是直接在文件夹,还是在as里面删除build,或者直接用第三方粉碎文件都是删除不了的,此时需重启,完成之后你会发现文件已经被删除,所以重启可解决此文件错乱的问题。若重启也删除不了那么把此项目复制,debug - unaligned .apk会提示无法复制,跳过,然后再打开即可 ,经查,这是金山毒霸搞得鬼,卸载可根除此问题
12.一个开发过程中比较少遇到的错误:java.lang.NoSuchFieldError: ***R$id***
造成此问题的原因在于库文件的xml与主文件的xml同名了,作修改即可
13.终于我也更新了AS2.1stable版本,据说其Instant Run功能可以大大提高编译速度,在Run
和Debug
的时候,只有在第一次build会花费大量的时间,之后再次Run或者Debug的时候会直接把变化的代码更新到手机,再上面生成一个新的APK直接运行,这样就能很快的显示出更改代码之后的变化,加快了开发效率和调试效率。当我怀着期待的心情更新了gradle为2.1,然后run了一下项目,既遭提示:
Instant Run is disabled: Instant Run does not support deploying build variants with multidex enabled, to a targetwith API level 20 or below. To use Instant Run with a multidex enabled build variant, deploy to a target with API level 21 or higher.
经查,当项目开启了multidex,
Instant Run需要在5.0手机(既是minSdkVersion是21以上)才能正常使用。
14.
出现如下错误:
解决方法:
dexOptions {
javaMaxHeapSize"5120"
preDexLibraries =false
}
15.出现如下错误:
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
>com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException:java.util.concurrent.ExecutionException:
java.lang.OutOfMemoryError: GC overhead limit exceeded
解决方法:
在.gradle的gradle.properties的 org.gradle.jvmargs=-Xmx512m调大点,我的调为4g即可,若无此文件,则新建
16.出现如下错误:
Error:trouble processing "java/lang/AutoCloseable.class":
Error:Ill-advised or mistaken usage of a core class (java.* or javax.*)
Error:when not building a core library.
Error:This is often due to inadvertently including a core library file
Error:in your application's project, when using an IDE (such as
Error:Eclipse). If you are sure you're not intentionally defining a
Error:core class, then this is the most likely explanation of what's
Error:going on.
Error:However, you might actually be trying to define a class in a core
Error:namespace, the source of which you may have taken, for example,
Error:from a non-Android virtual machine project. This will most
Error:assuredly not work. At a minimum, it jeopardizes the
Error:compatibility of your app with future versions of the platform.
Error:It is also often of questionable legality.
原因是项目中引用了以java开头包名的代码,jar里的包名也算,修改包名即可
17.有些项目还是用到eclipse的,每次用eclipse新开一个workspace的时候应该先调环境:
①,设置主题:
②,颜色主题:
3,设置代码提示:
④,设置字体大小
⑤,设置logcat字体颜色
18:zip file is empty:
一般你删除了jar包 撤回的时候会造成此问题,重新把有效的jar包覆盖进去即可解决此问题
19:文件编码问题,报如下错误:
Error:(23, 48) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗?
Error:(23, 49) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗?
Error:(23, 50) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗?
Error:(23, 51) 閿欒: 缂栫爜UTF-8鐨勪笉鍙槧灏勫瓧绗?
错误文件比较少的话可以手动复制内容过去即可解决问题,如果错误文件比较多需要批量转换请移至以下文章
http://blog.csdn.net/u012874222/article/details/77367464
20:INSTALL_FAILED_TEST_ONLY
升级到AS3.0后,每次调试生成debug包MainFest的Application标签自带testOnly=true属性,即使你没在MainFest里设置此项,导致将此APK复制出来安装会报此错误,解决方法就是通过Gradle里的build来生成,如下图
21:
Error:Could not create service of type FileCollectionSnapshotterRegistry using TaskExecutionServices.createFileCollectionSnapshotterRegistry().
> Could not create service of type CachingFileHasher using TaskExecutionServices.createFileSnapshotter().
解决方法:删除项目根目录下的.gradle文件夹即可
22:如何利用AS创建Java项目?
首先AS默认没有创建Java项目的选项,所以直接创建一个Android项目,然后把项目转为Java即可,那怎么转呢?经过以下两个步骤即可实现:
①Android项目的build.gradle(非根目录的build.gradle)改为如下:
apply plugin: 'java'
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
②删除一些文件和文件夹,然后创建一个Java文件即可运行调试,最终文件结构如下
23、AS打Jar包(在对应的build.gradle下添加如下代码):
①java工程:
task makeJar(type: Jar, dependsOn: ['build']) {
archiveName = 'mtapm-inject.jar'//生成的jar包文件名
from('build/classes/java/main')//需要打包的class文件
from(project.zipTree("libs/antlr-runtime-3.5.2.jar"))//需要合并的jar
from(project.zipTree("libs/guava-18.0.jar"))
from(project.zipTree("libs/support-annotations-25.3.1.jar"))
from(project.zipTree("libs/SignatureAPI.jar"))
destinationDir = file('build/libs')//jar文件夹生成路径
manifest {//配置MAIN-INF
attributes(
'Manifest-Version': '1.0',
'Main-Class': "mt.apm.inject.MtApmInjector")
}
}
②Android Module工程:
task makeDebugJar(type: Jar, dependsOn: ['bundleDebug']) {//若打包Release,则参数为bundleRelease
archiveName = 'mtapm_debug.jar'//生成的jar包文件名
from zipTree('build/intermediates/packaged-classes/debug/classes.jar')//源Jar
destinationDir = file('build/libs')//jar文件夹生成路径
}
如为混淆打包,需要对应的buildType开启 minifyEnabled true
24、containing both DEX and Java-bytecode content:引入的jar包里除了有class还有dex
治标:gradle.properties添加 android.enableD8=false
治本:重新打jar包