.dex文件中的方法引用数不能超过64k API 17

San*_*ana 122 android gradle android-gradle-plugin sugarorm android-multidex

我正在使用SugarORM Library构建一个应用程序但是当我尝试为API 17构建项目时(没有检查其他人)它显示了构建错误.

    Information:Gradle tasks [:app:assembleDebug]
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAnimatedVectorDrawable2330Library UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72330Library UP-TO-DATE
:app:prepareComAndroidSupportCardviewV72330Library UP-TO-DATE
:app:prepareComAndroidSupportDesign2330Library UP-TO-DATE
:app:prepareComAndroidSupportMediarouterV72300Library UP-TO-DATE
:app:prepareComAndroidSupportRecyclerviewV72330Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42330Library UP-TO-DATE
:app:prepareComAndroidSupportSupportVectorDrawable2330Library UP-TO-DATE
:app:prepareComAndroidVolleyVolley100Library UP-TO-DATE
:app:prepareComGithubSatyanSugar14Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServices840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAds840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAnalytics840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppindexing840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppinvite840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAppstate840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesAuth840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBase840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesBasement840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesCast840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesDrive840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesFitness840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGames840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesGcm840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesIdentity840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesLocation840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMaps840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesMeasurement840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesNearby840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesPanorama840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesPlus840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesSafetynet840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesVision840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesWallet840Library UP-TO-DATE
:app:prepareComGoogleAndroidGmsPlayServicesWearable840Library UP-TO-DATE
:app:prepareMeDrakeetMaterialdialogLibrary131Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:compileDebugJavaWithJavac
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:prePackageMarkerForDebug
:app:transformClassesWithDexForDebug
Error:The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 2
Information:BUILD FAILED
Information:Total time: 21.663 secs
Information:2 errors
Information:0 warnings
Information:See complete output in console
Run Code Online (Sandbox Code Playgroud)

但是当我为android v5.0或更高版本构建这个项目时,它运行正常.如果我删除SugarORM gradle依赖项,它对于v4.2.2和v5.0设备都可以正常工作.

Gab*_*tti 246

你有太多的方法.dex只能有65536种方法.

如建议您可以使用multidex支持.

只需在以下位置添加以下行module/build.gradle:

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

此外,您还可以ManifestMultiDexApplicationmultidex支持库中的类添加到application元素中

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.android.multidex.myapplication">
        <application
            ...
            android:name="android.support.multidex.MultiDexApplication">
<!--If you are using your own custom Application class then extend -->
<!--MultiDexApplication and change above line as-->
            android:name=".YourCustomApplicationClass">

            ...
        </application>
    </manifest>
Run Code Online (Sandbox Code Playgroud)

覆盖attachBaseContext方法

@Override
protected void attachBaseContext(Context newBase) {
    super.attachBaseContext(newBase);
    MultiDex.install(this);
}
Run Code Online (Sandbox Code Playgroud)

如果您使用自己的Application类,请将父类更改ApplicationMultiDexApplication.

