我想在我的 Android 项目中使用 Kotlin 扩展,但据我所知,它是已弃用的功能。我尝试在我的 Android 项目中使用 1.7.0 Kotlin 版本。除了降级这个版本之外,我还将AGP版本更改为7.4.1。我需要什么额外配置?这是我的项目文件:
build.gradle.kts(根):
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
}
build.gradle.kts(应用程序):
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.kotlin.compose)
}
android {
namespace = "com.example.selfpromoapp"
compileSdk = 34
defaultConfig {
applicationId = "com.example.selfpromoapp"
minSdk = 21
targetSdk = 34
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}
dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.ui)
implementation(libs.androidx.ui.graphics)
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.material)
testImplementation(libs.junit)
implementation(libs.androidx.appcompat)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)
}
libs.versions.toml:
[versions]
agp = "7.4.1"
appcompat = "1.7.0"
kotlin = "1.7.0"
coreKtx = "1.13.1"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
lifecycleRuntimeKtx = "2.8.6"
activityCompose = "1.9.3"
composeBom = "2024.04.01"
material = "1.12.0"
[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" }
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
material = { module = "com.google.android.material:material", version.ref = "material" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlin-compose = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
我不知道你所说的“Kotlin Extensions”是什么意思(也许是来自 Synthetics 的 Kotlin Android Extensions,它是视图绑定的前身,也是 Compose 的前身?)或者为什么它的弃用会导致你恢复到古老的 Kotlin 版本(1.7.0 于 2022 年中期发布),但如果您坚持这样做,也可以。
最重要的部分是将 Kotlin 2.0.0 中所做的更改恢复到 Compose 编译器的使用方式:
从版本目录中删除插件
kotlin-compose
以及 gradle 文件的插件块中的相关条目。
在应用程序级别 gradle 文件中,将其添加到
android
块中:
composeOptions {
kotlinCompilerExtensionVersion = "1.2.0"
}
Compose 编译器的版本与 Kotlin 版本相关联,因此只要您使用 Kotlin 1.7.0,您就无法更新此版本。不过,您将能够使用更新的 Compose BOM,尽管我不确定随着时间的推移可能出现的不兼容性或它们将如何体现。
最后,我不知道您为什么将 AGP 版本降级到 7.4.1,但您很可能需要再次增加它才能成功构建。使用Android Studio Ladybug | 2024.2.1 补丁 2 最新版本
8.7.2
对我来说效果很好。
作为结束语,我想再次强调这个练习故意停留在如此旧的 Kotlin 版本上似乎是多么无用。无论您认为需要这样做的原因是什么,我强烈建议您重新考虑。