Android Studio到3.0 - 无法合并dex [DexArchiveMergerException]

时间:2017-12-09 08:49:46

标签: android android-gradle

我有项目,我添加了另一个项目作为模块,我对此错误消息有很大的疑问:

  

错误:任务执行失败   ':应用程序:transformDexArchiveWithExternalLibsDexMergerForDebug'

     
    

java.lang.RuntimeException:com.android.builder.dexing.DexArchiveMergerException:无法合并     DEX

  

我在stackoverflow和google中尝试了很多解决方案,但我没有找到答案。

例如:

  1. 我尝试添加到defaultConfig multiDexEnabled true。
  2. 我尝试清理并重建。
  3. 我尝试Invalidate catch / Restart然后重建。
  4. 我的项目gradle:

     // Top-level build file where you can add configuration options common to all sub-projects/modules.
    
        buildscript {
    
            repositories {
                google()
                jcenter()
            }
            dependencies {
                classpath 'com.android.tools.build:gradle:3.0.1'
    
    
                // NOTE: Do not place your application dependencies here; they belong
                // in the individual module build.gradle files
            }
        }
    
        allprojects {
            repositories {
                google()
                jcenter()
            }
        }
    
        task clean(type: Delete) {
            delete rootProject.buildDir
        }
    

    我的应用程序gradle:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "adms.foxitpdf"
            minSdkVersion 21
            targetSdkVersion 26
            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'
            }
        }
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'com.android.support:appcompat-v7:26.1.0'
        implementation 'com.android.support.constraint:constraint-layout:1.0.2'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.1'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
        implementation files('libs/FoxitRDK.jar')
        compile project(path: ':uiextensions_src')
    }
    

    我的模块gradle:

    buildscript {
        repositories {
            mavenCentral()
            jcenter()
        }
        dependencies {
             classpath 'com.android.tools.build:gradle:2.2.3'
        }
    }
    
    apply plugin: 'com.android.library'
    
    android {
        compileSdkVersion 26
        buildToolsVersion '26.0.2'
    
        defaultConfig {
            minSdkVersion 21
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
        lintOptions{
            abortOnError false
        }
    }
    
    dependencies {
        compile 'com.android.support:appcompat-v7:26.1.0'
        compile 'com.android.support:design:26.1.0'
        implementation files('src/libs/FoxitRDK.jar')
    }
    
    task clearAar(type: Delete){
        delete '../FoxitRDKUIExtensions.aar'
    }
    
    task makeAar(type: Exec){
        dependsOn(clearAar,build)
        def fromFile = 'build/outputs/aar/'
        def intoFile = '../'
        if(org.gradle.internal.os.OperatingSystem.current().windows) {
            commandLine "cmd",
                    '-/C', 'copy', file("$fromFile/uiextensions_src-release.aar").absolutePath, file("$intoFile/FoxitRDKUIExtensions.aar")
        }else {
            commandLine "cp",
                    file("$fromFile/uiextensions_src-release.aar").absolutePath, file("$intoFile/FoxitRDKUIExtensions.aar")
        }
    }
    makeAar {}.dependsOn(clearAar,build)
    
    task makeJavaDoc(type: Javadoc) {
        List<String> mSourcePath = new ArrayList<String>();
        mSourcePath.add("../../fsdk/app/src/main/java/com/foxit/sdk/");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/UIExtensionsManager.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/Module.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/pdfreader/impl/PDFReader.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/menu/IMenuView.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/menu/MoreMenuConfig.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/menu/MoreMenuModule.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/toolbar/IBaseItem.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/toolbar/IBarsHandler.java");
        mSourcePath.add("src/main/java/com/foxit/uiextensions/controls/propertybar/IMultiLineBar.java");
    
        source = mSourcePath;
        classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
        Properties properties = new Properties()
        InputStream inputStream = project.rootProject.file('local.properties').newDataInputStream() ;
        properties.load( inputStream )
        //读取文件
        def sdkDir = properties.getProperty('sdk.dir')
        ext.androidJar = sdkDir + "/extras/android/support/annotations/android-support-annotations.jar"
        classpath += project.files(ext.androidJar)
    
        destinationDir = file("../../docs/api_reference_android")
        failOnError false
    }
    
    makeJavaDoc {
        options {
            encoding "UTF-8"
            charSet 'UTF-8'
            author true
            version true
            memberLevel = org.gradle.external.javadoc.JavadocMemberLevel.PUBLIC
            locale = "en"
            title = "Document of RDK for Android"
    
            // fsdk overview
            Map<String,List<String>> map = new HashMap<>();
            List<String> fsdkOverViews= new ArrayList<>();
            fsdkOverViews.add("com.foxit.sdk");
            fsdkOverViews.add("com.foxit.sdk.*");
            map.put("Fsdk Packages",fsdkOverViews);
    
            // UIExtensions overview
            List<String> uiExtendOverViews = new ArrayList<>();
            uiExtendOverViews.add("com.foxit.uiextensions");
            uiExtendOverViews.add("com.foxit.uiextensions.*");
            map.put("UIExtenstions Packages",uiExtendOverViews);
    
            groups = map;
        }
    }
    

    我使用 Android Studio 3.0

    有人知道为什么?

1 个答案:

答案 0 :(得分:0)

我猜您需要将其中一个包含在您的gradle中,并添加依赖项并包含在清单中。如果您仅在 gradle 中启用且未包含在清单和依赖项中,请尝试一下。

multiDexEnabled true

添加到您的依赖项

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

gradle文件

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "adms.foxitpdf"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 9
        versionName "1.0"
        multiDexEnabled true //Add this
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

根据您覆盖的方式将应用程序添加到清单中。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

或者我们可以这样做并再次包含在带有名称标签的清单中。

public class MyApplication extends SomeOtherApplication {
  @Override
  protected void attachBaseContext(Context base) {
     super.attachBaseContext(base);
     MultiDex.install(this);
  }
}

阅读全文here