java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory 错误解决方案
环境: Android Studio 4.1.2
记录一次简单的出包过程遇到的问题。接入小米的sdk的时候,遇到crash,查看日志发现
--------- beginning of crash 2021-01-29 15:18:06.435 28062-29203/? E/AndroidRuntime: FATAL EXCEPTION: Thread-18 Process: cn.xxxxxx.xxx.mi, PID: 28062 java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory; at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:155) at com.xxxxxx.sdk.tools.HttpTools.getNewHttpClient(HttpTools.java:1918) at com.xxxxxx.sdk.tools.HttpTools.access$100(HttpTools.java:154) at com.xxxxxx.sdk.tools.HttpTools$11.run(HttpTools.java:1651) at java.lang.Thread.run(Thread.java:929) Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/cn.xxxxxx.xxx.mi-nkXbCPUYbnY7-dXsVuKWBw==/base.apk"],nativeLibraryDirectories=[/data/app/cn.xxxxxx.xxx.mi-nkXbCPUYbnY7-dXsVuKWBw==/lib/arm, /data/app/cn.xxxxxx.xxx.mi-nkXbCPUYbnY7-dXsVuKWBw==/base.apk!/lib/armeabi, /system/lib, /hw_product/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager.<init>(ThreadSafeClientConnManager.java:155) at com.xxxxxx.sdk.tools.HttpTools.getNewHttpClient(HttpTools.java:1918) at com.xxxxxx.sdk.tools.HttpTools.access$100(HttpTools.java:154) at com.xxxxxx.sdk.tools.HttpTools$11.run(HttpTools.java:1651) at java.lang.Thread.run(Thread.java:929)
我的代码:
private static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
return new DefaultHttpClient(ccm, params);
} catch (Exception e) {
return new DefaultHttpClient();
}
}
看上去好像是没有 Lorg/apache/commons/logging/LogFactory 这个包, 于是我就把它下载下来给导进去
这里下载: http://commons.apache.org/proper/commons-logging/download_logging.cgi
导进去又报了这样一个莫名其妙的错误
Dex file with version '39' cannot be used with min sdk level '29'
通篇没有找到 这个版本号在哪里设置的,这个问题找到一个方案。 我没有尝试
因为我意识到可能是 由于导入的这个包引起的。
我把它重新删掉,不报这个错了!但是还是会引发上面的 crash。 所以我觉得是方向错了。
后面找到这个解决方案 解决了我的问题 androidmanifest.xml 文件中添加以下库的引用
<uses-library android:name ="org.apache.http.legacy" android:required ="false"/>
或者
build.gradle中添加下面的代码 android { useLibrary 'org.apache.http.legacy' }
那么问题来了,为什么会报这个问题呢?
org.apache.http.legacy.jar 是如何解决这个问题的?