From a0aa5e99740a2da80ecbb0489be60310f821667b Mon Sep 17 00:00:00 2001 From: bennyhuo Date: Thu, 20 Jun 2024 07:27:08 +0800 Subject: [PATCH] Upgrade Kotlin to 2.0.0. --- README.md | 3 ++- README_zh.md | 3 ++- build.gradle.kts | 6 ++--- gradle.properties | 4 ++-- gradle/wrapper/gradle-wrapper.properties | 2 +- kace-compiler/build.gradle.kts | 14 ++++++------ .../KaceFirSupertypeGenerationExtension.kt | 3 ++- .../kanyun/kace/compiler/KaceIrTransformer.kt | 4 +--- .../kanyun/kace/compiler/utils/IrCommon.kt | 22 +++++++++++++++++-- kace-compiler/testData/expect.txt | 2 +- kace-sample/build.gradle.kts | 4 ++-- test_agp.sh | 4 ++-- 12 files changed, 45 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index e57db5e..58fd1f1 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,7 @@ The Kace currently supports the above four most commonly used types. Other types | 1.8.20-1.1.0 | 1.8.20 | 4.2.0 | 6.8.3 | | 1.9.0-1.2.0 | 1.9.0~1.9.20 | 4.2.2 | 6.8.3 | | 1.9.20-1.2.0 | 1.9.20 | 4.2.2 | 6.8.3 | +| 2.0.0-1.2.0 | 2.0.0 | 7.1.3 | 7.2 | Since the goal of the Kace is to help developers easily upgrade to Kotlin 1.8, the minimum supported version of Kotlin is relatively high @@ -104,4 +105,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -```` \ No newline at end of file +```` diff --git a/README_zh.md b/README_zh.md index 075ae67..981bc96 100644 --- a/README_zh.md +++ b/README_zh.md @@ -74,6 +74,7 @@ Kace 目前支持了以上四种最常用的类型,其他 kotlin-android-exten | 1.8.20-1.1.0 | 1.8.20 | 4.2.0 | 6.8.3 | | 1.9.0-1.2.0 | 1.9.0~1.9.20 | 4.2.2 | 6.8.3 | | 1.9.20-1.2.0 | 1.9.20 | 4.2.2 | 6.8.3 | +| 2.0.0-1.2.0 | 2.0.0 | 7.1.3 | 7.2 | 由于 Kace 的目标是帮助开发者更方便地迁移到 Kotlin 1.8,因此 Kotlin 最低支持版本比较高 @@ -103,4 +104,4 @@ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -``` \ No newline at end of file +``` diff --git a/build.gradle.kts b/build.gradle.kts index e966823..c053ef8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,8 +1,8 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id("com.android.application") version "4.2.2" apply false - id("com.android.library") version "4.2.2" apply false - id("org.jetbrains.kotlin.android") version "1.9.20" apply false + id("com.android.application") version "7.1.3" apply false + id("com.android.library") version "7.1.3" apply false + id("org.jetbrains.kotlin.android") version "2.0.0" apply false id("com.vanniktech.maven.publish") version "0.18.0" apply false id("com.github.gmazzo.buildconfig") version "2.1.0" apply false } diff --git a/gradle.properties b/gradle.properties index 16ac9b2..a42334b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -23,7 +23,7 @@ kotlin.code.style=official android.nonTransitiveRClass=true KOTLIN_PLUGIN_ID=com.kanyun.kace -VERSION_NAME=1.9.20-1.2.0 +VERSION_NAME=2.0.0-1.2.0-SNAPSHOT GROUP=com.kanyun.kace @@ -44,4 +44,4 @@ POM_DEVELOPER_ID=solar-android POM_DEVELOPER_NAME=SolarAndroid POM_DEVELOPER_URL=https://github.com/beijing-kug/ -SONATYPE_HOST=S01 \ No newline at end of file +SONATYPE_HOST=S01 diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7cd1e9c..a241a53 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Thu Sep 29 08:11:34 CST 2022 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/kace-compiler/build.gradle.kts b/kace-compiler/build.gradle.kts index 8154f75..74f501c 100644 --- a/kace-compiler/build.gradle.kts +++ b/kace-compiler/build.gradle.kts @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { @@ -35,17 +36,16 @@ dependencies { testImplementation(kotlin("test-junit")) testImplementation("org.jetbrains.kotlin:kotlin-compiler-embeddable") - testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:1.9.20-1.3.0") + testImplementation("com.bennyhuo.kotlin:kotlin-compile-testing-extensions:2.0.0-1.3.0") } val compileKotlin: KotlinCompile by tasks -compileKotlin.kotlinOptions.freeCompilerArgs += listOf( - "-opt-in=kotlin.RequiresOptIn", - "-Xcontext-receivers" -) -compileKotlin.kotlinOptions.jvmTarget = "1.8" +compileKotlin.compilerOptions { + freeCompilerArgs.add("-opt-in=kotlin.RequiresOptIn") + jvmTarget.set(JvmTarget.JVM_1_8) +} buildConfig { packageName("$group") buildConfigField("String", "KOTLIN_PLUGIN_ID", "\"${project.property("KOTLIN_PLUGIN_ID")}\"") -} \ No newline at end of file +} diff --git a/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceFirSupertypeGenerationExtension.kt b/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceFirSupertypeGenerationExtension.kt index a621713..f6cc97e 100644 --- a/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceFirSupertypeGenerationExtension.kt +++ b/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceFirSupertypeGenerationExtension.kt @@ -46,9 +46,10 @@ class KaceFirSupertypeGenerationExtension( session: FirSession, ) : FirSupertypeGenerationExtension(session) { - context(TypeResolveServiceContainer) override fun computeAdditionalSupertypes( + override fun computeAdditionalSupertypes( classLikeDeclaration: FirClassLikeDeclaration, resolvedSupertypes: List, + typeResolver: TypeResolveService ): List { var shouldAddSuperType = false OUTER@ for (superTypeRef in resolvedSupertypes) { diff --git a/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceIrTransformer.kt b/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceIrTransformer.kt index d75177f..8423616 100644 --- a/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceIrTransformer.kt +++ b/kace-compiler/src/main/java/com/kanyun/kace/compiler/KaceIrTransformer.kt @@ -42,7 +42,6 @@ import org.jetbrains.kotlin.ir.builders.irGetField import org.jetbrains.kotlin.ir.builders.irReturn import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.types.defaultType -import org.jetbrains.kotlin.ir.types.impl.IrUninitializedType import org.jetbrains.kotlin.ir.types.makeNullable import org.jetbrains.kotlin.ir.util.SYNTHETIC_OFFSET import org.jetbrains.kotlin.ir.util.constructors @@ -78,8 +77,7 @@ class KaceIrTransformer(private val context: IrPluginContext) : IrElementTransfo // override fun findViewByIdCached(owner, id) = ... declaration.addOverride( ANDROID_EXTENSIONS_FQNAME, - FIND_VIEW_BY_ID_CACHED_NAME, - IrUninitializedType, + FIND_VIEW_BY_ID_CACHED_NAME ).apply { val parameterT = addTypeParameter("T", context.typeOfView()) returnType = parameterT.defaultType.makeNullable() diff --git a/kace-compiler/src/main/java/com/kanyun/kace/compiler/utils/IrCommon.kt b/kace-compiler/src/main/java/com/kanyun/kace/compiler/utils/IrCommon.kt index 186fb8c..1845f09 100644 --- a/kace-compiler/src/main/java/com/kanyun/kace/compiler/utils/IrCommon.kt +++ b/kace-compiler/src/main/java/com/kanyun/kace/compiler/utils/IrCommon.kt @@ -17,9 +17,12 @@ package com.kanyun.kace.compiler.utils import org.jetbrains.kotlin.backend.jvm.ir.erasedUpperBound +import org.jetbrains.kotlin.descriptors.DescriptorVisibilities import org.jetbrains.kotlin.descriptors.Modality +import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.builders.declarations.addFunction import org.jetbrains.kotlin.ir.declarations.IrClass +import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.expressions.IrExpression @@ -27,10 +30,13 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.util.allOverridden +import org.jetbrains.kotlin.ir.util.copyTo import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable import org.jetbrains.kotlin.ir.util.functions +import org.jetbrains.kotlin.ir.util.parentAsClass import org.jetbrains.kotlin.ir.util.parentClassOrNull import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.Name fun IrClass.isSubclassOfFqName(fqName: String): Boolean = fqNameWhenAvailable?.asString() == fqName || superTypes.any { @@ -42,9 +48,21 @@ fun IrClass.isSubclassOfFqName(fqName: String): Boolean = fun IrClass.addOverride( baseFqName: FqName, name: String, - returnType: IrType, modality: Modality = Modality.FINAL, -): IrSimpleFunction = addFunction(name, returnType, modality).apply { +): IrSimpleFunction = addFunction { + this.name = Name.identifier(name) + this.modality = modality + this.startOffset = UNDEFINED_OFFSET + this.endOffset = UNDEFINED_OFFSET + this.visibility = DescriptorVisibilities.PUBLIC + this.isFakeOverride = false + this.isSuspend = false + this.isInline = false + this.origin = IrDeclarationOrigin.DEFINED +}.apply { + val thisReceiver = parentAsClass.thisReceiver!! + dispatchReceiverParameter = thisReceiver.copyTo(this, type = thisReceiver.type) + overriddenSymbols = superTypes.mapNotNull { superType -> superType.classOrNull?.owner?.takeIf { superClass -> superClass.isSubclassOfFqName( diff --git a/kace-compiler/testData/expect.txt b/kace-compiler/testData/expect.txt index 354a38c..0fb81f2 100644 --- a/kace-compiler/testData/expect.txt +++ b/kace-compiler/testData/expect.txt @@ -17,10 +17,10 @@ class SixthActivity 7 // FILE: Main.kt.ir class MainActivity : AndroidExtensions { + private val $$delegate_0: AndroidExtensionsImpl = AndroidExtensionsImpl() override fun findViewByIdCached(owner: AndroidExtensionsBase, id: Int, viewClass: Class): T? { return .$$delegate_0.findViewByIdCached(owner, id, viewClass) } - private val $$delegate_0: AndroidExtensionsImpl = AndroidExtensionsImpl() } class SecondActivity : AndroidExtensions { private var $$androidExtensionsImpl: AndroidExtensionsImpl = AndroidExtensionsImpl() diff --git a/kace-sample/build.gradle.kts b/kace-sample/build.gradle.kts index 9d9358a..6a6cacf 100644 --- a/kace-sample/build.gradle.kts +++ b/kace-sample/build.gradle.kts @@ -2,7 +2,7 @@ plugins { id("com.android.application") apply false id("com.android.library") apply false - id("org.jetbrains.kotlin.android") version "1.9.20" apply false + id("org.jetbrains.kotlin.android") version "2.0.0" apply false id("com.kanyun.kace") version "0.0.0-SNAPSHOT" apply false } @@ -19,4 +19,4 @@ allprojects { google() mavenCentral() } -} \ No newline at end of file +} diff --git a/test_agp.sh b/test_agp.sh index 2480378..1f3c3ab 100755 --- a/test_agp.sh +++ b/test_agp.sh @@ -16,8 +16,8 @@ function testUnderAGPVersion() { cd kace-sample -setGradleVersion 6.8.3 -testUnderAGPVersion 4.2.2 +setGradleVersion 7.2 +testUnderAGPVersion 7.1.3 setGradleVersion 7.3.3 testUnderAGPVersion 7.2.0