多个Dex文件定义了Landroid / support / v4

时间:2015-09-16 05:52:39

标签: java android android-studio gradle android-gradle

我几个小时以来一直在抨击我。我尝试将2bard的AndroidPianoView导入为库项目(内部为PianoView和PianoViewExample),并且他们的support-v4 jar目录中都有libs

我的项目在Android Studio中。

从其中一个或两个目录中删除jar对我没有好处,我尝试在this popular question中使用该解决方案但没有成功。

这是运行gradle -q:app:dependencies的依赖关系树:

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.1
|    \--- com.android.support:support-v4:22.2.1
|         \--- com.android.support:support-annotations:22.2.1
\--- project :PianoViewExample
     \--- project :PianoView

以下是build.gradle文件:

我的项目

buildscript {
    repositories {
        mavenCentral() // or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.ajbowler.jamory"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile project(':PianoViewExample')
}

PianoView

buildscript {
    repositories {
        mavenCentral() // or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
       }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

PianoViewExample

buildscript {
    repositories {
        mavenCentral() // or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.library'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':PianoView')
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

控制台输出

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v4/app/ActivityCompatHoneycomb;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)
Error:Execution failed for task ':app:dexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_25\bin\java.exe'' finished with non-zero exit value 2

我在主项目中exclude module: 'support-v4'会发生什么

buildscript {
    repositories {
        mavenCentral() // or jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.1'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.ajbowler.jamory"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile project(':PianoViewExample')
    compile('com.android.support:appcompat-v7:23.0.1') {
        configurations {
            all*.exclude group: 'com.android.support', module: 'support-v4'
            all*.exclude group: 'com.google.android.gms', module: 'play-services'
        }
    }
}

输出

Error:(8, 8) error: cannot access TaskStackBuilder
class file for android.support.v4.app.TaskStackBuilder not found
Error:(10, 5) error: method does not override or implement a method  from a supertype
Error:(12, 9) error: cannot find symbol variable super
Error:(13, 9) error: cannot find symbol method setContentView(int)
Error:(16, 5) error: method does not override or implement a method from a supertype
Error:(19, 9) error: cannot find symbol method getMenuInflater()
Error:(23, 5) error: method does not override or implement a method from a supertype
Error:(35, 16) error: cannot find symbol variable super
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

我真的很感激这方面的一些帮助。我一直无法弄清楚问题来自过去的exclude - 来自其中一个PianoView模块的支持jar,它还没有工作。

1 个答案:

答案 0 :(得分:0)

是的,今天就修好了。在我的情况下,支持v4和maven中的支持v7之间存在冲突。我所做的就是在主项目的support v7标签中添加支持v4的exclude标志。
我不得不从v7中排除v4因为我已经在使用它了(v4的导入位于我的pom文件中的5行以上)。
例如:
http://www.thedevline.com/2014/06/unexpected-top-level-exception.html?m=1
编辑
类似帖子:
Multiple dex files define Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat
希望能帮助到你。
Multiple dex files define Landroid/support/v4/ in android studio