那些年,你遇见的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即可,如下:

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


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,类似如下错误

> <span class="hljs-constant" style="padding: 0px;">Duplicate</span> files copied <span class="hljs-keyword" style="color: rgb(0, 0, 136); padding: 0px;">in</span> <span class="hljs-constant" style="padding: 0px;">APK</span> <span class="hljs-constant" style="padding: 0px;">META</span>-<span class="hljs-constant" style="padding: 0px;">INF</span>/<span class="hljs-constant" style="padding: 0px;">NOTICE</span>.txt
    <span class="hljs-constant" style="padding: 0px;">File</span> <span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">1</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); padding: 0px;">:</span> <span class="hljs-constant" style="padding: 0px;">F</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); padding: 0px;">:</span>\android_studio_project\nizaoma\app\libs\httpmime-<span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">4.2</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">4</span>.jar
    <span class="hljs-constant" style="padding: 0px;">File</span> <span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">2</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); padding: 0px;">:</span> <span class="hljs-constant" style="padding: 0px;">F</span><span class="hljs-symbol" style="color: rgb(0, 102, 102); padding: 0px;">:</span>\android_studio_project\nizaoma\app\libs\httpmime-<span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">4.2</span>.<span class="hljs-number" style="color: rgb(0, 102, 102); padding: 0px;">4</span>.jar
<code style="font-family: 'Source Code Pro', Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; color: inherit; padding: 0px; white-space: inherit; background-image: none; background-position: initial initial; background-repeat: initial initial;">build.gradle android{}里添加:</code><pre style="white-space: pre-wrap; word-wrap: break-word; background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: 宋体; font-size: 12pt;">packagingOptions {
    exclude <span style="color: rgb(106, 135, 89);">'META-INF/DEPENDENCIES.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/LICENSE.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/NOTICE.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/NOTICE'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/LICENSE'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/DEPENDENCIES'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/notice.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/license.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/dependencies.txt'
</span><span style="color: rgb(106, 135, 89);">    </span>exclude <span style="color: rgb(106, 135, 89);">'META-INF/LGPL2.1'
</span>}
 


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了一下项目,既遭提示:

<code style="font-family: 'Source Code Pro', Consolas, Menlo, Monaco, 'Courier New', monospace; font-size: 1em; color: inherit; padding: 0px; white-space: initial; word-break: break-word; background-image: none; background-position: initial initial; background-repeat: initial initial;">Instant Run <span class="hljs-keyword" style="color: rgb(133, 153, 0);">is</span> <span class="hljs-attribute" style="color: rgb(181, 137, 0);">disabled</span>: Instant Run does <span class="hljs-keyword" style="color: rgb(133, 153, 0);">not</span> support deploying build variants <span class="hljs-reserved">with</span> multidex enabled, to a target<span class="hljs-reserved">with</span> API level <span class="hljs-number" style="color: rgb(42, 161, 152);">20</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">or</span> below. To use Instant Run <span class="hljs-reserved">with</span> a multidex enabled build variant, deploy to a target <span class="hljs-reserved">with</span> API level <span class="hljs-number" style="color: rgb(42, 161, 152);">21</span> <span class="hljs-keyword" style="color: rgb(133, 153, 0);">or</span> higher.</code>
经查,当项目 开启了 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里的包名也算,修改包名即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值