升級(jí)總是伴隨著痛苦,總是因?yàn)橐恍╊惖淖儎?dòng),查無(wú)此類、此方法、此屬性,讓人真是頭大。不經(jīng)一番徹骨寒,哪有梅花撲鼻香?下面是我升級(jí)中遇到的一些問(wèn)題,我在這里做個(gè)簡(jiǎn)單的記錄。如果不巧你也正在因?yàn)榇耸路敢苫?,那?qǐng)看看以下內(nèi)容是否能夠幫到你?
升級(jí)中的變更說(shuō)明如下:
-
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)錯(cuò)誤: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版本匹配,不然會(huì)報(bào)錯(cuò) 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") //如果是導(dǎo)入第三方,使用apply(plugin="plugin名稱")- 附言:
如果項(xiàng)目中引入了外部的 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并存,因?yàn)橛行┑谌降倪€未切換到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 { // 簽名的導(dǎo)入 signingConfigs { // 因?yàn)閐ev不存在,因此使用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")) } // 如果有默認(rèn)簽名 signingConfig = signingConfigs.getByName("dev") } buildFeatures { viewBinding = true buildConfig = true // 增加該屬性,項(xiàng)目才能生成BuildConfig類 } sourceSets { // 又創(chuàng)建了一個(gè)渠道: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")) //原來(lái)寫(xiě)法是:flavorDimensions = ["a", "b"] productFlavors { // 創(chuàng)建一個(gè)“風(fē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")) //引入其他模塊 } }-
其他需要注意的事項(xiàng)
因?yàn)轫?xiàng)目使用的Gradle8.0.0+,所以R8模式是默認(rèn)全開(kāi)的,會(huì)導(dǎo)致一些類在經(jīng)過(guò)混淆后出現(xiàn)問(wèn)題,特別典型的是:Gson庫(kù),請(qǐng)看:ISSUE。通過(guò)嘗試,需要設(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針對(duì)協(xié)程相關(guān)的混淆:
# -keepattributes Signature -keep class kotlin.coroutines.Continuation** 如果在升級(jí)kotlin-dsl出現(xiàn)Parcelable報(bào)錯(cuò)等問(wèn)題,試試讀取使用方法BundleCompat.getParcelable*(args) **
-
- 一步一個(gè)腳印,總算是解決了升級(jí)的問(wèn)題。如果覺(jué)得有幫助,麻煩動(dòng)動(dòng)您發(fā)財(cái)?shù)男∈纸o點(diǎn)個(gè)贊!原創(chuàng)內(nèi)容,轉(zhuǎn)載請(qǐng)注明作者和出處,謝謝!
對(duì)了,這里在增加一個(gè)WARING! 雖然上面的內(nèi)容都正常運(yùn)轉(zhuǎn)了,我還是遇到了一個(gè)新問(wèn)題,就是打包時(shí),如何用kts給APK重命名,使其打包出來(lái)就是我們想要的名字,因?yàn)樵瓉?lái)的 output.setOutputFileName 已經(jīng)無(wú)法使用,暫未找到解決方案,正在探索中。。。