【问题标题】:Gradle - Unexpected top-level exception - Out of nowhereGradle - 意外的顶级异常 - 无处不在
【发布时间】:2014-06-16 07:57:50
【问题描述】:

相当肯定 gradle 不适合我。几天前开始了一个运行良好的项目。更新了 android studio 并再次打开了项目。我已经尝试了我能想到的一切,从删除/更新库检查 xml 文件和结构。删除 gradle 缓存并安装最新的 jdk,这里似乎没有任何帮助。

也尝试添加:

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
}

在stackoverflow上的其他帖子中提到

我从控制台导出了错误,它看起来像这样:

objc[2338]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.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)
:Derp:dexDerpDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':Derp:dexDerpDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Applications/Android Studio.app/sdk/build-tools/19.1.0/dx --dex --num-threads=4 

--output /Users/MorePathStuffForALongWhile

  Error Code:
    2
  Output:
    objc[2338]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/bin/java and /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk/Contents/Home/jre/lib/libinstrument.dylib. One of the two will be used. Which one is undefined.

【问题讨论】:

  • 我不认为 Hiam 所描述的问题被您提供的 Scott Barta 链接所捕获。
  • 你迁移到 Dagger 1.2.2 了吗?我得到的只是将我的 Dagger 依赖项从 1.1.x 更改为 1.2.2。虽然看起来可能是基于其他搜索结果的“Android 应用程序中的方法太多”。
  • 作为回应,我很确定这是“方法太多”错误:)

标签: android android-studio android-gradle-plugin build.gradle


【解决方案1】:

你的问题:你已经通过了大约。编译应用程序时的 65000 方法限制。您必须找到一种方法来减少应用中的方法数量。

如果您使用的是 Google Play Services,可以尝试this method 进行更细粒度的编译。

如果没有,您可以简单地使用 multiDex(确保您使用的是最新版本的 Android Studio)。在你的 build.gradle 文件中使用它:

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

【讨论】:

    【解决方案2】:

    如果您使用 gson 或 jackson,请尝试删除其依赖项。它对我有用。 compile 'com.google.code.gson:gson:2.1

    【讨论】:

      【解决方案3】:

      您的源代码 + 所有包含的库最终会得到超过 65k 的方法定义。目前是limitation of dex

      为了避免这种情况,引入了multidex support。编译时输出类被拆分并捆绑在多个 dex 文件中。并且在运行时它们是从多个 dex 文件中加载的。

      要实现这一点,您需要添加 multidex 支持:

      android {
      compileSdkVersion 21
      buildToolsVersion "21.1.0"
      
      defaultConfig {
          ...
          minSdkVersion 14
          targetSdkVersion 21
          ...
      
          // Enabling multidex support.
          multiDexEnabled true
      }
      ...
      }
      
      dependencies {
        compile 'com.android.support:multidex:1.0.0'
      }
      

      并设置一个扩展MultiDexApplication的Application类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-07
        • 2015-08-29
        • 1970-01-01
        • 2014-11-04
        相关资源
        最近更新 更多