升級總是伴隨著痛苦云茸,總是因為一些類的變動,查無此類谤饭、此方法标捺、此屬性,讓人真是頭大揉抵。不經(jīng)一番徹骨寒亡容,哪有梅花撲鼻香?下面是我升級中遇到的一些問題冤今,我在這里做個簡單的記錄闺兢。如果不巧你也正在因為此事犯疑惑,那請看看以下內(nèi)容是否能夠幫到你?
升級中的變更說明如下:
-
settings.gradle.kts
@file:Suppress("DEPRECATION", "UnstableApiUsage") pluginManagement { repositories { google() mavenLocal() mavenCentral() gradlePluginPortal() maven("https://jitpack.io") } } dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() //noinspection JcenterRepositoryObsolete jcenter() mavenLocal() mavenCentral() maven("https://jitpack.io") } } rootProject.name = "KtsDemo" include(":app")
-
根目錄 build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile buildscript { val objectboxVersion by extra("3.6.0") //ext { ... } 的替代內(nèi)容 dependencies { classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion") } } // 增加一下配置屋谭,防止出現(xiàn)錯誤:task (current target is 1.8) and 'kaptGenerateStubsUatDebugKotlin' task (current target is 17) allprojects { tasks.withType(KotlinCompile::class.java) { kotlinOptions { jvmTarget = "1.8" } } } // 新式插件引入樣式脚囊,注意ksp的引入需要與kotlin版本匹配,不然會報錯 plugins { id("com.android.application") version "8.0.0" apply false id("com.android.library") version "8.0.0" apply false id("org.jetbrains.kotlin.android") version "1.8.20" apply false id("com.google.devtools.ksp") version "1.8.20-1.0.11" apply false } apply(from = "本地的gradle.kts") //如果是導入第三方桐磁,使用apply(plugin="plugin名稱")
- 附言:
如果項目中引入了外部的 Gradle.kts 文件悔耘,那么 Gradle.kts 中聲明配置可如下:val appRootConfig by extra(mutableMapOf( "namespace" to "com.kts.demo", "appName" to "Demo", "applicationId" to "com.kts.demo" ))
- 附言:
-
子模塊 build.gradle.kts
// 引入Plugin plugins { id("com.android.application") id("org.jetbrains.kotlin.android") id("kotlin-kapt") //kapt可與ksp并存,因為有些第三方的還未切換到ksp id("kotlin-parcelize") id("com.google.devtools.ksp") //引入ksp } // 引入根目錄引入的外部gradle.kts的相關(guān)配置 val appRootConfig by extra(rootProject.extra["appRootConfig"] as Map<String, *>) android { // 簽名的導入 signingConfigs { // 因為dev不存在我擂,因此使用create方法 create("dev") { keyAlias = "Test" keyPassword = "123456" storePassword = "123456" storeFile = file("./docs/sign-test.jks") //定位jsk簽名文件位置 } } namespace = appRootConfig["namespace"] as String //使用全局的配置屬性 compileSdk = 33 buildToolsVersion = "33.0.0" defaultConfig { applicationId = appRootConfig["applicationId"] as String minSdk = 21 targetSdk = 33 versionCode = 1 versionName = "1.0.0" testInstrumentationRunner = "..." //此處省略 // 聲明BuildConfig變量衬以,需配置buildFeatures { buildConfig = true } buildConfigField("String", "APP_ENV_MODE", "\"DEBUG\"") // 如果有cpp相關(guān)的編譯 externalNativeBuild { cmake { cppFlags("") abiFilters.add("armeabi-v7a") //add或者addAll } } // 如果配置有NDK ndk { abiFilters.addAll(listOf("armeabi-v7a", "x86")) } // 如果有默認簽名 signingConfig = signingConfigs.getByName("dev") } buildFeatures { viewBinding = true buildConfig = true // 增加該屬性,項目才能生成BuildConfig類 } sourceSets { // 又創(chuàng)建了一個渠道:google校摩,并引入它的清單文件 create("google") { manifest.srcFile("src/google/AndroidManifest.xml") } } kotlinOptions { jvmTarget = "1.8" } //指定使用Java8編譯 compileOptions { encoding = "UTF-8" sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } packaging { resources { merges.addAll(listOf("**/LICENSE.txt", "**/NOTICE.txt")) excludes.addAll(listOf("/META-INF/{AL2.0,LGPL2.1}", "DebugProbesKt.bin")) } } flavorDimensions.addAll(listOf("prod")) //原來寫法是:flavorDimensions = ["a", "b"] productFlavors { // 創(chuàng)建一個“風味”名稱 create("google") { dimension = "prod" versionCode = 1 versionName = "1.0.0" applicationId = "com.demo.kts" buildConfigField("String", "APP_CHANNEL_NAME", "\"Google\"") } } } buildTypes { // debug { signingConfig signingConfigs.dev } release { isDebuggable = true isJniDebuggable = true isMinifyEnabled = true isShrinkResources = true signingConfig = signingConfigs.getByName("dev") //noinspection ChromeOsAbiSupport ndk { abiFilters.addAll(listOf("armeabi-v7a", "arm64-v8a", "x86_64")) } proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro") } } dependencies { implementation("androidx.core:core-ktx:1.10.1") implementation("androidx.activity:activity-ktx:1.7.2") implementation("androidx.fragment:fragment-ktx:1.6.0") implementation("androidx.appcompat:appcompat:1.6.1") implementation("com.google.android.material:material:1.9.0") implementation("androidx.constraintlayout:constraintlayout:2.1.4") //... 省略許多看峻,許多 api(project(":common")) //引入其他模塊 implementation(project(":test")) //引入其他模塊 } }
-
其他需要注意的事項
因為項目使用的Gradle8.0.0+,所以R8模式是默認全開的衙吩,會導致一些類在經(jīng)過混淆后出現(xiàn)問題互妓,特別典型的是:Gson庫,請看:ISSUE分井。通過嘗試车猬,需要設(shè)置如下規(guī)則:Gson混淆規(guī)則:
##---------------Begin: proguard configuration for Gson ---------- # Gson uses generic type information stored in a class file when working with fields. Proguard # removes such information by default, so configure it to keep all of it. # Gson specific classes # -keep class sun.misc.Unsafe { *; } # Gson uses generic type information stored in a class file when working with # fields. Proguard removes such information by default, keep it. # -keepattributes Signature # This is also needed for R8 in compat mode since multiple # optimizations will remove the generic signature such as class # merging and argument removal. See: # https://r8.googlesource.com/r8/+/refs/heads/main/compatibility-faq.md#troubleshooting-gson-gson -keep class com.google.gson.reflect.TypeToken { *; } -keep class * extends com.google.gson.reflect.TypeToken # Optional. For using GSON @Expose annotation -keepattributes AnnotationDefault,RuntimeVisibleAnnotations -keepclassmembers class * { !transient <fields>; } -if class * -keepclasseswithmembers class <1> { <init>(...); @com.google.gson.annotations.SerializedName <fields>; } ##---------------End: proguard configuration for Gson ----------
Retrofit也需要做如下混淆:
-if interface * { @retrofit2.http.* public *** *(...); } -keep,allowoptimization,allowshrinking,allowobfuscation class <3> # Platform calls Class.forName on types which do not exist on Android to determine platform. -dontnote retrofit2.Platform
針對協(xié)程相關(guān)的混淆:
# -keepattributes Signature -keep class kotlin.coroutines.Continuation
** 如果在升級kotlin-dsl出現(xiàn)Parcelable報錯等問題霉猛,試試讀取使用方法BundleCompat.getParcelable*(args) **
-
- 一步一個腳印尺锚,總算是解決了升級的問題。如果覺得有幫助惜浅,麻煩動動您發(fā)財?shù)男∈纸o點個贊瘫辩!原創(chuàng)內(nèi)容,轉(zhuǎn)載請注明作者和出處坛悉,謝謝伐厌!
對了,這里在增加一個WARING! 雖然上面的內(nèi)容都正常運轉(zhuǎn)了裸影,我還是遇到了一個新問題挣轨,就是打包時,如何用kts給APK重命名轩猩,使其打包出來就是我們想要的名字卷扮,因為原來的 output.setOutputFileName 已經(jīng)無法使用,暫未找到解決方案均践,正在探索中晤锹。。彤委。