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

问题描述 投票:0回答:2

我在将 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

这是项目级别

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'
android kotlin gradle serialization
2个回答
35
投票

第一步是从

项目级别
开始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'
}

您的项目应该可以工作,但我想提请注意,如果您使用不同版本的

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
}

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

    //...
}

0
投票

如果您使用的是 Kotlin DSL 那么您可以按照以下步骤操作 在关卡gradle文件中添加以下代码

id("org.jetbrains.kotlin.plugin.serialization") version "1.7.10" apply false

转到应用程序级别的 gradle 文件 在插件中添加以下代码

plugins {
    id("com.android.application")
..
..
..
    id("org.jetbrains.kotlin.plugin.serialization")
}

在依赖项部分添加以下依赖项

// KotlinX Serialization
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.2.2")
© www.soinside.com 2019 - 2024. All rights reserved.