gpt4 book ai didi

android - 如何将 Kotlin 序列化添加到新的 Android 项目?

转载 作者:行者123 更新时间:2023-12-05 08:28:54 25 4
gpt4 key购买 nike

我正忙于将 Kotlin 序列化添加到新的 Android 项目中。我正在使用 Android Studio Chipmunk | 2021.2.1 补丁 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

这是我尝试添加插件的项目级 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
}

这是应用程序模块 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'
}

这是 settings.gradle 也是默认的:

pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "GradleTest"
include ':app'

最佳答案

第一步是项目级别build.gradle:您忘记了插件行中版本前面的apply false

plugins {
id 'org.jetbrains.kotlin.plugin.serialization' version '1.7.10' apply false
}

第二步是在 app 模块build.gradle 上:应用 plugin 并添加 dependency

plugins {
id 'org.jetbrains.kotlin.plugin.serialization'
}

dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.3'
}

这样您的项目应该可以正常工作,但我想提请注意如果您使用不同版本的 KoltinKotlin Plugins 可能会出现不兼容问题。并且由于你使用的是Compose,需要注意的是ComposeCompiler版本需要兼容Kotlin 版本,并且目前 ComposeCompiler 版本不再链接到其他 Compose 库的版本。要了解更多信息,您可以查看此 link .

因此,要提高项目中的版本兼容性,您可以这样做:

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
}

// ...
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'

//...
}

关于android - 如何将 Kotlin 序列化添加到新的 Android 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73502370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com