Reputation: 2490
On Android 9 on a OnePlus5, when I try a remote connection using the ion
library, I receive this error:
Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/RequestLine;
at com.koushikdutta.ion.Ion$Config$1.createAsyncHttpRequest + 559(Ion.java:559)
at com.koushikdutta.ion.IonRequestBuilder.prepareRequest + 282(IonRequestBuilder.java:282)
at com.koushikdutta.ion.IonRequestBuilder.execute + 616(IonRequestBuilder.java:616)
at com.koushikdutta.ion.IonRequestBuilder.execute + 606(IonRequestBuilder.java:606)
at com.koushikdutta.ion.IonRequestBuilder.as + 891(IonRequestBuilder.java:891)
at io.dishup.dishup.Network.request + 79(Network.java:79)
at io.dishup.dishup.EndpointCustomer.setNewCustomer + 29(EndpointCustomer.java:29)
at io.dishup.dishup.GoogleSignInToDishup.sendTokensToServer + 129(GoogleSignInToDishup.java:129)
at io.dishup.dishup.GoogleSignInToDishup.access$400 + 29(GoogleSignInToDishup.java:29)
at io.dishup.dishup.GoogleSignInToDishup$5.onComplete + 114(GoogleSignInToDishup.java:114)
at com.google.android.gms.tasks.zzj.run + 4(:4)
at android.os.Handler.handleCallback + 873(Handler.java:873)
at android.os.Handler.dispatchMessage + 99(Handler.java:99)
at android.os.Looper.loop + 193(Looper.java:193)
at android.app.ActivityThread.main + 6898(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 537(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main + 858(ZygoteInit.java:858)
Caused by java.lang.ClassNotFoundException: Didn't find class "org.apache.http.RequestLine" on path: DexPathList[[zip file "/data/app/io.dishup.dishup-OqmfH61IongnKfllcQH-mQ==/base.apk"],nativeLibraryDirectories=[/data/app/io.dishup.dishup-OqmfH61IongnKfllcQH-mQ==/lib/arm64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass + 169(BaseDexClassLoader.java:169)
at java.lang.ClassLoader.loadClass + 379(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass + 312(ClassLoader.java:312)
at com.koushikdutta.ion.Ion$Config$1.createAsyncHttpRequest + 559(Ion.java:559)
at com.koushikdutta.ion.IonRequestBuilder.prepareRequest + 282(IonRequestBuilder.java:282)
at com.koushikdutta.ion.IonRequestBuilder.execute + 616(IonRequestBuilder.java:616)
at com.koushikdutta.ion.IonRequestBuilder.execute + 606(IonRequestBuilder.java:606)
at com.koushikdutta.ion.IonRequestBuilder.as + 891(IonRequestBuilder.java:891)
at io.dishup.dishup.Network.request + 79(Network.java:79)
at io.dishup.dishup.EndpointCustomer.setNewCustomer + 29(EndpointCustomer.java:29)
at io.dishup.dishup.GoogleSignInToDishup.sendTokensToServer + 129(GoogleSignInToDishup.java:129)
at io.dishup.dishup.GoogleSignInToDishup.access$400 + 29(GoogleSignInToDishup.java:29)
at io.dishup.dishup.GoogleSignInToDishup$5.onComplete + 114(GoogleSignInToDishup.java:114)
at com.google.android.gms.tasks.zzj.run + 4(:4)
at android.os.Handler.handleCallback + 873(Handler.java:873)
at android.os.Handler.dispatchMessage + 99(Handler.java:99)
at android.os.Looper.loop + 193(Looper.java:193)
at android.app.ActivityThread.main + 6898(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run + 537(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main + 858(ZygoteInit.java:858)
I really don't understand what is wrong.
Upvotes: 4
Views: 5005
Reputation: 126523
I see that you are using Apache HTTP client in your code.
Caused by java.lang.ClassNotFoundException: Didn't find class "org.apache.http.RequestLine"
Enabling legacy mode for the use of this library is not recomended, in fact since Android 9, Apache HTTP client library was completely removed from the bootclasspath and is not available to apps by default.
The real solution is by implementing HttpUrlConnection class. If you are using the ion library, you can have two better options that are already using **HttpUrlConnection class.**
The use of Glide and Picasso is similar to the ion library.
Glide
Loading image from resources (/drawable
directory):
Glide.with(context).load(R.drawable.android).into(imageView);
Loading image from url :
Glide.with(context).load(<image Url>).into(imageView);
define this dependency into your build.gradle
file:
dependencies {
...
implementation 'com.github.bumptech.glide:glide:4.9.0'
...
}
Picasso :
Loading image from resources (/drawable
directory):
Picasso.get().load(R.drawable.android).into(imageView);
Loading image from url :
Picasso.get().load(<image Url>).into(imageView);
you need to define this dependency into your build.gradle
file:
dependencies {
...
implementation 'com.squareup.picasso:picasso:2.71828
...
}
with both options you will get the same result:
Upvotes: 2
Reputation: 8191
try adding this line to your build.gradle:
android {
useLibrary 'org.apache.http.legacy' //(this worked for us when we had a similar issue with apache)
alternatively , try this in your manifest :
<uses-library android:name="org.apache.http.legacy" android:required="false" />
the reason for this is due to the fact that, from android 9, that library is removed from the bootclasspath and is not available to apps by default, so it has to be added.
see : https://developer.android.com/about/versions/pie/android-9.0-changes-28#apache-p
Upvotes: 3
Reputation: 1320
As you can see the exception is ClassNotFoundException
. This exception occurs when you want to create an object of a class or access fields of a class that no exist. but wait, if the class does not exist then my code will be not compiled. yes, you right so the class exists but something deletes that class. something like Progaurd
.
So you must add some progaurd rules to keep that class. Add these code into your proguard-rules.pro
file:
-dontwarn org.apache.commons.**
-keep class org.apache.http.** { *; }
-dontwarn org.apache.http.**
Also instead of using maven repository, download jar files and put them into `/app/libs' folder then write following line in your build.gradle file.
compile files('libs/httpmime-4.3.5.jar')
compile files('libs/httpclient-4.3.5.jar')
compile files('libs/httpclient-cache-4.3.5.jar')
compile files('libs/httpcore-4.3.2.jar')
Upvotes: 0