我在使用 gson 庫時遇到了大麻煩。在構建項目時,我得到:“無法解析符號gson”我用谷歌搜索了幾個小時,但似乎沒有解決方案。這是我的 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 回答

浮云間
TA貢獻1829條經驗 獲得超4個贊
compile已經過時了。請用implementation
implementation 'com.google.code.gson:gson:2.8.5'
您在項目級build.gradle 中需要這些
buildscript {
repositories {
google()
jcenter()
}
.....
allprojects {
repositories {
google()
jcenter()
}
}

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
嘗試將所有存儲庫編寫在一起,并使用已包含在 Google 依賴項和 jcenter 中的函數,而不是提供 URL。像這樣:
buildscript {
...
repositories {
google()
jcenter()
}
}
并且:
allprojects {
repositories {
google()
jcenter()
}
}
開始使用implementation標簽而不是compile在 gradle 中也很重要,因為它已被棄用:
implementation 'com.google.code.gson:gson:2.8.5'
添加回答
舉報
0/150
提交
取消