java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory

java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory

类似的问题已经有人问过了。但是由于 Android 平台兼容性的变化,这个似乎比以前的更复杂。

这是我注册了 Android Beta 计划

的 Pixel 和 Pixel2 的错误日志
08-16 13:20:53.146 9630-9630/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: me.project.android.dev, PID: 9630
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;
        at com.amazonaws.util.VersionInfoUtils.<clinit>(VersionInfoUtils.java:41)
        at com.amazonaws.util.VersionInfoUtils.c(VersionInfoUtils.java:77)
        at com.amazonaws.ClientConfiguration.<clinit>(ClientConfiguration.java:43)
        //project specific class reference removed
        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)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/me.project.android.dev-0SPRJnc8-4voauRU7Y20zQ==/base.apk"],nativeLibraryDirectories=[/data/app/me.project.android.dev-0SPRJnc8-4voauRU7Y20zQ==/lib/arm64, /data/app/me.project.android.dev-0SPRJnc8-4voauRU7Y20zQ==/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
        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 com.amazonaws.util.VersionInfoUtils.<clinit>(VersionInfoUtils.java:41) 
        at com.amazonaws.util.VersionInfoUtils.c(VersionInfoUtils.java:77) 
        at com.amazonaws.ClientConfiguration.<clinit>(ClientConfiguration.java:43) 
        //project specific class reference removed
        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) 

在 运行 设备 运行 Android 7.0 及以下版本上使用相同的代码,它工作得很好。

我也试过为我的项目添加依赖

implementation "commons-logging:commons-logging:1.2"

添加此依赖项可使应用程序在 Pixel 和 Pixel 中运行,但随后会在所有其他设备中崩溃并出现异常

org.apache.commons.logging.impl.LogFactoryImpl does not extend or implement org.apache.commons.logging.LogFactory

我已经尝试在 ProGuard 中进行所有更改。这是我的混淆器配置

-keep class org.apache.commons.logging.impl.LogFactoryImpl
-keep class org.apache.commons.logging.LogFactory
-keepnames class org.apache.commons.logging.impl.* {*;}
-keepnames class org.apache.commons.logging.*
-keepclassmembers class org.apache.commons.logging.impl.* {*;}
-keepclassmembers class org.apache.commons.logging.*
-keepnames interface org.apache.commons.logging.impl.* {*;}
-keepnames interface org.apache.commons.logging.*

仍然导致崩溃。

此问题与亚马逊 AWS SDK 有关 - https://github.com/aws/aws-sdk-android/issues/476

在 AWS 更新其 SDK 以解决此问题之前是否有任何解决方法?

另一种解决方法是将 commons-logging 库作为 .jar 文件添加到您的 libs/ 文件夹中,而不是使用 implementation。确保 implementation fileTree(include: ['*.jar'], dir: 'libs') 在您的依赖项中。

commons-logging-1.2.jar 的来源:

  1. Maven Repository
  2. Apache Commons Logging

来源: Dale Lim - https://github.com/aws/aws-sdk-android/issues/265#issuecomment-411978696

这是一个似乎已在 version 2.6.30 of the SDK 中解决的 AWS SDK 错误:

Fixed a bug where getting a logger using Apache Commons Logging would crash starting Android 9.0 (Pie / API Level 28). See pull #521. Now, Apache Commons Logging would be used if it's being added as a dependency, otherwise android.util.Log will be used.

在清单文件中添加以下行。

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

为了解决这个问题我有依赖 实施"commons-logging:commons-logging-api:1.1" 在 gradle 文件中。

   /*Apache Commons Logging would crash starting Android 9.0 (Pie / API Level 28).*/

   //try this 

    testImplementation 'commons-logging:commons-logging:1.2'

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

     /* if this files  'commons-logging:commons-logging:1.2' fails to download  then
      download this file manual  from http://commons.apache.org/proper/commons- 
       logging/download_logging.cgi and paste it lib folder*/