I'm trying getAccessToken gg credentials in viewmodel and I get error:
Failed resolution of: Ljava/time/Duration;
I searched on gg but not find any useful information. Please help if you know. Thank you very much. Here is my code:
class NotificationViewModel(application: Application) : AndroidViewModel(application) {
val header = MutableLiveData<HashMap<String, String>>()
init {
viewModelScope.launch {
header.postValue(getRemoteMessageHeaders(ClassLoader.getSystemClassLoader()))
}
}
}
@Throws(Exception::class)
fun getAccessToken(classLoader: ClassLoader): String {
val credentials =
GoogleCredentials.fromStream(classLoader.getResourceAsStream("credentials.json"))
.createScoped("https://www.googleapis.com/auth/firebase.messaging")
credentials.refreshIfExpired()
return credentials.accessToken.tokenValue
}
fun getRemoteMessageHeaders(classLoader: ClassLoader): HashMap<String, String> {
val header = HashMap<String, String>()
header[Constants.REMOTE_MSG_AUTHORIZATION] =
"Bearer " + getAccessToken(classLoader)
header[Constants.REMOTE_MSG_CONTENT_TYPE] = "application/json"
return header
}
This is error in logcat I got:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/Duration;
at com.google.auth.oauth2.OAuth2Credentials.<clinit>(OAuth2Credentials.java:70)
at com.trochoinhacai.kubetchat.viewmodels.NotificationViewModelKt.getAccessToken(NotificationViewModel.kt:28)
at com.trochoinhacai.kubetchat.viewmodels.NotificationViewModelKt.getRemoteMessageHeaders(NotificationViewModel.kt:37)
at com.trochoinhacai.kubetchat.viewmodels.NotificationViewModel$1.invokeSuspend(NotificationViewModel.kt:20)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.internal.DispatchedContinuationKt.resumeCancellableWith(DispatchedContinuation.kt:367)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable(Cancellable.kt:30)
at kotlinx.coroutines.intrinsics.CancellableKt.startCoroutineCancellable$default(Cancellable.kt:25)
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.Duration" on path: DexPathList[[zip file "/data/app/com.trochoinhacai.kubetchat-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.google.auth.oauth2.OAuth2Credentials.<clinit>(OAuth2Credentials.java:70)
Suppressed: java.lang.ClassNotFoundException: java.time.Duration
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available