簡體   English   中英

無法在 kotlin 項目中使用 java 庫(Gson 和 Toasty)

[英]Can't use java libraries in kotlin project (Gson and Toasty)

我嘗試在我的 kotlin 項目中使用 Gson 和 Toasty 庫但沒有成功,但 android 工作室找不到他們的課程,我錯過了什么?

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'
}

Class 我正在嘗試使用 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 項目,所以我確定我遺漏了一些東西,但花了一些時間后我無法弄清楚它是什么。

嗯,看起來 Gson 的問題是 IDE 的問題,因為一旦我重新啟動它,修復代碼編譯問題,同步 gradle 並執行Build -> Clean Project/Rebuild Project ,它按預期工作。

另一方面,Toasty 看起來需要移動到另一個托管服務,如此處所述: https:https://github.com/GrenderG/Toasty/issues/174

謝謝你的時間

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM