那些年,你遇见的Android-Studio遇到的各种坑!!!!

本文转自:http://blog.csdn.net/u012874222/article/details/50616698


开此文章,主要是为了记录在使用虐我千百遍,我还待他如初恋的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即可,如下:

xmlns:android="http://schemas.android.com/apk/res/android"
          package="***">


4,昨天创建了一个libModule,为了匹配拷贝过来的java文件的包,删除了他的原始的包,自己新建了一个包,并且把mainfest里面的包名也对应进行了更换,然后看了下build.gradle,发现也没什么要改的,原以为就可以正常调试了,结果确是死活都找不到R包,网上也未找到对应解决方法,最终重建了一个module,不删除原始的包,才得以正常执行。虽然不知道最终问题在哪,哪里还需要改。总之,结论就是新建的项目莫随意改变他的包名。。


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
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或者删除该文件夹



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功能可以大大提高编译速度,RunDebug的时候,只有在第一次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

解决方法:

.gradlegradle.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里的包名也算,修改包名即可

你可能感兴趣的:(Android,Studio)