
錯誤:(24, 17) 無法解析:junit:junit:4.12 “openFile:/home/jeevansai/AndroidStudioProjects/MyApplication/app/build.gradle”
上述錯誤出現在 Android Studio 中,製作專案時存在許多問題。有人能告訴解決辦法嗎。
的輸出 cat /home/jeevansai/AndroidStudioProjects/MyApplication/app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '23.0.1'
defaultConfig {
applicationId "com.example.jeevansai.myapplication"
minSdkVersion 8
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
final def types = buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
types
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
的輸出apt-cache policy junit4
junit4:
Installed: 4.12-2ubuntu1
Candidate: 4.12-2ubuntu1
Version table:
*** 4.12-2ubuntu1 0
500 http://in.archive.ubuntu.com/ubuntu/ wily/main amd64 Packages
100 /var/lib/dpkg/status
答案1
Gradle 是Android Studio 中整合的建置工具:它從Maven 儲存庫下載專案依賴項(您在程式碼中使用的jar 檔案)(maven 是另一個眾所周知的建置工具,具有套件依賴項功能和儲存庫的遠端生態系統)。最常用的儲存庫之一是jcenter()。
您的 gradle 檔案缺少儲存庫配置:將這段程式碼新增至您的 gradle 檔案 ( /home/jeevansai/AndroidStudioProjects/MyApplication/app/build.gradle
):apply plugin
和android {...}
部分之間。
apply plugin: 'com.android.application'
repositories {
jcenter()
}
android {
compileSdkVersion 21
...
或者,如果您目前沒有編寫單元測試程式碼,您可以簡單地註解掉該app/build.gradle
檔案的 junit 依賴關係:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
}
無論如何,您始終可以從 Maven Central 下載所需的工件並將其放入您的/libs
目錄中:
wget 'http://central.maven.org/maven2/junit/junit/4.12/junit-4.12.jar' -O /home/jeevansai/AndroidStudioProjects/MyApplication/app/libs/junit-4.12.jar