[开发笔记] [Android][Update Kotlin Version] [Update SDK Version] 所遇到 Build 的 Error 与 Fix 步骤
前言
每个 Android Developer 都会遇到要升级 Android SDK、Kotlin Language Version、Library 的时候,
而当升级完毕尝试 Build 一下
没事就安然度过一天 有事就冷静处理等升天,
当你认为某个 Error 是这个的关系,尝试更新 Library Version 后又出现其他 Error,这算还好!
当处理一系列的 Error 后,殊不知又回到一开始的 Error 时,这时脑中就开始想起胡夏 又回到最初的起点~,
光这次升级,胡夏就在我脑袋中唱了3、4次那些年,我带同事作一次又再唱好几次 ....
再看下去之前先来回顾一下 胡夏的那些年吧 (Yt)!
需求
Update Stripe Library Version
16.0.0 --> 20.8.0
我做过一些 research 如果要升级到20.5.0 - 20.8.0 最基本要同时升级的问题:
Android SDK 31
GradleVersion 5.4.1
KotlinVersion 1.6.0
coding 上修改语法配合API 31
build.gradle新增插件
implementation ‘com.google.dagger:dagger:2.27’
annotationProcessor “com.google.dagger:dagger-compiler:2.27”
升级build.gradle插件:
implementation ‘com.stripe:stripe-android:20.5.0’
Step
更新 Stripe
Maven
Build Exception : The minCompileSdk (31) specified in a dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties) is greater than this module's compileSdkVersion (android-30).
The minCompileSdk (31) specified in a dependency's AAR metadata (META-INF/com/android/build/gradle/aar-metadata.properties) is greater than this module's compileSdkVersion (android-30). Dependency: androidx.appcompat:appcompat:1.4.1. AAR metadata file: /Users/username/.gradle/caches/transforms-2/files-2.1/5c1f32c78a72902c36b66aa50e7efafb/appcompat-1.4.1/META-INF/com/android/build/gradle/aar-metadata.properties.
更新 SdkVersion
compileSdkVersion = 31 minSdkVersion = 23 targetSdkVersion = 31
Build Exception : Caused by: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed
Caused by: com.android.builder.internal.aapt.v2.Aapt2Exception: Android resource linking failed /Users/username/app-name/app/src/main/res/layout/fragment_address_add.xml:107: AAPT: error: resource drawable/ic_arrow (aka com.packageName.appId.uat:drawable/ic_arrow) not found. /Users/username/app-name/app/src/main/res/layout/fragment_address_add.xml:125: AAPT: error: resource drawable/ic_arrow (aka com.packageName.appId.uat:drawable/ic_arrow) not found. /Users/username/app-name/app/src/main/res/layout/fragment_cart_delivery.xml:170: AAPT: error: resource drawable/ic_arrow (aka com.packageName.appId.uat:drawable/ic_arrow) not found. /Users/username/app-name/app/src/main/res/layout/fragment_cart_delivery.xml:187: AAPT: error: resource drawable/ic_arrow (aka com.packageName.appId.uat:drawable/ic_arrow) not found. /Users/username/app-name/app/src/main/res/layout/fragment_cart_delivery.xml:251: AAPT: error: resource drawable/ic_arrow (aka com.packageName.appId.uat:drawable/ic_arrow) not found.
更改 drawableId
ic_arrow --> ic_arrow_down
Build Exception
w: Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
类路径中的运行时 JAR 文件应该具有相同的版本。 这些文件在类路径中找到:
/Users/username/.gradle/caches/transforms-2/files-2.1/ed1791edcdff7f9300e8883e6f10ce2c/jetified-kotlin-stdlib-jdk8-1.6.10.jar (version 1.6) /Users/username/.gradle/caches/transforms-2/files-2.1/d943c03d26b2a6908c45cb49e800fc6d/jetified-kotlin-stdlib-jdk7-1.6.10.jar (version 1.6) /Users/username/.gradle/caches/transforms-2/files-2.1/581b1a3ffda168a8ea92dfef7cdb0d9b/jetified-kotlin-reflect-1.4.0.jar (version 1.4) /Users/username/.gradle/caches/transforms-2/files-2.1/755fb8ca5be53840e3926c4769461a40/jetified-kotlin-stdlib-1.6.21.jar (version 1.6) /Users/username/.gradle/caches/transforms-2/files-2.1/b9f335f68f3f6214cb85ac89109d4285/jetified-kotlin-stdlib-common-1.6.21.jar (version 1.6)
/jetified-kotlin-stdlib-jdk8-1.6.10.jar (version 1.6) /jetified-kotlin-stdlib-jdk7-1.6.10.jar (version 1.6) /jetified-kotlin-reflect-1.4.0.jar (version 1.4) /jetified-kotlin-stdlib-1.6.21.jar (version 1.6) /jetified-kotlin-stdlib-common-1.6.21.jar (version 1.6)
w: Some runtime JAR files in the classpath have an incompatible version. Consider removing them from the classpath
类路径中的某些运行时 JAR 文件的版本不兼容。 考虑将它们从类路径中删除
/jetified-paymentsheet-20.5.0-api.jar!/META-INF/paymentsheet_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-payments-core-20.5.0-api.jar!/META-INF/payments-core_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-stripe-core-20.5.0-api.jar!/META-INF/stripe-core_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlin-stdlib-1.6.21.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-payments-model-20.5.0-api.jar!/META-INF/payments-model_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /navigation-runtime-2.4.2-api.jar!/META-INF/navigation-runtime_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlinx-coroutines-core-jvm-1.6.1.jar!/META-INF/kotlinx-coroutines-core.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlin-stdlib-common-1.6.21.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlinx-coroutines-android-1.6.1.jar!/META-INF/kotlinx-coroutines-android.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /navigation-common-2.4.2-api.jar!/META-INF/navigation-common_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlin-stdlib-jdk7-1.6.10.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlin-android-extensions-runtime-1.6.10.jar!/META-INF/kotlin-android-extensions-runtime.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1. /jetified-kotlin-stdlib-jdk8-1.6.10.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.1.
更新 Kotlin Version
The binary version of its metadata is 1.6.0, expected version is 1.4.1.
尝试将 Kotlin 版本更新至 1.6 版本
而使用 Depedencies 有使用 kotlin version 的插件为
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
Maven
ext.kotlin_version = '1.4.21'
尝试改为
ext.kotlin_version = '1.6.21'
Build Exception : A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> Task :app:kaptUatDebugKotlin /Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:7: error: cannot find symbol @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) ^ symbol: class AssistedInject_AssistedInjectModule Note: [1] Wrote GeneratedAppGlideModule with: [com.bumptech.glide.integration.okhttp3.OkHttpLibraryGlideModule] > Task :app:kaptUatDebugKotlin FAILED Execution failed for task ':app:kaptUatDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
AssistedInjectModule
package com.packageName.appId.di; import java.lang.System; @com.squareup.inject.assisted.dagger2.AssistedModule() @kotlin.Metadata(mv = {1, 6, 0}, k = 1, d1 = {"u0000fnu0002u0018u0002nu0002u0010u0000nu0002bu0002b'u0018u00002u00020u0001Bu0005u00a2u0006u0002u0010u0002u00a8u0006u0003"}, d2 = {"Lcom/packageName/appId/di/AssistedInjectModule;", "", "()V", "app_uatDebug"}) @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) public abstract class AssistedInjectModule { public AssistedInjectModule() { super(); } }
AssistedInjectModule 跟 com.squareup.inject.assisted.dagger2.AssistedModule 有关系
// Dependency Injection compileOnly "com.squareup.inject:assisted-inject-annotations-dagger2:$rootProject.assistedInjectVersion" kapt "com.squareup.inject:assisted-inject-processor-dagger2:$rootProject.assistedInjectVersion"
更新 Assisted Inject Dagger 2
assistedInjectVersion = '0.5.2'
更新为
assistedInjectVersion = '0.7.0'
Maven
Rebuild
> Task :app:kaptUatDebugKotlin FAILED /Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:7: error: cannot find symbol @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) ^ symbol: class AssistedInject_AssistedInjectModule Note: [1] Wrote GeneratedAppGlideModule with: [com.bumptech.glide.integration.okhttp3.OkHttpLibraryGlideModule] Execution failed for task ':app:kaptUatDebugKotlin'. > A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction > java.lang.reflect.InvocationTargetException (no error message) * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
恢复 Assisted Inject Dagger 2
assistedInjectVersion = '0.7.0'
恢复为
assistedInjectVersion = '0.5.2'
comment
// implementation('com.github.bumptech.glide:glide:$rootProject.glideVersion') { // exclude group: "com.android.support" // }
更新 Glide
跟这个没关系
glideVersion = '4.11.0'
更新为
glideVersion = '4.12.0'
Rebulid
没有了
Note: [1] Wrote GeneratedAppGlideModule with: [com.bumptech.glide.integration.okhttp3.OkHttpLibraryGlideModule]
但还是报错
/Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:7: error: cannot find symbol @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) ^ symbol: class AssistedInject_AssistedInjectModule FAILURE: Build completed with 3 failures.
Build Exception : Caused by: java.lang.IllegalStateException: Could not parse metadata! This should only happen if you're using Kotlin <1.1.
Caused by: java.lang.IllegalStateException: Could not parse metadata! This should only happen if you're using Kotlin <1.1. at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.readKotlinClassMetadata(KotlinPoetMetadata.kt:83) at com.squareup.moshi.kotlinpoet.metadata.KotlinPoetMetadata.toImmutableKmClass(KotlinPoetMetadata.kt:113) at com.squareup.moshi.kotlin.codegen.MoshiCachedClassInspector.toImmutableKmClass(MoshiCachedClassInspector.kt:22) Caused by: java.lang.IllegalStateException: Could not parse metadata! This should only happen if you're using Kotlin <1.1. at com.squareup.moshi.kotlin.codegen.MetadataKt.targetType(metadata.kt:126) at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.adapterGenerator(JsonClassCodegenProcessor.kt:146) at com.squareup.moshi.kotlin.codegen.JsonClassCodegenProcessor.process(JsonClassCodegenProcessor.kt:115) at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt:90) at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:188) at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980) ... 38 more
有可能是 Dagger 的问题
This is due to Dagger's use of older version of kotlinx-metadata-jvm. See https://youtrack.jetbrains.com/issue/KT-45885 Update your dagger to 2.34
更新 Moshi Version
目前为 MoshiVersion = 1.10.0
Maven
MoshiVersion 更新为 1.3.0
Build Exception : ailed to transform moshi-1.13.0.jar to match attributes
Execution failed for task ':app:checkDevDebugDuplicateClasses'. > Could not resolve all files for configuration ':app:devDebugRuntimeClasspath'. > Failed to transform moshi-1.13.0.jar (com.squareup.moshi:moshi:1.13.0) to match attributes {artifactType=enumerated-runtime-classes, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.jvm.environment=standard-jvm, org.gradle.jvm.version=8, org.gradle.libraryelements=jar, org.gradle.status=release, org.gradle.usage=java-runtime, org.jetbrains.kotlin.platform.type=jvm}. > Execution failed for JetifyTransform: /Users/username/.gradle/caches/modules-2/files-2.1/com.squareup.moshi/moshi/1.13.0/da685586facab9eb5c4fb630ce248be14e7da21b/moshi-1.13.0.jar. > Failed to transform '/Users/username/.gradle/caches/modules-2/files-2.1/com.squareup.moshi/moshi/1.13.0/da685586facab9eb5c4fb630ce248be14e7da21b/moshi-1.13.0.jar' using Jetifier. Reason: IllegalArgumentException, message: Unsupported class file major version 60. (Run with --stacktrace for more details.) Suggestions: - Check out existing issues at https://issuetracker.google.com/issues?q=componentid:460323&s=modified_time:desc, it's possible that this issue has already been filed there. - If this issue has not been filed, please report it at https://issuetracker.google.com/issues/new?component=460323 (run with --stacktrace and provide a stack trace if possible).
app:checkDevDebugDuplicateClasses
更新 Gradle 版本
Maven
AndroidLintAnalysisTask IllegalArgumentException warning because unsupported class files in META-INF
Build Exception : Manifest merger failed with multiple errors, see logs
Execution failed for task ':app:processDevDebugMainManifest'. > Manifest merger failed with multiple errors, see logs
Merging Errors: Warning uses-permission#com.google.android.gms.permission.AD_ID was tagged at AndroidManifest.xml:17 to remove other declarations but no other declaration present android.app main manifest (this file), line 16 Error: android:exported needs to be explicitly specified for element <activity#com.onesignal.NotificationOpenedActivityHMS>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 77 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.GcmBroadcastReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 56 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.BootUpReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 108 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.UpgradeReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 114
新增 android:exported 在 Manifests 的 activity 中
Error: android:exported needs to be explicitly specified for element <activity#com.onesignal.NotificationOpenedActivityHMS>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 77 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.GcmBroadcastReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 56 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.BootUpReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 108 Error: android:exported needs to be explicitly specified for element <receiver#com.onesignal.UpgradeReceiver>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details. android.app main manifest (this file), line 114
更新 oneSignal
Android 版本
oneSignal 版本
更新为 4.6.7
Build Exception : Caused by: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0
/Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:7: error: cannot find symbol @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) ^ symbol: class AssistedInject_AssistedInjectModule Note: [1] Wrote GeneratedAppGlideModule with: [com.bumptech.glide.integration.okhttp3.OkHttpLibraryGlideModule] FAILURE: Build completed with 3 failures.
Caused by: java.lang.IllegalStateException: Unsupported metadata version. Check that your Kotlin version is >= 1.0 at com.google.common.collect.SingletonImmutableBiMap.forEach(SingletonImmutableBiMap.java:66) at dagger.internal.codegen.validation.TypeCheckingProcessingStep.process(TypeCheckingProcessingStep.java:48) at dagger.internal.codegen.ModuleProcessingStep.process(ModuleProcessingStep.java:104) at dagger.internal.codegen.ModuleProcessingStep.process(ModuleProcessingStep.java:59) at dagger.internal.codegen.statistics.DaggerStatisticsCollectingProcessingStep.process(DaggerStatisticsCollectingProcessingStep.java:52) at dagger.shaded.auto.common.BasicAnnotationProcessor$ProcessingStepAsStep.process(BasicAnnotationProcessor.java:495) at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:228) at dagger.shaded.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:208) at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt:90) at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:188) at jdk.compiler/com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:980) ... 41 more
更新 Dagger
dependencies { ... classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.0" classpath "com.google.dagger:hilt-android-gradle-plugin:2.42" }
com.google.dagger Maven
更新为 2.42
更新完 Build
Build Exception : ModuleProcessingStep was unable to process 'com.packageName.appId.viewmodels.MainViewModel' because '' could not be resolved.
> Task :app:kaptUatDebugKotlin /Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/viewmodels/MainViewModel.java:5: error: cannot find symbol @dagger.hilt.InstallIn(value = {ApplicationComponent.class}) ^ symbol: class ApplicationComponent /Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:7: error: cannot find symbol @dagger.Module(includes = {AssistedInject_AssistedInjectModule.class}) ^ symbol: class AssistedInject_AssistedInjectModule error: [Hilt] @InstallIn, 'value' class is invalid or missing: @dagger.hilt.InstallIn({<error>}) [Hilt] Processing did not complete. See error above for details. Note: [1] Wrote GeneratedAppGlideModule with: [com.bumptech.glide.integration.okhttp3.OkHttpLibraryGlideModule] [Hilt] /Users/username/app-name/app/build/tmp/kapt3/stubs/uatDebug/com/packageName/appId/di/AssistedInjectModule.java:8: error: @AssistedModule's @Module must include AssistedInject_AssistedInjectModule public abstract class AssistedInjectModule { ^ error: ModuleProcessingStep was unable to process 'com.packageName.appId.viewmodels.MainViewModel' because '<error>' could not be resolved. Dependency trace: => element (CLASS): com.packageName.appId.viewmodels.MainViewModel => annotation: @dagger.hilt.InstallIn("<error>") ModuleProcessingStep was unable to process 'com.packageName.appId.viewmodels.MainViewModel' because '<error>' could not be resolved. => annotation method: java.lang.Class<?>[] value() => annotation value (ARRAY): value '<error>' with expected type java.lang.Class<?>[] => annotation value (STRING): value '<error>' with expected type java.lang.Class<?> If type '<error>' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type '<error>' is on your classpath. error: ModuleProcessingStep was unable to process 'com.packageName.appId.di.AssistedInjectModule' because '<error>' could not be resolved. ModuleProcessingStep was unable to process 'com.packageName.appId.di.AssistedInjectModule' because '<error>' could not be resolved. Dependency trace: => element (CLASS): com.packageName.appId.di.AssistedInjectModule => annotation: @dagger.Module(includes = "<error>") => annotation method: java.lang.Class<?>[] includes() => annotation value (ARRAY): value '<error>' with expected type java.lang.Class<?>[] => annotation value (STRING): value '<error>' with expected type java.lang.Class<?> If type '<error>' is a generated type, check above for compilation errors that may have prevented the type from being generated. Otherwise, ensure that type '<error>' is on your classpath.
更新 androidx.hilt
就没有然后了