另一种解决方案是尝试使用ProGuard删除未使用的代码- 为应用程序配置ProGuard设置以运行ProGuard,并确保为发布版本启用了收缩功能.

  • 如果我没有太多方法怎么办?我的应用程序上没有那​​么多方法.我相信它甚至达不到这一点. (4认同)
  • 我只在我的build.grandle文件中执行了这两部分:"multiDexEnabled true"并编译'com.android.support:multidex:1.0.0'.我没有必要更改清单文件中的任何内容,也没有提到有关覆盖attachBaseContext等的其他内容.但是您的解决方案为我解决了问题!谢谢. (4认同)
  • 我正在研究 flutter,并且在 defaultConfig 中仅添加 multiDexEnabled true 对我有用,thnx (4认同)
  • @NeonWarge可能是由于您的依赖性。也许您已经包含了整个Google Play服务?选中此[答案以获取详细信息](http://stackoverflow.com/a/27589562/2304737) (2认同)

Dha*_*hra 91

android {

compileSdkVersion 23

 buildToolsVersion '23.0.0'

    defaultConfig {
        applicationId "com.dkm.example"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
Run Code Online (Sandbox Code Playgroud)

把它放在你的defaultConfig中:

multiDexEnabled true 
Run Code Online (Sandbox Code Playgroud)

它为我工作

  • 这对于棒棒糖或更高只是不是吗? (3认同)

小智 20

更改应用级别 build.gradle :

android {

compileSdkVersion 23

 buildToolsVersion '23.0.0'

    defaultConfig {
        applicationId "com.dkm.example"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
Run Code Online (Sandbox Code Playgroud)

它对我有用。

  • 是的,这是一个老问题,但对于最新版本的 API,我们只需要 `multiDexEnabled` 选项,无需依赖项或清单文件更改 (3认同)

Qua*_*yen 18

我收到此错误消息,因为编码我的项目时自动更新编译版本在我的build.gradle文件中:

android {
    ...
    buildToolsVersion "23.0.2"
    ...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0' }
Run Code Online (Sandbox Code Playgroud)

通过更正版本来解决它:

android {
        ...
        buildToolsVersion "23.0.2"
        ...
    }

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.android.support:design:23.0.1'
}
Run Code Online (Sandbox Code Playgroud)

  • 幸运的是,这种方法很有效,因为新的编译库有太多的方法(超过65536),旧的库没有.所以这只是一个快速修复,迟早你应该通过添加multidex支持来解决,比如接受的答案. (4认同)
  • 我正在创建简单的应用程序,在我尝试使用Google Map Objects教程之前一直运行良好.突然间我再也无法编译了.这个解决方案为我解决了这个问题.你到底怎么解决这个问题? (2认同)

Thu*_*ick 13

这对我有用:

发生这种情况是因为有太多未使用的方法.大多数这些方法都来自build.gradle中包含的库

使用minify和shrink资源来解决这个问题,同时使用gradle并清理代码.

buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            shrinkResources true
        }
    }
Run Code Online (Sandbox Code Playgroud)


小智 11

当您仅使用afew加载所有Google Play服务apis时,也会发生此错误.

正如谷歌所述:"在6.5之前的Google Play服务版本中,您必须将整个API包编译到您的应用中.在某些情况下,这样做会使您在应用中保留方法数量变得更加困难(包括65,536限制下的框架API,库方法和您自己的代码.

从版本6.5开始,您可以选择性地将Google Play服务API编译到您的应用中."

例如,当您的应用需要播放服务地图,播放服务位置时.您需要在应用级别的build.gradle文件中仅添加两个api,如下所示:

compile 'com.google.android.gms:play-services-maps:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
Run Code Online (Sandbox Code Playgroud)

代替:

compile 'com.google.android.gms:play-services:10.2.1'
Run Code Online (Sandbox Code Playgroud)

有关完整文档和Google Play服务列表,请单击此处


Bas*_*asi 10

当您的应用引用超过65,536 个方法时,您会遇到构建错误,表明您的应用已达到 Android 构建架构的限制

Android 5.0 之前的 Multidex 支持

Android 5.0(API 级别 21)之前的平台版本使用 Dalvik 运行时来执行应用程序代码。默认情况下,Dalvik 将应用程序限制为每个 APK 的单个 classes.dex 字节码文件。为了解决这个限制,您可以将 multidex 支持库添加到您的项目中:

dependencies {
  implementation 'com.android.support:multidex:1.0.3'
}
Run Code Online (Sandbox Code Playgroud)

对 Android 5.0 及更高版本的 Multidex 支持

Android 5.0(API 级别 21)及更高版本使用称为 ART 的运行时,该运行时本机支持从 APK 文件加载多个 DEX 文件。因此,如果您的 minSdkVersion 为 21 或更高,则不需要 multidex支持库。

避免 64K 限制

  • 使用 ProGuard 删除未使用的代码 - 启用代码收缩

在应用程序中配置 multidex

如果您的 minSdkVersion 设置为 21 或更高,您需要做的就是在模块级 build.gradle 文件中将 multiDexEnabled 设置为 true

android {
defaultConfig {
    ...
    minSdkVersion 21 
    targetSdkVersion 28
    multiDexEnabled true
  }
 ...
}
Run Code Online (Sandbox Code Playgroud)

如果您的 minSdkVersion 设置为 20 或更低,那么您必须使用 multidex 支持库

android {
defaultConfig {
    ...
    minSdkVersion 15 
    targetSdkVersion 28
    multiDexEnabled true
   }
   ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.3'
}
Run Code Online (Sandbox Code Playgroud)

覆盖 Application 类,将其更改为扩展MultiDexApplication(如果可能),如下所示:

public class MyApplication extends MultiDexApplication { ... }
Run Code Online (Sandbox Code Playgroud)

添加到清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="MyApplication" >
        ...
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)


And*_*eks 9

添加此以避免 React Native 或任何 Android 项目的 multidex 问题

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }  
}

dependencies {
  implementation 'com.android.support:multidex:1.0.3'  //with support libraries
  //implementation 'androidx.multidex:multidex:2.0.1'  //with androidx libraries
Run Code Online (Sandbox Code Playgroud)


小智 7

**

对于 Unity 游戏开发者

**

如果有人因为 Unity 项目中出现此错误而来到这里,请转到“文件”->“构建设置”->“播放器设置”->“播放器”。转到“发布设置”,然后在“构建”选项卡下启用“自定义启动器 Gradle 模板”。该文本下方将显示一条路径。转到路径并添加 multiDexEnabled true,如下所示:

defaultConfig {
    minSdkVersion **MINSDKVERSION**
    targetSdkVersion **TARGETSDKVERSION**
    applicationId '**APPLICATIONID**'
    ndk {
        abiFilters **ABIFILTERS**
    }
    versionCode **VERSIONCODE**
    versionName '**VERSIONNAME**'
    multiDexEnabled true
}
Run Code Online (Sandbox Code Playgroud)


Sud*_*nsu 6

我一直面临同样的问题,对于multidex支持,你必须记住你的应用程序的minSdkVersion.如果您使用的minSdkVersion 21或以上,然后只写multiDexEnabled真正喜欢这个

defaultConfig {
    applicationId *******************
    minSdkVersion 21
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}
Run Code Online (Sandbox Code Playgroud)

它对我有用,如果你使用21以下的minSdkVersion(在lolipop下面)那么你必须做两件额外的简单的事情

1.首先添加此依赖项

编译'com.android.support:multidex:1.0.1'

在你的build.gradle中.

2.上一行和第二行将以下一行添加到清单中的应用程序中

机器人:名字= "android.support.multidex.MultiDexApplication"

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:name="android.support.multidex.MultiDexApplication"
    android:theme="@style/AppTheme" >
    <activity android:name=".MainActivity" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

Bingo然后它将工作在较低版本也.. :)快乐的编码