Multidex app仍然超过64K方法限制

时间:2017-12-14 13:44:10

标签: android android-multidex

我在使用multidex时遇到了一个奇怪的问题。我已经将我的应用程序多索引了很长时间,但最近我无法再构建它了。它是在项目中配置Kotlin之后开始的。

Android Studio中的“运行”选项:应用在我的设备中成功运行。但是,如果我尝试“构建APK”选项或运行gradlew assembleDebug,则构建将失败,但通常例外:

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:com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536

但是, multidex在添加Kotlin之前正常工作。我可以在添加Kotlin之前检查提交,构建apk并检查有3个.dex文件,方法总数超过100k。

一些关键细节:

  • Gradle 4.1,使用Gradle插件3.0.1
  • Build-tools版本26.0.2
  • Kotlin版本1.2.0

在编程之后,应用程序不需要multidex,但我真的不想进行调试编译,此外,这个应该正常工作。

这是完整的build.gradle脚本:

buildscript {
    repositories {
        mavenCentral()
    }
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'idea'



repositories {
    mavenCentral()
    jcenter()
    maven { url "https://maven.google.com" }
    maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    maven { url 'https://jitpack.io' }
}

idea {
    module {
        downloadJavadoc = true
        downloadSources = true
    }
}

// read keystore for app signing
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = null
if (keystorePropertiesFile.exists()) {
    keystoreProperties = new Properties()
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}


def getDate() {
    def date = new Date()
    def formattedDate = date.format('yyyy.MM.dd')
    return formattedDate
}

def getCustomProguardFiles() {
    return fileTree(dir: "proguard", include: ["*.pro"]).asList().toArray()
}

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        applicationId "my.app.id"
        multiDexEnabled true

        manifestPlaceholders = [
                'appAuthRedirectScheme': 'my.app.id'
        ]

        //TODO do not put this into release
        resValue "string", "google_client_id", google_appId + ".apps.googleusercontent.com"
        resValue "string", "google_auth_redirect_uri", "com.googleusercontent.apps." + google_appId + ":/oauth2redirect"
    }

    dexOptions {

    }

    dexOptions {
        javaMaxHeapSize "2g"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties != null ? keystoreProperties['keyAlias'] : null
            keyPassword keystoreProperties != null ? keystoreProperties['keyPassword'] : null
            storeFile keystoreProperties != null ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties != null ? keystoreProperties['storePassword'] : null
        }
    }


    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt')
            proguardFiles getCustomProguardFiles()
            testProguardFiles getDefaultProguardFile('proguard-android.txt')
            testProguardFiles getCustomProguardFiles()
            signingConfig signingConfigs.release
        }
        debug {
            debuggable true
            minifyEnabled false
        }
    }

    flavorDimensions "buildType"

    productFlavors {
        develop {
            applicationIdSuffix ""
            dimension "buildType"
        }
        product {
            dimension "buildType"
        }
        ci {
            dimension "buildType"
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all {
            outputFileName = "${variant.name}-${variant.versionName}.apk"
        }
    }
    compileOptions.incremental = false
}

