UNEXPECTED TOP-LEVEL EXCEPTION in IntelliJ with An

2019-08-09 09:06发布

问题:

Firtly, I am new with IntelliJ and Gradle.

I am migrating an Android Eclipse project to IntelliJ, but I have some problems.

I know in a common Eclipse project, libraries are imported manually and those dependencies must to be imported in the same way, but when are imported from gradle dependencies are imported automatically like maven do.

Currently, I have next configuration.

In my project:

Gradle config:

apply plugin: 'android'

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.google.android.gms:play-services:5.2.+')
    compile ('mobi.parchment:parchment:1.6.7@aar')
}

android {
    compileSdkVersion "Google Inc.:Google APIs:19"
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 13
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    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'
    }
}

In libs folder (old inheritance of Eclipse project dependencies):

-asmack-android-9-0.8.9.jar
-db4o-8.0.249.16098-all-java5.jar
-jackson-annotations-2.2.2.jar
-jackson-core-2.2.2.jar
-jackson-databind-2.2.2.jar
-prime-0.6.1.jar
-spring-android-auth-1.0.1.RELEASE.jar
-spring-android-core-1.0.1.RELEASE.jar
-spring-android-rest-template-1.0.1.RELEASE.jar

All those libraries are added with "compile" scope to the project from Module Dependencies Panel.

in my gradle, as you could see above.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile ('com.google.android.gms:play-services:5.2.+')
    compile ('mobi.parchment:parchment:1.6.7@aar')
}

I can see play-services and parchment libraries are added too from the Module Dependencies Panel too.

Then, I look for libraries incompatibilities using command "gradle -q dependencies" in the terminal.

+--- com.google.android.gms:play-services:5.2.+ -> 5.2.08
|    \--- com.android.support:support-v4:19.1.0
\--- mobi.parchment:parchment:1.6.7

As I previously removed default library support-v4 from project, I understand that all it's alright.

But, When I going to run my project to mobile device, I get next error:

Error:Gradle: Execution failed for task ':projectname:dexDebug'.

com.android.ide.common.internal.LoggedErrorException: Failed to run command:

\path\Android\sdk\build-tools\19.0.0\dx.bat --dex --output
\my\path\project\subproject\build\dex\debug \my\path\project\subproject\build\classes\debug 
\my\path\project\subproject\build\dependency-cache\debug 
\my\path\project\subproject\build\pre-dexed\debug\asmack-android-9-0.8.9-658505d39f87eb574ca22fd661701c368ba56c43.jar 
\my\path\project\subproject\build\pre-dexed\debug\classes-240d6f35592e28516cbacf563e5cf7d397c47597.jar
\my\path\project\subproject\build\pre-dexed\debug\classes-5396eb2d8c60977d5c7d783ccb88f7ee9f9ac0a5.jar 
\my\path\project\subproject\build\pre-dexed\debug\db4o-8.0.249.16098-all-java5-f4e9589ca8c62f8f04674804913667eaa24098e1.jar 
\my\path\project\subproject\build\pre-dexed\debug\jackson-annotations-2.2.2-c64587b3c9ef74b3bc31853dd03c4298b0d1c8e7.jar 
\my\path\project\subproject\build\pre-dexed\debug\jackson-core-2.2.2-0680a2e78b72cbcf4363912327628ec2dc9d9e8c.jar 
\my\path\project\subproject\build\pre-dexed\debug\jackson-databind-2.2.2-a3024e8795a241f49dd299408ad4afcc1550bc6f.jar 
\my\path\project\subproject\build\pre-dexed\debug\prime-0.6.1-235f838c02be4726326732322228c79267281ab3.jar 
\my\path\project\subproject\build\pre-dexed\debug\spring-android-auth-1.0.1.RELEASE-23860c8c94cb82ac5a3173256145e410145bf99a.jar 
\my\path\project\subproject\build\pre-dexed\debug\spring-android-core-1.0.1.RELEASE-18a42ae43d0082129e8fa1dbd9a76d7665ec6c44.jar 
\my\path\project\subproject\build\pre-dexed\debug\spring-android-rest-template-1.0.1.RELEASE-d7e59e9cc594e05c76438d8db3ed6ec4dafe5f04.jar 
\my\path\project\subproject\build\pre-dexed\debug\support-v4-19.1.0-463587e3dcdb43547b185cddcf5e895c619b1cbc.jar
Error Code:
    2
Output:
    UNEXPECTED TOP-LEVEL EXCEPTION:
    java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536
        at com.android.dx.merge.DexMerger$6.updateIndex(DexMerger.java:500)
        at com.android.dx.merge.DexMerger$IdMerger.mergeSorted(DexMerger.java:275)
        at com.android.dx.merge.DexMerger.mergeMethodIds(DexMerger.java:489)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:166)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:187)
        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)

回答1:

Dalvik VM can have a maximum of 65536 methods per dex file

Try this http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html

"You can partition part of the program into multiple secondary dex files, and load them at runtime."

Check this also https://stackoverflow.com/a/18069006/3020568