【问题标题】:I have implemented retrofit library with gson but getting an importing error "Cannot resolve symbol 'Gson'"我已经使用 gson 实现了改造库,但出现导入错误“无法解析符号 'Gson'”
【发布时间】:2022-01-26 07:51:42
【问题描述】:

我已经使用 gson 实现了改造库,但出现导入错误。

Android 工作室版

Android Studio 北极狐 | 2020.3.1 补丁 2 构建 #AI-203.7717.56.2031.7678000,建于 2021 年 8 月 27 日

应用级 build.gradle

implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.app"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    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 {
        viewBinding true
        dataBinding true
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'

    // Import the Firebase BoM
    implementation platform('com.google.firebase:firebase-bom:29.0.3')

    // Declare the dependency for the Firebase Authentication library
    implementation 'com.google.firebase:firebase-auth-ktx'
    implementation 'com.google.firebase:firebase-messaging-ktx'
    implementation 'com.google.firebase:firebase-analytics-ktx'

    implementation 'com.google.android.gms:play-services-safetynet:18.0.0'

    //Nav Component
    implementation "androidx.fragment:fragment-ktx:1.3.6"
    implementation "androidx.navigation:navigation-fragment-ktx:2.3.5"
    implementation "androidx.navigation:navigation-ui-ktx:2.3.5"

    // Live Data
    implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"

    //Material Dialog
    implementation 'com.afollestad.material-dialogs:core:3.3.0'
    implementation 'com.afollestad.material-dialogs:bottomsheets:3.3.0'

    //RecyclerView
    implementation 'androidx.recyclerview:recyclerview:1.2.1'

    implementation 'androidx.constraintlayout:constraintlayout:2.1.1'

    //interceptor
    implementation 'com.itkacher.okhttpprofiler:okhttpprofiler:1.0.7'

    //Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation "com.squareup.okhttp3:logging-interceptor:4.8.0"
    implementation 'org.conscrypt:conscrypt-android:2.2.1'

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

顶层 build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.3"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.31"

        classpath 'com.google.gms:google-services:4.3.10'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


task clean(type: Delete) {
    delete rootProject.buildDir
}

settings.gradel

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
        maven { url "https://jitpack.io" }
    }
}
rootProject.name = "Exaple app"
include ':app'

收到此错误

我已经完成了一些工作,例如清理项目、重建和使缓存无效以及重新启动。但仍然出现同样的错误。

提前致谢。

【问题讨论】:

  • 你能发布你的根 gradle.app 文件吗?
  • @Skizo-ozᴉʞS 立即查看
  • 你能上传 build.gradle 文件吗?项目一你有 google() 在 repositories{...} 吗?
  • @Skizo-ozᴉʞS 是上传
  • 这发生在哪个文件上?改造,还是你自己的代码?因为如果是你自己的代码,除非retrofit-gson包有api com.google:gson作为它的依赖,否则你不会从Retrofit获得传递依赖,你仍然需要添加直接GSON依赖,这样你的代码才能使用它,如@ Skizo-ozᴉʞS 说。

标签: android kotlin gson retrofit


【解决方案1】:

您还需要实现GSON dependency

我会说你错过了这个

implementation("com.google.code.gson:gson:2.8.9")

【讨论】:

  • com.squareup.retrofit2:converter-gson:2.9.0 将其作为依赖项。因此,您也不必自己添加它,但是尝试一下也无妨
猜你喜欢
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
相关资源
最近更新 更多