在build.gradle中编译具有useLibrary'org.apache.http.legacy'的项目时出现'app:transformClassesWithDexForDebug'错误

时间:2017-08-21 17:39:13

标签: android android-studio compiler-errors multidex processexception

我正在开发一个我正在使用此library的应用。

在编译项目时,会弹出包含 getProductList = function(){ let productArray =[]; loadFBData = function(){ firebase.initializeApp(config); return new Promise(resolve => { firebase.database().ref().once('value') .then(function(snapshot){ return resolve(function(snapshot){ productArray = Object.keys(snapshot).map((key, prod) => prod); }); }); }); } loadFBData(); } Departments.php?action=907fgash6f8906a6asf6g...的错误:

TransformException

这是RuntimeException

Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: Unable to pre-dex 'C:\Users\hp\.gradle\caches\modules-2\files-2.1\org.brunocvcunha.instagram4j\instagram4j\1.4\e86d0ec82fb3eb4225b983e6dc14db048005bc9b\instagram4j-1.4.jar' to 'C:\Users\hp\Downloads\Hamy\app\build\intermediates\transforms\dex\debug\folders\1000\10\instagram4j-1.4_ea292e8a2c317dbd8ab0226ff9507ee97ac0794b'

更新

不改变代码中的任何内容,我现在收到此错误:

build.gradle (Module: app)

当我尝试删除文件时,他们会在运行应用程序时再次创建它们。

Gradle控制台输出:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.xxx.abc"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'LICENSE.txt'
        exclude '**/log4j.properties'
//        exclude 'log4j.properties'
        exclude 'org/bytedeco/javacpp/macosx-x86_64/libusb-1.0.dylib'
        exclude 'META-INF/DEPENDENCIES'
    }
}

repositories {
    mavenCentral()
}

android {
    useLibrary 'org.apache.http.legacy'
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:preference-v7:25.3.1'
    compile 'ai.api:sdk:2.0.7@aar'
    compile 'ai.api:libai:1.4.8'
    compile 'com.google.code.gson:gson:2.7'
    compile 'commons-io:commons-io:2.4'
    compile 'com.twitter.sdk.android:twitter:3.1.1'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'
    compile 'com.android.support:multidex:1.0.1'
    compile 'org.brunocvcunha.instagram4j:instagram4j:1.4'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.5'
    compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
    testCompile 'junit:junit:4.12'
}

configurations {
    all*.exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}

更新时间:31-08-2017

在尝试了以下答案中推荐的所有内容之后,我仍然会收到此错误:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK log4j.properties
    File1: C:\Users\user\.gradle\caches\modules-2\files-2.1\org.brunocvcunha.inutils4j\inutils4j\0.4\566b82b1e70d7ebb8d1d3bb513d4bbf7913b118b\inutils4j-0.4.jar
    File2: C:\Users\user\.gradle\caches\modules-2\files-2.1\org.brunocvcunha.ghostme4j\ghostme4j\0.2\d44c3633f23975c355c89192d6e8daa9ce549b2d\ghostme4j-0.2.jar

我无法弄清楚为什么我会在这里收到此错误。请帮帮我。

5 个答案:

答案 0 :(得分:2)

将此代码放入您的第一个活动(应用的启动活动):

public class YouApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}

答案 1 :(得分:2)

你要

  

DuplicateFileException:在APK中复制的重复文件

确保调用相同的依赖项。

答案 2 :(得分:2)

将“排除”拉出“依赖项”部分并输入配置。 e.g。

dependencies {
    ...
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.5'
    ...
}

configurations {
    all*.exclude group: 'org.apache.httpcomponents',module: 'httpclient'
}

答案 3 :(得分:2)

来自日志:

Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK log4j.properties
    File1: C:\Users\user\.gradle\caches\modules-2\files-2.1\org.brunocvcunha.inutils4j\inutils4j\0.4\566b82b1e70d7ebb8d1d3bb513d4bbf7913b118b\inutils4j-0.4.jar
    File2: C:\Users\user\.gradle\caches\modules-2\files-2.1\org.brunocvcunha.ghostme4j\ghostme4j\0.2\d44c3633f23975c355c89192d6e8daa9ce549b2d\ghostme4j-0.2.jar

您似乎需要从项目中排除 log4j.properties ,因为它位于两个库(inutils4j和ghostme4j)中。您可以尝试使用:

android {
...
    packagingOptions {
         exclude '**/log4j.properties'
    }
...
}

详细了解PackagingOptions

答案 4 :(得分:1)

我认为您错过了在MultiDexApplication中添加manifest

    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>

如果你错过了。添加它并清理并重建项目。

如果您使用Application Class,您的应用程序类应该扩展MultiDexApplication而不是Application。

class MyApplication extends MultiDexApplication

参考:https://developer.android.com/studio/build/multidex.html

希望有所帮助:)