Android Multidex无法正常工作

时间:2018-08-02 20:41:02

标签: android gradle dex

我有以下争吵:

因此,对于一个较大的项目,我需要拥有多个带有可重用库模块的应用,以用于多种用途。不幸的是,似乎我的库模块中包含的类和库的基本数量超出了我项目的dex限制 具有以下gradle依赖项

dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        testImplementation 'junit:junit:4.12'

        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation "org.jetbrains.kotlin:kotlin-reflect:1.2.51"
        implementation "org.jetbrains.anko:anko:$anko_version"

        implementation 'com.android.support:multidex:1.0.3'

        androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

        implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
        implementation 'com.google.android.material:material:1.0.0-beta01'
        implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha1'
        implementation 'androidx.exifinterface:exifinterface:1.0.0-beta01'
        implementation "androidx.browser:browser:1.0.0-beta01"

        implementation 'com.android.volley:volley:1.1.0'

        implementation 'com.google.code.gson:gson:2.8.5'
        implementation 'com.google.android.gms:play-services-vision:15.0.2'

        implementation 'com.jakewharton:butterknife:8.8.1'
        annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

        implementation 'com.github.ornolfr:rating-view:0.1.2@aar'
        implementation 'com.github.siyamed:android-shape-imageview:0.9.3@aar'
        implementation 'com.github.bumptech.glide:glide:4.7.1'
        annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
        implementation("com.github.bumptech.glide:recyclerview-integration:4.6.1") {
            // Excludes the support library because it's already included by Glide.
            transitive = false
        }

        implementation 'net.cachapa.expandablelayout:expandablelayout:2.9.2'

        implementation 'io.realm:android-adapters:2.1.1'
    }

如您所见,它有很多库(我确实需要)。除此之外,我已经将multiDex设置为true,添加了支持multidex库(如您在依赖项中所见),并且我还使用了一个自MultidexApplication扩展的自定义Application类。

但是不幸的是我仍然得到

Error: Cannot fit requested classes in a single dex file (# methods: 68405 > 65536)

构建期间出错。似乎解决该问题的唯一方法是将我的minSDK设置为19到21,但是对于该项目,我需要将其设置为19。

有人知道我也可以尝试修复它吗?

2 个答案:

答案 0 :(得分:0)

要在API 21以下实现multidex,需要两个步骤。

第一步:

android {
defaultConfig {
    ...
    minSdkVersion 15 
    targetSdkVersion 26
    multiDexEnabled true
}
...
}
dependencies {
  compile 'com.android.support:multidex:1.0.3'
}

第二步: 从MultidexApplication扩展了您的应用程序类。

public class MyApplication extends MultiDexApplication { ... }

或者,如果您没有自定义应用程序类,请按如下所示更改您的AndroidManifest.xml类:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

快乐编码:)

答案 1 :(得分:0)

您当前正在使用 support multidex库,但已升级为使用 androidx

因此,您需要使用 androidx 版本。

即:

替换IEnumerable

category="chess", page=1