升级到 AndroidX 和 Android Studio 4.0 后出现错误 java.lang.ClassNotFoundException

Error java.lang.ClassNotFoundException After Upgrading to AndroidX and Android Studio 4.0

我最近刚刚将我的旧项目从目标 sdk 版本 26 升级到 28,并使用 AndroidX 来使用 WorkManager 库。我还将 Android Studio 升级到 4.0 版。

我有一个外部jar库commons-httpclient-3.1-rc1.jar (实现文件('libs/commons-httpclient-3.1-rc1.jar')).构建过程 运行 很好,我的应用程序可以正常启动。但是当我的应用程序启动后台同步过程(使用 workmanger)时,我收到了以下警告。同步 class 使用了一些来自 commons-httpclient 库的 classes。

Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/com.myapp.android-_mXRtE6XltfExOFTAj7PRQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp.android-_mXRtE6XltfExOFTAj7PRQ==/lib/x86, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at org.apache.commons.httpclient.util.EncodingUtil.<clinit>(EncodingUtil.java:54) 
    at org.apache.commons.httpclient.util.EncodingUtil.getBytes(EncodingUtil.java:202) 
    at org.apache.commons.httpclient.util.URIUtil.encode(URIUtil.java:502) 
    at org.apache.commons.httpclient.util.URIUtil.encodeAll(URIUtil.java:218) 
    at com.myapp.android.http.util.EncodeUtil.encode(EncodeUtil.java:12) 
    at com.myapp.android.http.params.HttpParams.getQueryString(HttpParams.java:69) 
    at com.myapp.android.http.kauth.KAuthProvider.getSignedUrl(KAuthProvider.java:52) 
    at com.myapp.android.http.client.HttpConnection.get(HttpConnection.java:62) 
    at com.myapp.android.http.impl.client.SyncConnection.syncMaster(SyncConnection.java:28) 
    at com.myapp.android.worker.SyncWorker.lambda$startWork[=12=]$SyncWorker(SyncWorker.java:103) 
    at com.myapp.android.worker.-$$Lambda$SyncWorker$jzconowJk0nkjIXH-ROtVtccjT8.attachCompleter(Unknown Source:4) 
    at androidx.concurrent.futures.CallbackToFutureAdapter.getFuture(CallbackToFutureAdapter.java:102) 
    at com.myapp.android.worker.SyncWorker.startWork(SyncWorker.java:66) 
    at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:281) 
    at android.os.Handler.handleCallback(Handler.java:873) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

我猜错误可能是在处理 Multidex 功能时出现的。我正在使用 Multidex,并且已经用 AndroidX 版本替换了旧的 Multidex。在我的 build.gradle 文件中

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

android {
    compileSdkVersion 28
    buildToolsVersion "30.0.0"
    flavorDimensions "version"

    productFlavors {
        dev {
            minSdkVersion 21 //The important bit.
            dimension "version"
        }
        prod {
            minSdkVersion 15 //Example. Set to your real requirement.
            dimension "version"
        }
    }

    defaultConfig {
        applicationId "com.myapp.android"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 7
        versionName "1.0.6"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            debuggable true
        }
    }
    lintOptions {
        disable 'TypographyFractions','TypographyQuotes'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation 'androidx.work:work-runtime:2.4.0-rc01'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.concurrent:concurrent-futures:1.0.0'
    implementation 'com.google.android.material:material:1.1.0-alpha04'
    
    implementation 'com.google.android.gms:play-services-auth:11.0.0'
    implementation 'com.google.android.gms:play-services-plus:11.0.0'
    implementation 'com.google.android.gms:play-services-analytics:11.0.0'
    implementation 'com.google.firebase:firebase-messaging:11.0.0'
    implementation 'com.google.firebase:firebase-config:11.0.0'
    implementation 'com.google.firebase:firebase-ads:11.0.0'
    implementation 'androidx.multidex:multidex:2.0.1'

    implementation 'com.loopj.android:android-async-http:1.4.9'
    implementation 'com.facebook.android:facebook-android-sdk:4.8.2'
    implementation 'com.github.clans:fab:1.6.2'
    implementation 'com.github.bumptech.glide:glide:3.5.2'
    implementation 'com.github.rey5137:material:1.2.2'
    implementation 'com.github.ganfra:material-spinner:1.1.1'
    implementation 'com.wdullaer:materialdatetimepicker:2.1.1'
    implementation 'com.soundcloud.android:android-crop:1.0.1@aar'
    implementation 'de.greenrobot:eventbus:2.4.0'
    implementation 'net.danlew:android.joda:2.9.4.3'
    implementation 'com.github.chrisbanes:PhotoView:1.3.1'
    implementation 'com.github.paolorotolo:appintro:4.1.0'

    implementation 'com.squareup.picasso:picasso:2.3.2'
    implementation 'com.nineoldandroids:library:2.4.0'
    implementation 'com.daimajia.slider:library:1.1.5@aar'
    implementation 'uk.co.chrisjenx:calligraphy:2.3.0'

    implementation fileTree(dir: 'libs', include: ['*.aar', '*.jar'], exclude: [])
    implementation files('libs/commons-httpclient-3.1-rc1.jar')

    implementation('com.crashlytics.sdk.android:crashlytics:2.6.8@aar') {
        transitive = true;
    }

}

apply plugin: 'com.google.gms.google-services'

并且在 BaseApplication 中 class

import androidx.multidex.MultiDexApplication

public class BaseApplication extends MultiDexApplication {}

我尝试过清理项目、重建、使缓存无效并重新启动,但都没有用。 有什么解决办法吗?

谢谢

我已经解决了这个问题。从这个答案中找到解决方案

java.lang.NoClassDefFoundError:解析失败:Lorg/apache/http/ProtocolVersion

解决方法是添加

<uses-library android:name="org.apache.http.legacy" android:required="false" />

在清单文件中。

我没有使用 google 地图,但错误可能是由 apache 库引起的。