【问题标题】:UNEXPECTED TOP-LEVEL EXCEPTION in IntelliJ with Android Gradle使用 Android Gradle 的 IntelliJ 中出现意外的顶级异常
【发布时间】:2023-03-30 20:36:03
【问题描述】:

首先,我是 IntelliJ 和 Gradle 的新手。

我正在将一个 Android Eclipse 项目迁移到 IntelliJ,但是我遇到了一些问题。

我知道在一个常见的 Eclipse 项目中,库是手动导入的,这些依赖项必须以相同的方式导入,但是当从 gradle 导入时,依赖项会像 maven 一样自动导入。

目前,我有下一个配置。

在我的项目中:

Gradle 配置:

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

在 libs 文件夹中(Eclipse 项目依赖的旧继承):

-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

所有这些库都以“编译”范围从模块依赖面板添加到项目中。

在我的 gradle 中,正如你在上面看到的那样。

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

我也可以从模块依赖面板中看到播放服务和羊皮纸库也添加了。

然后,我在终端中使用命令“gradle -q dependencies”查找库不兼容问题。

+--- 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

由于我之前从项目中删除了默认库 support-v4,我知道一切都很好。

但是,当我要在移动设备上运行我的项目时,我收到下一个错误:

错误:Gradle:任务 ':projectname:dexDebug' 执行失败。

com.android.ide.common.internal.LoggedErrorException:运行命令失败:

\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)

【问题讨论】:

    标签: android intellij-idea gradle


    【解决方案1】:

    Dalvik VM 每个 dex 文件最多可以有 65536 个方法

    试试这个http://android-developers.blogspot.co.il/2011/07/custom-class-loading-in-dalvik.html

    "您可以将程序的一部分分割成多个二级dex文件,并在运行时加载。"

    也检查一下https://stackoverflow.com/a/18069006/3020568

    【讨论】:

    • 非常感谢,我不知道这个问题,你给了我正确的指导。例如,将 db4o-all 库替换为 db4o-core 就足以解决问题。但是,在这个未来的问题之前,你知道如何用 gradle insteaf of ant 修复吗?
    • 将 multiDexEnabled true 添加到 build.gradle 文件的 defaultConfig 中
    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 1970-01-01
    • 2015-09-07
    • 2015-08-29
    • 2014-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多