在Android Studio 2.1.1中构建APK文件时出错

问题描述 投票:7回答:5

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

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

错误

  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:进程'命令'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
android apk build.gradle
5个回答
29
投票

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的实现,所以编译器很困惑可以选择。所以它显示错误重复条目。这将由packaging options解决,这将允许您使用重复文件。


1
投票

在构建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'
    }

1
投票

我也得到了同样的错误。添加编译'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中使用调试模式。

我认为它会对你有所帮助。


0
投票

在更新firebase任何谷歌播放服务然后尝试更新所有库。这对我有用。希望它在某些情况下有效。


-1
投票

回答迟到但我面临同样的问题。

我能够使用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 {  
    ...  
    ...  
}  

希望能帮助到你。

© www.soinside.com 2019 - 2024. All rights reserved.