【问题标题】:Can't use java libraries in kotlin project (Gson and Toasty)不能在 kotlin 项目中使用 java 库(Gson 和 Toasty)
【发布时间】:2022-11-28 05:49:40
【问题描述】:

我尝试在我的 kotlin 项目中使用 Gson 和 Toasty 库失败,但 android studio 找不到它们的类,我错过了什么?

构建.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()

    }

}

}

Toasty 也一样。

这是我的第一个 kotlin 项目,所以我确定我遗漏了一些东西,但花了一些时间后我无法弄清楚它是什么。

【问题讨论】:

  • 也许你应该import com.google.gson.Gson
  • 根据您的 IDE,您可能希望将项目与 Gradle 配置重新同步。然后IDE可能会建议导入com.google.gson.Gson
  • @radof IDE 不建议自动导入,当我尝试手动导入时,它无法识别库包
  • @Joffrey 试过了,没用
  • 如果您手动添加导入并修复函数的编译错误(因为您没有返回任何内容),它是否可以从命令行使用 gradle 正确编译?

标签: java android kotlin gson


【解决方案1】:

好吧,看起来 Gson 的问题是 IDE 的问题,因为一旦我重新启动它,修复了代码编译问题,同步了 gradle 并执行了 Build -> Clean Project/Rebuild Project,它就按预期工作了。

另一方面,Toasty 看起来需要转移到另一个托管服务,如此处讨论:https://github.com/GrenderG/Toasty/issues/174

谢谢你的时间

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 2018-07-03
    • 2019-02-03
    • 2012-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-20
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多