无法在kotlin项目中使用java库(Gson和Toasty)

wydwbb8l  于 2022-11-29  发布在  Kotlin
关注(0)|答案(1)|浏览(233)

我尝试在我的kotlin项目中使用Gson和Toasty库,但没有成功,但android studio找不到它们的类,我错过了什么?
build.gradle

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'androidx.navigation.safeargs.kotlin'
    id 'kotlin-kapt'

}

android {
    compileSdk 33

    defaultConfig {
        applicationId "dev.gmarques.compras"
        minSdk 28
        targetSdk 33
        versionCode 1
        versionName "1.0"

        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            versionNameSuffix '.release'
        }
        debug {
            versionNameSuffix '.debug'
        }
    }

    dataBinding {
        enabled = true
    }

    viewBinding {
        enabled = true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildToolsVersion '33.0.0'
    namespace 'dev.gmarques.compras'
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.1'
    implementation 'com.google.android.material:material:1.7.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment-ktx:2.5.2'
    implementation 'androidx.navigation:navigation-ui-ktx:2.5.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.5'
    implementation 'androidx.recyclerview:recyclerview:1.2.1'

    // room
    implementation "androidx.room:room-runtime:2.4.3"
    annotationProcessor "androidx.room:room-compiler:2.4.3"
    implementation "androidx.room:room-ktx:2.4.3"
    kapt "androidx.room:room-compiler:2.4.3"
    // room

    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    implementation 'com.google.code.gson:gson:2.10'  // ----------------------Gson
    implementation 'com.github.GrenderG:Toasty:1.5.2' // ----------------------Toasty

    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

我尝试使用Gson并得到Unresolved reference: Gson的类

package dev.gmarques.compras

import dev.gmarques.compras.objetos.Categoria
import dev.gmarques.compras.objetos.CategoriaRv

 class Clone {
    companion object {

    fun categoriaParaCategoriaRv(categoria: Categoria): CategoriaRv {
        var jsonString = Gson().toJson(categoria)
        //return Gson().fromJson()

    }

}

}
烤面包也是一样
这是我的第一个kotlin项目,所以我肯定我错过了一些东西,但花了一段时间后,我还没有能够弄清楚它是什么。

cunj1qz1

cunj1qz11#

好吧,看起来Gson的问题是IDE的问题,因为一旦我重新启动它,修复了代码编译问题,同步了Gradle并执行了Build -> Clean Project/Rebuild Project,它就像预期的那样工作了。
另一方面,Toasty看起来需要移动到另一个托管服务,如这里所讨论的:https://github.com/GrenderG/Toasty/issues/174
感谢您抽出宝贵时间

相关问题