使用 okhttp-3.12.0 没有发现 class "kotlin.text.Charsets" 错误 Android

Didn't find class "kotlin.text.Charsets" error using okhttp-3.12.0 Android

代码没有问题所以我只是复制错误。我正在使用 okhttp 3.12.0 从互联网上获取一些东西(在 Android 编程中)

下一行可能是主要问题:

java.lang.ClassNotFoundException: Didn't find class "kotlin.text.Charsets"

AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                         Process: com.example.infitualer.ipdemo, PID: 3306
                                                                         java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                             at android.os.AsyncTask.done(AsyncTask.java:325)
                                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                             at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                             at java.lang.Thread.run(Thread.java:761)
                                                                          Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lkotlin/text/Charsets;
                                                                             at okio.Buffer.<clinit>(Buffer.kt:2212)
                                                                             at okhttp3.ResponseBody.create(ResponseBody.java:210)
                                                                             at okhttp3.internal.Util.<clinit>(Util.java:62)
                                                                             at okhttp3.internal.Util.immutableList(Util.java:0)
                                                                             at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:127)
                                                                             at com.example.infitualer.ipdemo.MainActivity$IpTask.doInBackground(MainActivity.java:34)
                                                                             at com.example.infitualer.ipdemo.MainActivity$IpTask.doInBackground(MainActivity.java:25)
                                                                             at android.os.AsyncTask.call(AsyncTask.java:305)
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                             at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243) 
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                             at java.lang.Thread.run(Thread.java:761) 
                                                                          Caused by: java.lang.ClassNotFoundException: Didn't find class "kotlin.text.Charsets" on path: DexPathList[[zip file "/data/app/com.example.infitualer.ipdemo-1/base.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.infitualer.ipdemo-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.infitualer.ipdemo-1/lib/x86, /system/lib, /vendor/lib]]
                                                                             at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                             at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                             at okio.Buffer.<clinit>(Buffer.kt:2212) 
                                                                             at okhttp3.ResponseBody.create(ResponseBody.java:210) 
                                                                             at okhttp3.internal.Util.<clinit>(Util.java:62) 
                                                                             at okhttp3.internal.Util.immutableList(Util.java:0) 
                                                                             at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:127) 
                                                                             at com.example.infitualer.ipdemo.MainActivity$IpTask.doInBackground(MainActivity.java:34) 
                                                                             at com.example.infitualer.ipdemo.MainActivity$IpTask.doInBackground(MainActivity.java:25) 
                                                                             at android.os.AsyncTask.call(AsyncTask.java:305) 
                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                             at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:243) 
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                             at java.lang.Thread.run(Thread.java:761) 

您必须为 Okio 使用 Kotlin stdlib 依赖项。 要么使用 OkHttp 所依赖的 Okio 1.1.15,要么包括 Okio 2.0 的依赖项 Kotlin stdlib。

从MvnRepository下载Okio 1.1.15.jar并添加到lib文件夹,然后配置依赖

implementation files('libs/okio-1_15_0.jar')