MultiDexApplication not recognized

前端 未结 4 876
温柔的废话
温柔的废话 2020-12-20 11:14

Trying to use MultiDexApplication in my app, but the class is not recognized when I try to extend my application activity with it.

Here is my build.gradle file:

4条回答
  •  情话喂你
    2020-12-20 11:50

    I have followed THIS blog post according to which MultiDexApplication should be included in r21 of support library.

    My IDE had trouble resolving it also.

    I made it work for now with the help of MULTIDEX github project by adding (you can see more details on the project's page):

    android {
        dexOptions {
            preDexLibraries = false
        }
    }
    
    repositories {
      jcenter()
    }
    
    dependencies {
      compile 'com.google.android:multidex:0.1'
    }
    
    afterEvaluate {
        tasks.matching {
            it.name.startsWith('dex')
        }.each { dx ->
            if (dx.additionalParameters == null) {
                dx.additionalParameters = []
            }
            dx.additionalParameters += '--multi-dex' // enable multidex
    
            dx.additionalParameters += "--main-dex-list=$projectDir/multidex.keep".toString()
        }
    }
    

    and adding $project_dir/multidex.keep file with following contents:

    android/support/multidex/BuildConfig.class
    android/support/multidex/MultiDex$V14.class
    android/support/multidex/MultiDex$V19.class
    android/support/multidex/MultiDex$V4.class
    android/support/multidex/MultiDex.class
    android/support/multidex/MultiDexApplication.class
    android/support/multidex/MultiDexExtractor$1.class
    android/support/multidex/MultiDexExtractor.class
    android/support/multidex/ZipUtil$CentralDirectory.class
    android/support/multidex/ZipUtil.class
    

    The github project page mentions also some consideration for the contents of your implementation of MultiDexApplication class:

    • The static fields in your application class will be loaded before the MultiDex#installbe called! So the suggestion is to avoid static fields with types that can be placed out of main classes.dex file.
    • The methods of your application class may not have access to other classes that are loaded after your application class. As workarround for this, you can create another class (any class, in the example above, I use Runnable) and execute the method content inside it.

提交回复
热议问题