我有以下争吵:

因此,对于较大的项目,我需要具有多个应用程序,并且其中包含可重复使用的库模块,以用于多种用途。不幸的是,似乎我的项目超出了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。

有人知道我可能还会尝试解决什么吗?

最佳答案

为低于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>

快乐编码:)

09-04 15:12
查看更多