dependencies {

    /********** DEBUGGING **********/
    // Chrome debug bridge
    compile 'com.facebook.stetho:stetho:1.5.0'
    compile 'com.facebook.stetho:stetho-okhttp3:1.5.0'

    // Memory leak debugging
    //  1.5.2 held back because of a bug with gradle 3.0
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    androidTestCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'


    /********** SUPPORT **********/
    // Multidex support
    compile 'com.android.support:multidex:1.0.2'

    // Support libs
    compile "com.android.support:appcompat-v7:$libVersions.android.support"
    compile "com.android.support:cardview-v7:$libVersions.android.support"
    compile "com.android.support:customtabs:$libVersions.android.support"
    compile "com.android.support:design:$libVersions.android.support"
    compile "com.android.support:gridlayout-v7:$libVersions.android.support"
    compile "com.android.support:palette-v7:$libVersions.android.support"
    compile "com.android.support:preference-v7:$libVersions.android.support"
    compile "com.android.support:recyclerview-v7:$libVersions.android.support"
    compile "com.android.support:support-annotations:$libVersions.android.support"
    compile "com.android.support:support-v4:$libVersions.android.support"
    compile "com.android.support:support-v13:$libVersions.android.support"

    // Testing dependencies
    //  Made explicit to avoid conflicts with other testing libs
    androidTestCompile "com.android.support:appcompat-v7:$libVersions.android.support"
    androidTestCompile "com.android.support:design:$libVersions.android.support"
    androidTestCompile "com.android.support:recyclerview-v7:$libVersions.android.support"
    androidTestCompile "com.android.support:support-annotations:$libVersions.android.support"
    androidTestCompile "com.android.support:support-v4:$libVersions.android.support"


    /********** TESTING **********/
    // JUnit
    testCompile 'junit:junit:4.12'

    // Mockito
    testCompile "org.mockito:mockito-core:2.8.47"

    // Support testing
    androidTestCompile 'com.android.support.test.espresso:espresso-contrib:3.0.0'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.0'
    androidTestCompile 'com.android.support.test:rules:1.0.0'
    androidTestCompile 'com.android.support.test:runner:1.0.0'


    /********** UTILITIES **********/
    // HTML parsing
    compile 'org.jsoup:jsoup:1.10.2'

    // Event bus
    compile 'org.greenrobot:eventbus:3.0.0'

    // FHIR
    compile 'ca.uhn.hapi.fhir:hapi-fhir-android:3.1.0'
    compile 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:3.1.0'

    // Jobs
    compile 'com.evernote:android-job:1.1.11'

    // JSON serializing-deserializing
    compile 'com.google.code.gson:gson:2.8.1'

    // JWT validation
    compile 'com.nimbusds:nimbus-jose-jwt:4.41.1'

    // Lang utilities
    //  Used for Levenshtein distance, etc
    compile 'org.apache.commons:commons-lang3:3.6'
    compile 'org.apache.commons:commons-text:1.1'

    // OpenID authentication
    compile 'net.openid:appauth:0.7.0'

    // QR code scanning
    compile 'com.google.zxing:core:3.3.0'
    compile 'com.journeyapps:zxing-android-embedded:3.5.0@aar'

    // Play Services (for Firebase InstanceID)
    compile 'com.google.android.gms:play-services-auth:11.6.0'

    // Recurrence processing
    compile 'org.dmfs:lib-recur:0.10'

    // REST services
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'

    // SQLite ORM
    compile 'com.j256.ormlite:ormlite-android:5.0'

    // Time and date handling
    compile 'com.fatboyindustrial.gson-jodatime-serialisers:gson-jodatime-serialisers:1.6.0'
    compile 'joda-time:joda-time:2.9.7'

    // View/resource binding
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    compile 'com.jakewharton:butterknife:8.8.1'


    /********** VIEWS AND VIEW UTILITIES **********/
    // About page with libraries
    compile('com.mikepenz:aboutlibraries:5.2.5@aar') {
        transitive = true
    }

    // Calendar views
    compile 'com.roomorama:caldroid:3.0.1'
    compile 'com.squareup:android-times-square:1.6.4@aar'

    // Circular progress "pie" view
    //  Used for pill picker
    compile 'com.github.filippudak.progresspieview:library:1.0.4'

    // Dialogs with material style
    compile 'com.github.javiersantos:MaterialStyledDialogs:2.0'

    // Floating Action Button
    compile 'com.getbase:floatingactionbutton:1.9.0'

    // GIF ImageView
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.2.3'

    // Icons
    compile 'com.mikepenz:community-material-typeface:1.9.32.2@aar'
    compile 'com.mikepenz:google-material-typeface:2.1.2.1@aar'
    compile 'com.mikepenz:iconics-core:2.7.1@aar'

    // Image loading
    compile 'com.squareup.picasso:picasso:2.5.2'

    // Intro slides
    compile 'com.heinrichreimersoftware:material-intro:1.5.8'

    // Material style utilities
    compile('com.mikepenz:materialize:0.2.7@aar') {
        transitive = true
    }

    // Picker DialogFragments
    compile 'com.code-troopers.betterpickers:library:3.1.0'

    // Round image view
    compile 'com.makeramen:roundedimageview:1.5.0'

    // Tab strip
    compile 'com.astuetz:pagerslidingtabstrip:1.0.1'

    // Sliding left drawer
    compile('com.mikepenz:materialdrawer:4.4.1@aar') {
        transitive = true
    }

    // Recyclerview adapters
    compile('com.mikepenz:fastadapter:2.1.5@aar') {
        transitive = true
    }
    compile 'com.mikepenz:fastadapter-commons:2.1.0@aar'
    compile 'com.mikepenz:fastadapter-extensions:2.1.0@aar'
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Schedule step-by-step building
    compile 'com.stepstone.stepper:material-stepper:3.3.0'
    compile 'com.wdullaer:materialdatetimepicker:3.2.2'
    compile 'com.shawnlin:number-picker:2.4.4'
}

apply plugin: 'com.google.gms.google-services'

项目的build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = '1.2.0'
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.0.0'
    }
    ext {
        libVersions = [
                android: [
                        support: '25.4.0'
                ]
        ]
    }
}

1 个答案:

答案 0 :(得分:2)

结果证明我的方式存在问题HAPI-FHIR。它是一个非常大的库,有很多依赖项,它可能正在崩溃主要的dexfile。

最后,我通过替换:

解决了这个问题
// FHIR
compile 'ca.uhn.hapi.fhir:hapi-fhir-android:3.1.0'
compile 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:3.1.0'

使用:

// FHIR
compile 'ca.uhn.hapi.fhir:hapi-fhir-base:3.1.0@jar'
compile 'ca.uhn.hapi.fhir:hapi-fhir-utilities:3.1.0@jar'
compile 'ca.uhn.hapi.fhir:hapi-fhir-structures-dstu3:3.1.0@jar'
compile 'commons-io:commons-io:2.5'
compile 'org.slf4j:slf4j-api:1.7.25@jar'

commons-ioslf4j是HAPI的运行时依赖项,当使用@jar时,不会下载这些依赖项,因此需要显式声明它们。

注意:此特定配置仅对我的用例有效,因为HAPI具有更多的运行时依赖性,如果需要,还需要手动添加。