MultiDex problems with Gradle and ProGuard

时光总嘲笑我的痴心妄想 提交于 2019-12-12 03:24:41

问题


I am having difficulties with MultiDex. I have enabled it in Gradle (multiDexEnabled true) and main class is calling MultiDex.install in attachBaseContext.

I suspect the Gradle is not adding MultiDexExtractor class to first dex file (it is not in maindexlist_deobfuscated.txt).

Snippet from log:

06-03 07:56:59.030 4088-4088/xxx.android I/dalvikvm: Could not find method android.support.multidex.MultiDexExtractor.verifyZipFile, referenced from method android.support.multidex.MultiDex.checkValidZipFiles
06-03 07:56:59.030 4088-4088/xxx.android W/dalvikvm: VFY: unable to resolve static method 195: Landroid/support/multidex/MultiDexExtractor;.verifyZipFile (Ljava/io/File;)Z
06-03 07:56:59.030 4088-4088/xxx.android D/dalvikvm: VFY: replacing opcode 0x71 at 0x0010

Exception:

06-03 07:56:59.030 4088-4088/xxx.android E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    java.lang.NoClassDefFoundError: android.support.multidex.MultiDexExtractor
                                                                                        at android.support.multidex.MultiDex.install(Unknown Source)
                                                                                        at xxx.android.AndroidLauncher.attachBaseContext(Unknown Source)
                                                                                        at android.app.Activity.attach(Activity.java:4965)
                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2008)
                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
                                                                                        at android.app.ActivityThread.access$600(ActivityThread.java:130)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                        at android.os.Looper.loop(Looper.java:137)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:4745)
                                                                                        at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                        at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                                        at dalvik.system.NativeStart.main(Native Method)

Do I have to (no idea how) modify a list which is generated by Gradle or is problem somewhere else? How do I make it working with MultiDex?

PS: I am using ProGuard. I think it is not messing with things, but to be sure here is the part for MultiDex:

-keep class android.support.multidex.**
-keepclassmembernames class android.support.multidex.**{*;}
-keepclassmembers class android.support.multidex.** {*;}

EDIT: Here is the Android file (it's mainly generated by libGDX boostrap app):

android {
    buildToolsVersion "23.0.1"
    compileSdkVersion 23
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }

        instrumentTest.setRoot('tests')
    }

    lintOptions {
        abortOnError false // make sure you're paying attention to the linter output!
    }

// FIXME: How can we apply this simply for all builds? Copy-pasta makes me sad.
    buildTypes {
        release {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
            proguardFile 'proguard-project.txt'
        }
        debug {
            minifyEnabled true
            proguardFile getDefaultProguardFile('proguard-android-optimize.txt')
            proguardFile 'proguard-project.txt'
        }
    }

    dexOptions {
        javaMaxHeapSize "6g"
    }

    packagingOptions {
        exclude 'rootdoc.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/services/javax.script.ScriptEngineFactory'
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
        multiDexEnabled true
    }

    dexOptions {
        preDexLibraries = false
    }
}

// called every time gradle gets executed, takes the native dependencies of
// the natives configuration, and extracts them to the proper libs/ folders
// so they get packed with the APK.
task copyAndroidNatives() {
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
        if (jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if (jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if (outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}
task run(type: Exec) {
    def path
    def localProperties = project.file("../local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        if (sdkDir) {
            path = sdkDir
        } else {
            path = "$System.env.ANDROID_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'xxx.android/xxx.android.AndroidLauncher'
}
// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitely, SpringSource Gradle Eclipse plugin
    // ignores any nodes added in classpath.file.withXml
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }

    jdt {
        sourceCompatibility = 1.6
        targetCompatibility = 1.6
    }

    classpath {
        plusConfigurations += [project.configurations.compile]
        containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
    }

    project {
        name = appName + "-android"
        natures 'com.android.ide.eclipse.adt.AndroidNature'
        buildCommands.clear();
        buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
        buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
        buildCommand "org.eclipse.jdt.core.javabuilder"
        buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
    }
}
// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
        scopes = [COMPILE: [plus: [project.configurations.compile]]]

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
                            option(name: "UPDATE_PROPERTY_FILES", value: "true")
                        }
                    }
                }
            }
        }
    }
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
    compile fileTree(dir: 'libs_jar', include: '*.jar')
}

PS: It doesn't seem to make a difference if I add the multidex library to dependencies or not.


回答1:


Had you extend your application class with MultiDexApplication?

if yes, then post your build.gradle file.

I don't have the power to comment, thats why I am posting a link. Check this link




回答2:


Add this too in android tag

android {
    ...
     afterEvaluate {
            tasks.matching {
                it.name.startsWith('dex')
            }.each { dx ->
                if (dx.additionalParameters == null) {
                    dx.additionalParameters = ['--multi-dex']
                } else {
                    dx.additionalParameters += '--multi-dex'
                }
            }
        }
}


来源:https://stackoverflow.com/questions/37614300/multidex-problems-with-gradle-and-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!