如何修复minSdkVersion 21中的DEX 64K方法限制?

时间:2016-07-21 07:45:50

标签: java android

创建APK文件时出现此错误:

  

错误:.dex文件中的方法引用数不能超过64K。   了解如何在https://developer.android.com/tools/building/multidex.html

解决此问题

我引用此tutorial来解决此问题,但我仍然遇到此错误。

我在我的项目中使用了MultiDex:

在biuld.gradle中为sdk 16:

apply plugin: 'com.android.application'

repositories {
    jcenter()
    mavenCentral()
}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.saly.test"

        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':volley')
    compile 'com.android.support:multidex:1.0.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.google.android.gms:play-services:9.2.1'
    compile 'com.google.android.gms:play-services-location:9.2.1'
    compile 'com.google.android.gms:play-services-gcm:9.2.1'
    compile 'com.google.android.gms:play-services-maps:9.2.1'
    compile 'com.onesignal:OneSignal:2.+@aar'
}

在清单中为sdk 16:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    ...>

    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">

        ...

    </application>
   </manifest>

现在我收到了新的错误:

  

错误:UNEXPECTED TOP-LEVEL错误:错误:java.lang.OutOfMemoryError:GC   超出开销限制错误:at   com.android.dx.cf.cst.ConstantPoolParser.parse0(ConstantPoolParser.java:289)   错误:at   com.android.dx.cf.cst.ConstantPoolParser.parse(ConstantPoolParser.java:150)   错误:at   com.android.dx.cf.cst.ConstantPoolParser.parseIfNecessary(ConstantPoolParser.java:124)   错误:at   com.android.dx.cf.cst.ConstantPoolParser.getPool(ConstantPoolParser.java:115)   错误:at   com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:491)   错误:at   com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)   错误:at   com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)   错误:at   com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)   错误:在com.android.dx.command.dexer.Main.parseClass(Main.java:772)   错误:在com.android.dx.command.dexer.Main.access $ 1500(Main.java:85)   错误:at   com.android.dx.command.dexer.Main $ ClassParserTask.call(Main.java:1700)   错误:at   com.android.dx.command.dexer.Main.processClass(Main.java:755)   错误:at   com.android.dx.command.dexer.Main.processFileBytes(Main.java:723)   错误:在com.android.dx.command.dexer.Main.access $ 1200(Main.java:85)   错误:at   com.android.dx.command.dexer.Main $ FileBytesConsumer.processFileBytes(Main.java:1653)   错误:at   com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)   错误:at   com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)   错误:at   com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)   错误:在com.android.dx.command.dexer.Main.processOne(Main.java:677)   错误:at   com.android.dx.command.dexer.Main.processAllFiles(Main.java:569)   错误:在com.android.dx.command.dexer.Main.runMultiDex(Main.java:366)   错误:在com.android.dx.command.dexer.Main.run(Main.java:275)   错误:在com.android.dx.command.dexer.Main.main(Main.java:245)   错误:在com.android.dx.command.Main.main(Main.java:106)   :app:transformClassesWithDexForDebug FAILED错误:执行失败   任务&#39;:app:transformClassesWithDexForDebug&#39;。   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:进程&#39;命令   &#39; C:\ Program Files \ Java \ jdk1.8.0_66 \ bin \ java.exe&#39;&#39;完成了   非零退出值3

然后我将最小sdk更改为21以修复此错误,但它无效。

这是我对sdk 21的build.gradle:

apply plugin: 'com.android.application'

repositories {
    jcenter()
    mavenCentral()
}

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"
    defaultConfig {
        applicationId "com.saly.test" 

        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile project(':volley')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.google.android.gms:play-services:9.2.1'
    compile 'com.google.android.gms:play-services-location:9.2.1'
    compile 'com.google.android.gms:play-services-gcm:9.2.1'
    compile 'com.google.android.gms:play-services-maps:9.2.1'
}

如何解决此错误?

提前感谢。

2 个答案:

答案 0 :(得分:2)

只需添加这些行

即可
dexOptions {
    javaMaxHeapSize "4g"
}

在你的gradle脚本里面的任何地方

android { 
....

dexOptions {
    javaMaxHeapSize "4g"
}

....
}

答案 1 :(得分:-1)

您需要做的就是添加:

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        ...
        minSdkVersion 14
        targetSdkVersion 21
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
}

dependencies {
   compile 'com.android.support:multidex:1.0.1'
}

这是AndroidManifest.xml

<?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">
        ...
    </application>
</manifest>

或者如果你有你自己的Application类实现,请覆盖attachBaseContext内的Application方法:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}