Android Studio建立新module時出現: "Failed to resolve: junit:junit:4.12" 編譯錯誤
一. 錯誤出現的場景
今天從googlesamples下了一個截圖專案(android-ScreenCapture, 關於5.0開放的截圖API的demo)來學習. 看了原始碼之後, 覺得還是得親自寫一遍, 才能加深印象. 於是就建立了一個module(為了方便就在原專案下建立module). 結果就出現了下面這個gradle錯誤:
剛建立的module, gradle檔案的配置都是自動生成的, 怎麼會出現這個錯誤呢?
二. 解決
剛開始也是一頭霧水, 只能挨個看gradle檔案中的配置.
結果發現: **專案根目錄下的build.gradle檔案是空的!! **
我也是醉了, 谷歌攻城獅都不按套路出牌的!!!
外掛的倉庫和的依賴庫的倉庫都配置主module下的build.gradle檔案中!! 好吧, 那我也不按套路出牌了. 我也把外掛的倉庫配置到我自己建立的module下的build.gradle中, 結果還是出現上面哪個錯誤, 配置檔案如下:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.stone.demo"
minSdkVersion 21
targetSdkVersion 25
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:25.0.1'
testCompile 'junit:junit:4.12'
}
這是為啥捏? 各位觀眾能發現上面這個gradle檔案的問題麼?
我相信你已經發現了其中的問題. 我經過了一番折騰也發現了, 問題就是: 沒有配置依賴庫的倉庫, 即少了下面的語句:
repositories {
jcenter()
}
完整gradle配置如下:
buildscript {
//配置gradle外掛的倉庫
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.stone.demo"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//配置依賴庫的倉庫
repositories {
jcenter()
}
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:25.0.1'
testCompile 'junit:junit:4.12'
}
可以看出, 上面的指令碼中有兩處respositories {}
配置, 第一個是配置gradle外掛的依賴倉庫, 第二個是配置依賴庫的倉庫. 因此, 這兩個都不能少!!
三. 總結
第一個連結說直接刪除junit的依賴可以解決這個問題, 這個解決方法確實簡單, 可以滿足一部分人的需求. 但是如果需要單元測試, 那麼就不能刪除junit的依賴. 因此這個方案並不是好的解決方案 (錯誤出現場景和產生原因並沒說清楚, 很多情況並不能套用此解決方案).
第二個連結說新增maven倉庫, 其實"http://repo1.maven.org/maven2"這個倉庫就是gradle內建的maven倉庫, 即mavenCentral()
. 直接寫mavenCentral()
更加簡單, 還能避免拼寫錯誤. 再者作者新增倉庫的位置是新增外掛倉庫的位置, 對於依賴庫來說這樣寫並不正確 . (同樣作者也沒用說明, 錯誤出現的場景和產生原因)
第三個連結中, 有人說刪掉junit依賴, 再重新新增. 我試過, 沒效果 (因為我的問題產生的原因和他的並不一樣).
最終還是根據自己的實際情況和自己對與gradle的理解解決了問題.
我想說的是, 遇到問題的時候不要著急者去谷歌百度, 先根據錯誤資訊和自己已有的知識嘗試解決; 如果解決不了, 再谷歌百度; 如果谷歌百度也解決不了, 可以向大神諮詢 ......
這樣, 如果自己能解決問題, 那就可以節約不少搜尋的時間, 第二可以培養自己獨立思考問題和解決問題的能力.