【问题标题】:android library crash on start MultiDex启动MultiDex时android库崩溃
【发布时间】:2018-02-16 12:30:13
【问题描述】:

我已经构建了一个 android 库(各种自定义按钮)并将其上传到我的 JFrog Artifactory 现在我尝试在一个示例应用程序中测试它(非常简单的默认活动)。

同步进行得很顺利,我将 Button 添加到 xml 布局中,当我运行应用程序时它在它开始之前崩溃:

java.lang.RuntimeException: Unable to instantiate application com.mylib.library.LibApp: java.lang.ClassNotFoundException: Didn't find class "com.mylib.library.LibApp" on path: DexPathList

哪个是扩展 MultiDexApplication 的类,

当我从库项目(使用另一个本地应用程序模块和活动)运行它时,它运行良好,但是当我从服务器编译它时,它会导致此错误

经过大量谷歌搜索后,我尝试清理和重建并禁用即时运行,我尝试仅从 xml 文件中删除视图,但它仍然崩溃,看来应用程序的库有问题。

有人可以帮忙吗?所有帮助将不胜感激!

编辑:

我正在使用 MultiDex,我尝试了两种方法来实现它,扩展 Application 和扩展 MultiDexApplication,结果相同,还尝试了重建清理等等,我也尝试了 -dontobfuscate,如果它是由于 proguard 引起的

这是库 build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26

    lintOptions {
        abortOnError false
    }

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0.1"
        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            multiDexEnabled true
        } }}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.google.firebase:firebase-messaging:11.0.4'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.google.dagger:dagger:2.9'
    compile 'com.squareup.retrofit2:retrofit:2.+'
    compile 'com.squareup.retrofit2:converter-gson:2.+'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.+'
    compile 'io.reactivex:rxjava:1.0.4'
    compile "com.google.android.gms:play-services-gcm:11.0.4"
    compile 'io.reactivex:rxandroid:0.24.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:support-v4:23.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'com.evernote:android-job:1.1.8'
    compile 'com.android.support:multidex:1.0.2'
}
apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 能否请您发布您的 gradle 构建文件
  • 图书馆的 ?或应用的
  • 我已经添加了库的构建文件,我无法添加应用程序,因为它在私有 Artifactory 上
  • 好的,请添加崩溃应用的构建文件

标签: android android-library android-multidex


【解决方案1】:

尝试添加到android部分:

dexOptions { javaMaxHeapSize "2g" }

另外,如果问题消失,请检查您是否从 api 19 更改为 api 21。

【讨论】:

    【解决方案2】:

    尝试将此添加到扩展 Application 的类中。

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
    

    添加multiDexEnabled true 不足以使其实际工作。

    编辑:

    如果这不适合您,还有其他选择:

    通过AndroidManifest.xml添加:

    <application
        android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
    

    或者,不是扩展Application,而是扩展MultiDexApplication

    public class MyApplication extends MultiDexApplication { ... }
    

    【讨论】:

    • 我的LibApp扩展了MultiDexApplication,是不是也一样?
    • 嗯,应该是。我通常使用第一个答案中的版本,尝试切换到它以查看是否适合您。
    • 另外,尝试删除 build 文件夹并重新构建,我知道这很奇怪,但当我遇到同样的问题时它有时会有所帮助。
    • 我尝试使用 attachbasecontext(),它给了我一个类似但不一样的错误:NoClassDefFoundError: Failed resolution of: Landroid/support/multidex/MultiDex;原因:java.lang.ClassNotFoundException:找不到类“android.support.multidex.MultiDex”
    【解决方案3】:

    当 3 个方库中的一个或多个未按预期构建时,就会发生这种情况。尝试检查这些库是否有更新版本

    【讨论】:

    • 我都升级到最新了,还是一样
    • 所以可能是有缺陷的库。尝试在它发生之前考虑您添加了哪个库并将其版本低/升级到稳定版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多