use*_*437 8 serialization android gradle kotlin
我在将 Kotlin 序列化添加到新的 Android 项目中遇到了很大的麻烦。我在用着Android Studio Chipmunk | 2021.2.1 Patch 2。
当我将插件添加到项目级别build.gradle(见下文)时,我收到此同步错误:
Build file '/Users/sean/Desktop/code/GradleTest/app/build.gradle' line: 3
An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.android']
> Failed to apply plugin 'org.jetbrains.kotlin.android'.
> Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension.
> Companion
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Exception is:
org.gradle.api.plugins.InvalidPluginException: An exception occurred applying plugin request [id: 'org.jetbrains.kotlin.android']
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin 'org.jetbrains.kotlin.android'
Caused by: org.gradle.api.reflect.ObjectInstantiationException: Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension
Caused by: java.lang.NoSuchFieldError: Companion
Run Code Online (Sandbox Code Playgroud)
build.gradle这是我尝试添加插件的项目级别:
buildscript {
ext {
compose_version = '1.1.0-beta01'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.5.31' apply false
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10'
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
这是应用程序模块build.gradle,它是创建新项目时生成的默认模块:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.sthomas.gradletest"
minSdk 25
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
packagingOptions {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation "androidx.compose.ui:ui:$compose_version"
implementation 'androidx.compose.material3:material3:1.0.0-alpha01'
implementation "androidx.compose.ui:ui-tooling-preview:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'androidx.activity:activity-compose:1.3.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version"
debugImplementation "androidx.compose.ui:ui-tooling:$compose_version"
debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_version"
// implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2'
}
Run Code Online (Sandbox Code Playgroud)
这settings.gradle也是默认的:
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "GradleTest"
include ':app'
Run Code Online (Sandbox Code Playgroud)
Tha*_*oro 36
第一步是build.gradle从项目级别开始:您忘记了apply false插件行中版本前面的。
plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10' apply false
}
Run Code Online (Sandbox Code Playgroud)
第二步是build.gradle从应用程序模块开始:应用plugin并添加dependency.
plugins {
id 'org.jetbrains.kotlin.plugin.serialization'
}
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
}
Run Code Online (Sandbox Code Playgroud)
这样您的项目应该可以工作,但我想提请注意,如果您使用不同版本的Koltin和 ,可能会出现不兼容问题Kotlin Plugins。并且由于您使用的是Compose,所以需要注意 的Compiler版本也Compose需要与 的Kotlin版本兼容,并且目前 的Compiler版本Compose不再链接到其他Compose库的版本。要了解更多信息,您可以查看此链接。
因此,为了提高项目中的版本兼容性,您可以这样做:
buildscript {
ext {
kotlin_ver = '1.7.10'
compose_compiler_ver = '1.3.0'
compose_ver = '1.2.1'
}
}
plugins {
id 'com.android.application' version '7.2.2' apply false
id 'com.android.library' version '7.2.2' apply false
id 'org.jetbrains.kotlin.android' version "$kotlin_ver" apply false
id 'org.jetbrains.kotlin.plugin.serialization' version "$kotlin_ver" apply false
}
// ...
Run Code Online (Sandbox Code Playgroud)
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.plugin.serialization'
}
android {
// ...
composeOptions {
kotlinCompilerExtensionVersion compose_compiler_ver
}
// ...
}
dependencies {
// ...
implementation "androidx.compose.ui:ui:$compose_ver"
implementation "androidx.compose.ui:ui-tooling-preview:$compose_ver"
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
//...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9403 次 |
| 最近记录: |