无法解析符号gson

时间:2018-09-01 16:58:37

标签: java android gradle gson

我在使用gson库时遇到了很多麻烦。构建项目时,我得到:

  

“无法解析符号gson”

我已经用Google搜索了几个小时,但似乎没有解决方案。这是我的build.gradle文件:

android {
    compileSdkVersion 24
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.jasminkrhan.vaktija"
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.8.2'

}

还有该项目的build.gradle文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {

    repositories {
        jcenter {
            url "http://jcenter.bintray.com/"
        }
    }

    repositories {
        maven  {
            url "http://repo1.maven.org/maven2"
        }
    }
}

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

有人看到我的代码有问题吗? android studio为什么找不到该库?我已经尝试过重启Android Studio,但这无济于事。

2 个答案:

答案 0 :(得分:2)

  

params.permit(:company_id, :customer_id, :name, :del_address1, :del_address2, :del_address3, :del_address4, :del_postcode, :tel, :created_by) 已过时。请使用compile

implementation

  

您需要在项目级别 build.gradle

中使用这些功能
implementation 'com.google.code.gson:gson:2.8.5'

答案 1 :(得分:0)

尝试将所有存储库全部编写成一个库,并已使用Google依赖项和jcenter中包含的功能,而不要提供URL。像这样:

var questionText = "1 ding ?"
var index = 0;
var num = "";
do
{
    num += questionText[index];
} while (questionText[++index] >= "0" && questionText[index] <= "9");
questionText = questionText.substring(num.length);

还有:

buildscript {
...
    repositories {
        google()
        jcenter()
    }
}

同样重要的是,由于不推荐使用,所以开始在gradle中使用allprojects { repositories { google() jcenter() } } 标签而不是implementation

compile