当前,我无法解决java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Instant;
崩溃问题,这种崩溃发生在Android 7.1.1(Nougat)及其下方。但我的Oreo模拟器运行Instant.parse(...)
就好了。
堆栈跟踪:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Instant;
at (application class)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.Instant" on path: DexPathList[[zip file "/data/app/(app-domain)/base.apk", zip file "/data/app/(app-domain)/split_lib_dependencies_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_0_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_1_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_2_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_3_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_4_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_5_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_6_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_7_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_8_apk.apk", zip file "/data/app/(app-domain)/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/(app-domain)/lib/arm64, /data/app/(app-domain)/base.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_dependencies_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_0_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_1_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_2_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_3_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_4_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_5_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_6_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_7_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_8_apk.apk!/lib/arm64-v8a, /data/app/(app-domain)/split_lib_slice_9_apk.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]
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 (application class)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
违规代码:
Date parseDate = Date.from(Instant.parse(model.getDate() + "Z"));
其中model
是日期String
,格式为"yyyy-mm-ddTkk:mm:ss"
我的JDK版本现在为1.8。我知道我应该在我的build.gradle
:
android {
compileSdkVersion 26
defaultConfig {
...
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "0.2-2018-04-16"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "com.jakewharton.threetenabp:threetenabp:1.0.5"
}
根据我在此处阅读的内容,显然Instant.parse(...)
并未包含在支持的API中:https://developer.android.com/studio/write/java8-support.html#supported_features
我能错过什么?我不妨使用" ailing Date
and Calendar
APIs in both Java and Android" ,但这将是最后的选择。我想尽可能坚持JDK 1.8所提供的。
此外,我不习惯使用Joda Time,因为它对于我正在开发的这么小的应用来说太大了,而且在这种情况下我只需要几个功能。
任何意见都会受到赞赏!
Nota bene :合同明确要求我使用Java而不是Kotlin。
答案 0 :(得分:5)
如果您打开sdk文档https://developer.android.com/reference/java/time/Instant.html,则会在右上角看到"在API级别26" 中添加,这意味着该类在旧版Android上不可用设备
您的清单包含 minSdkVersion 19 。这意味着我保证我的应用程序将在api-19和更新版本上运行。例外情况告诉你承诺被破坏了。
如何解决:
Instant
来保持与android-api-19的兼容性。您可以尝试使用SimpleDateFormat将日期从字符串格式解析为日期格式。