MultiDex support in Android application error

2019-04-21 16:55发布

问题:

I want to use Android L compat libs. after adding the relevant code to gradle, I get the error:

  Error Code:
2
  Output:
objc[36290]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536
  at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:501)
  at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:276)
  at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:490)
  at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:167)
  at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
  at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
  at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
  at com.android.dx.command.dexer.Main.run(Main.java:230)
  at com.android.dx.command.dexer.Main.main(Main.java:199)
  at com.android.dx.command.Main.main(Main.java:103)

I saw questions about it this here and here, and tried out the solution from this blog post, and I still get an error, where in the case of the blog post, I get:

  Error Code:
2  Output:
objc[36323]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.7.0_67.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Library dex files are not supported in multi-dex mode
    at com.android.dx.command.dexer.Main.runMultiDex(Main.java:322)
    at com.android.dx.command.dexer.Main.run(Main.java:228)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

These are my android gradle settings:

android {
compileSdkVersion 21
buildToolsVersion "20.0.0"

defaultConfig {
    applicationId "com.my.package"
    minSdkVersion 9
    targetSdkVersion 21
}

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
}

}

These are my dependencies:

dependencies {
compile project(':libraries:ecoGallery')
compile project(':libraries:facebookSDK')
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.google.android.gms:play-services:6.1.71'
compile 'com.j256.ormlite:ormlite-android:4.48'
compile 'com.j256.ormlite:ormlite-core:4.48'
compile 'com.mixpanel.android:mixpanel-android:4.3.1@aar'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
compile 'com.nineoldandroids:library:2.4.0'
compile 'oauth.signpost:signpost-commonshttp4:1.2.1.2'
compile 'oauth.signpost:signpost-core:1.2.1.2'
compile 'com.uservoice:uservoice-android-sdk:+@aar'
compile 'com.newrelic.agent.android:android-agent:4.87.0'
compile 'com.google.guava:guava:18.0'
compile files('libs/android-support-multidex.jar')

}

Does anyone have any ideas for what I might be doing wrong?

回答1:

Gradle plugin v0.14.0 for Android adds full multidex support.
Remove all the build.gradle changes you made (for multidex), and simply add the following:

android {
   defaultConfig {
      ...
      multiDexEnabled = true
   }
}


回答2:

Trying adding the following code to your build.gradle, worked for me.

android{

...
dexOptions {
        preDexLibraries = false
    }

afterEvaluate {
        tasks.matching {
            it.name.startsWith('dex')
        }.each { dx ->
            if (dx.additionalParameters == null) {
                dx.additionalParameters = ['--multi-dex']
            } else {
                dx.additionalParameters += '--multi-dex'
            }
        }
    }
...
}


回答3:

instead of including the entire google library, use only the ones you require.

for ex. use:

    compile 'com.google.android.gms:play-services-maps:7.8.0'
    compile 'com.google.android.gms:play-services-location:7.8.0'

instead of

compile 'com.google.android.gms:play-services:7.8.0'


回答4:

Try to disabled "Instant run":

In android studio: Menu File -> Settings

In Build, Execution, Deployment -> Instant run

UNCHECK Enabled Instant Run to hot swap code/resource changes on deploy (default enabled)



回答5:

None of the answers they gave you were exhaustive. The problem lies in the Multidex. You must add the library in the app gradle :

implementation 'com.android.support:multidex:1.0.3'

After, add in the defaultConfig of the app gradle :

multiDexEnabled true

Your Application must be of the Multidex type.. You must write it in the manifest :

android:name=".MyApplication"

MyApplication must be either the Multidex class, or it must extend it.