Android Studio:Multiple dex files define Landroid/support/annotation/AnimRes

最近使用androidstudio 老是出问题,都有点返回继续用eclipse 的冲动了,问题是 :当我用真机5.1.1手机上运行时 没事,在4.4.4手机上却无法编译成功,出现以下问题


Information:Gradle tasks [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
:app:clean
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72310Library
:app:prepareComAndroidSupportSupportV42310Library
:app:prepareYunDuoVRModelCommonUnspecifiedLibrary
:app:prepareYunDuoVRModelCommonwidgetUnspecifiedLibrary
:app:prepareYunDuoVRModelPanowidgetUnspecifiedLibrary
:app:prepareDebugDependencies
:app:compileDebugAidl
:app:compileDebugRenderscript
:app:generateDebugBuildConfig
:app:mergeDebugShaders
:app:compileDebugShaders
:app:generateDebugAssets
:app:mergeDebugAssets
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources
:app:mergeDebugResources
:app:processDebugManifest
E:\YunDuoVR\app\src\main\AndroidManifest.xml:16:5-81 Warning:
Element uses-permission#android.permission.WRITE_EXTERNAL_STORAGE at AndroidManifest.xml:16:5-81 duplicated with element declared at AndroidManifest.xml:7:5-81
E:\YunDuoVR\app\src\main\AndroidManifest.xml:17:5-80 Warning:
Element uses-permission#android.permission.READ_EXTERNAL_STORAGE at AndroidManifest.xml:17:5-80 duplicated with element declared at AndroidManifest.xml:9:5-80
E:\YunDuoVR\app\src\main\AndroidManifest.xml:30:5-67 Warning:
Element uses-permission#android.permission.INTERNET at AndroidManifest.xml:30:5-67 duplicated with element declared at AndroidManifest.xml:18:5-67
E:\YunDuoVR\app\src\main\AndroidManifest.xml:40:5-84 Warning:
Element uses-permission#android.permission.MOUNT_UNMOUNT_FILESYSTEMS at AndroidManifest.xml:40:5-84 duplicated with element declared at AndroidManifest.xml:8:5-84
E:\YunDuoVR\app\src\main\AndroidManifest.xml:43:5-81 Warning:
Element uses-permission#android.permission.WRITE_EXTERNAL_STORAGE at AndroidManifest.xml:43:5-81 duplicated with element declared at AndroidManifest.xml:16:5-81
:app:processDebugResources
:app:generateDebugSources
:app:preDebugAndroidTestBuild UP-TO-DATE
:app:prepareDebugAndroidTestDependencies
:app:compileDebugAndroidTestAidl
:app:processDebugAndroidTestManifest
:app:compileDebugAndroidTestRenderscript
:app:generateDebugAndroidTestBuildConfig
:app:mergeDebugAndroidTestShaders
:app:compileDebugAndroidTestShaders
:app:generateDebugAndroidTestAssets
:app:mergeDebugAndroidTestAssets
:app:generateDebugAndroidTestResValues UP-TO-DATE
:app:generateDebugAndroidTestResources
:app:mergeDebugAndroidTestResources
:app:processDebugAndroidTestResources
:app:generateDebugAndroidTestSources
:app:mockableAndroidJar UP-TO-DATE
:app:preDebugUnitTestBuild UP-TO-DATE
:app:prepareDebugUnitTestDependencies
:app:compileDebugJavaWithJavac
注: 某些输入文件使用或覆盖了已过时的 API。
注: 有关详细信息, 请使用 -Xlint:deprecation 重新编译。
注: 某些输入文件使用了未经检查或不安全的操作。
注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:buildInfoDebugLoader
:app:transformClassesWithExtractJarsForDebug
:app:transformClassesWithInstantRunVerifierForDebug
:app:transformClassesWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:mergeDebugJniLibFolders
:app:transformNative_libsWithMergeJniLibsForDebug
:app:processDebugJavaRes UP-TO-DATE
:app:transformResourcesWithMergeJavaResForDebug
:app:transformResourcesAndNative_libsWithJavaResourcesVerifierForDebug UP-TO-DATE
:app:transformClassesWithInstantRunForDebug
:app:transformClasses_enhancedWithInstant+reloadDexForDebug UP-TO-DATE
:app:incrementalDebugTasks
:app:prePackageMarkerForDebug
:app:fastDeployDebugExtractor
:app:generateDebugInstantRunAppInfo
:app:transformClassesWithDexForDebug
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(com.tencent.mm.sdk.b.b) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(org.apache.commons.httpclient.HttpMethodBase$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:warning: Ignoring InnerClasses attribute for an anonymous inner class
Error:(org.apache.commons.httpclient.protocol.ControllerThreadSocketFactory$1) that doesn't come with an
Error:associated EnclosingMethod attribute. This class was probably produced by a
Error:compiler that did not target the modern .class file format. The recommended
Error:solution is to recompile the class from source, using an up-to-date compiler
Error:and without specifying any "-target" type options. The consequence of ignoring
Error:this warning is that reflective operations on this class will incorrectly
Error:indicate that it is *not* an inner class.
Error:Error converting bytecode to dex:
Cause: com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException:
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_11\bin\java.exe'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 55.02 secs
Information:26 errors
Information:0 warnings
Information:See complete output in console


后来查阅了很多资料,可能是随着项目不断的壮大,导入的jar越来越多,可能是jar包冲突,后来我看了一篇文章,

http://blog.csdn.net/yanzi1225627/article/details/47361375

出现这个问题是因为最新的v4包(compile ‘com.Android.support:support-v4:22.2.1’)已经包含了annotation.jar这个包,但其他jar包里也包含这个包造成的。而非同时包含了v4/V7造成的,于是在v7包里进行如下设置:

<code class="hljs cs has-numbering"><span style="font-size:18px;">compile (<span class="hljs-string">'com.android.support:appcompat-v7:22.2.1'</span>){
        exclude <span class="hljs-keyword">group</span>: <span class="hljs-string">"com.android.support"</span>, module: <span class="hljs-string">"support-v4"</span>
    }</span></code>

即设置v7包不包含v4,这个设了也是没用的。只要确保用v4和v7 的地方版本一致即可。正确的解决方法有如下几种:
1,找到项目其他的依赖包,一定在libs里的某个jar包里,隐藏着annotation.jar包,将其删掉就ok了,此为最正统的解决方法;
2,降级v4包,因为高版本的v4才包含annotation.jar,可以在最外面的build.grable里强制设置:

<code class="hljs matlab has-numbering"><span style="font-size:18px;">allprojects <span class="hljs-cell">{
    repositories {
        jcenter()
    }</span>
    <span class="hljs-transposed_variable">configurations.</span>all <span class="hljs-cell">{
        resolutionStrategy.force <span class="hljs-string">'com.android.support:support-annotations:22.1.0'</span>
    }</span>
}</span></code>

3,在需要v4包的地方exclude掉annotation包,注意compile要多加个括号:

<span style="font-size:18px;"><code class="hljs ruby has-numbering"> compile (<span class="hljs-string">'com.android.support:support-v4:22.2.1'</span>){
        exclude <span class="hljs-class"><span class="hljs-keyword">module</span>: '<span class="hljs-title">support</span>-<span class="hljs-title">annotations</span>'</span>
    }</code><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">用v4包的地方都要这么设置下。 </span></span>

4,在application的build.gradle里的dexOptions设置里增加一句:preDexLibraries = false就ok了:
这里写图片描述


我进行了一下设置就ok了。如下图





PS:以上四种方法都可以解决问题,推荐正统的方法1和偷懒的方法4。



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值