【问题标题】:Error during Building an APK file in Android Studio 2.1.1在 Android Studio 2.1.1 中构建 APK 文件时出错
【发布时间】:2016-05-17 09:51:55
【问题描述】:

几天前,我升级了我的 Android Studio,现在我遇到了一个问题。

实际上,我正在尝试从我的项目构建一个 APK 文件以在真实设备上测试我的应用程序,当我单击 Build--> Build Apk 时,我在 Message Gradle 中收到了几个错误建造。我不知道为什么会出现这些错误,请详细说明原因。

错误

  1. 错误:将字节码转换为 dex 时出错:
    原因:com.android.dex.DexException: 多个dex文件定义Lcom/android/volley/VolleyError;

  2. 错误:任务 ':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_51\bin\java.exe'' 以非零退出值 2 结束

build.gradle 文件

 apply plugin: 'com.android.application'

android {
    signingConfigs {
    }

    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.dovezeal.gapp"
        minSdkVersion 19
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7

    }

}



dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    //compile 'com.android.support:appcompat-v7:23.3.0'

    compile 'com.android.support:support-v4:23.3.0'
    compile 'com.android.support:design:23.0.1'
    compile 'com.android.support:design:23.1.1'

    // Volley
    compile 'com.android.volley:volley:1.0.0'
    //compile 'com.mcxiaoke.volley:library:1.0.+'
    /* compile files('libs/com.mcxiaoke.volley library-1.0.0.jar')*/


    // RecyclerView
    compile 'com.android.support:recyclerview-v7:23.0.+'

    // A simple way to define and render UI specs on top of your Android UI.
    compile 'org.lucasr.dspec:dspec:0.1.1'

    compile files('libs/library-1.0.0.jar')

    // YouTube Player
    compile files('libs/YouTubeAndroidPlayerApi.jar')

    // GOSN
   /* compile files('libs/gson-2.2.3.jar')*/




}

编辑 - 1

正如 janki gadhiya 在下面的评论中所说,更改 minifyEnabled true 并尝试在 defaultConfig 下添加 multiDexEnabled true
通过这些更改,上面的两个错误都消失了,但现在出现了以下错误。

  1. 错误:任务执行失败:app:transformClassesWithJarMergingForDebug' com.android.build.api.transform.TransformException:java.util.zip.ZipException:重复条目:com/android/volley/Request$Priority.class

【问题讨论】:

  • 更改minifyEnabled true 并尝试在defaultConfig 下添加multiDexEnabled true
  • @jankigadhiya 感谢您的帮助!请同时检查我的问题中的 Edit-1 部分,并解释这些错误的实际原因

标签: android apk build.gradle


【解决方案1】:

build.gradle 文件

apply plugin: 'com.android.application'

android {
signingConfigs {
}

compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.dovezeal.gapp"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/notice.txt'
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7

}

}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
//compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:design:23.1.1'

// as you already compiled gradle for volley here

compile 'com.android.volley:volley:1.0.0'

// RecyclerView
compile 'com.android.support:recyclerview-v7:23.0.+'
compile 'org.lucasr.dspec:dspec:0.1.1'

// you don't need this so comment the below line.
//compile files('libs/library-1.0.0.jar')

// YouTube Player
compile files('libs/YouTubeAndroidPlayerApi.jar')

}

编辑:解释

您的错误 1 ​​- 2:表示您的项目中有超过 65,000 个方法,所以我告诉您设置 multiDexEnable true

您的错误 3 :意味着您有多个库具有类 Request$Priority.class 的实现,因此编译器很困惑该选择哪个。所以它显示 error Duplicate entry。这将由packaging options 解决,这将允许您使用重复文件。

【讨论】:

  • 我尝试了错误 3 的解决方案,但什么也没发生,同样的错误即将到来我做错了什么??
  • 你的libs文件夹里有凌空吗??
  • 是的,我的 libs 文件夹中有 volley,名为 library-1.0.0
  • 这就是您将其编译为 gradle 依赖项并添加到库中的问题,这将复制库和方法。只需为libs 删除library-1.0.0.jar,然后同步、清理和重建项目。看我的编辑..!! 在我的编辑中可以看到,从依赖项中注释或删除编译行 compile files('libs/library-1.0.0.jar')..!!
  • 感谢您的帮助,终于成功了,非常感谢您的帮助。请告诉我另外一件事是必须从libs 文件夹中删除jar 文件,仅评论该图书馆的包含声明还不够吗??
【解决方案2】:

在你的构建 gradle 中添加这个

    dexOptions {
            incremental true
            javaMaxHeapSize "4g"    
}



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'
    }

【讨论】:

    【解决方案3】:

    我也遇到了同样的错误。添加时 编译'com.google.firebase:firebase-ads:10.2.0' 但是当我执行以下操作时它会被删除:

    dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    
    compile 'com.google.firebase:firebase-ads:10.2.0'
    }
    
    apply plugin: 'com.google.gms.google-services'**
    

    并在 BuildVarient 中使用调试模式

    我想它会对你有所帮助。

    【讨论】:

      【解决方案4】:

      在更新 firebase 任何 google play 服务时,然后尝试更新所有库。这对我有用。希望它在某些情况下有效。

      【讨论】:

        【解决方案5】:

        回答有点晚,但我遇到了同样的问题。

        我能够使用multiDexEnabled 纠正它 -> true 并使用 build.gradle 中的打包选项,发布更改 .apk 安装成功。

        语法:

        defaultConfig {  
            ....  
            ....  
            multiDexEnabled true  
        }  
        
        packagingOptions {  
            exclude 'META-INF/DEPENDENCIES'  
            exclude 'META-INF/NOTICE'  
            exclude 'META-INF/LICENSE'  
            exclude 'META-INF/license.txt'  
            exclude 'META-INF/notice.txt'  
        }  
        buildTypes {  
            ...  
            ...  
        }  
        

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-10-25
          • 2017-09-27
          • 1970-01-01
          • 2020-04-24
          • 2016-12-08
          • 1970-01-01
          • 2018-09-28
          • 1970-01-01
          相关资源
          最近更新 更多