Java 错误:(23, 17) 无法解决:junit:junit:4.12
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32519219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error:(23, 17) Failed to resolve: junit:junit:4.12
提问by fathurzero
Why is it that every time I create a new project in Android Studio, it always comes up with:
为什么每次我在 Android Studio 中创建一个新项目时,总是出现:
Error:(23, 17) Failed to resolve: junit:junit:4.12?
错误:(23, 17) 无法解决:junit:junit:4.12?
When I remove testCompile 'junit:junit:4.12'
in dependencies it is not a problem anymore.
当我删除testCompile 'junit:junit:4.12'
依赖项时,它不再是问题。
build.gradle
构建.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.okedroid.myapplication"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
}
回答by Parisa Baastani
In my case, I just add:
就我而言,我只是添加:
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
}
回答by Roy Selim
When you are not behind a company firewall, using the Android Studio will resolve that issue naturally. But if you are behind a company firewall, this problem will occur. So downloading the files will be a workaround. On your own home network, updating Android Studio will resolve the issue and will ask you to delete those files that you have manually created. Just press OK as it is safe to remove them.
当您不在公司防火墙后面时,使用 Android Studio 自然会解决该问题。但是如果你在公司防火墙后面,就会出现这个问题。因此,下载文件将是一种解决方法。在您自己的家庭网络上,更新 Android Studio 将解决该问题,并会要求您删除手动创建的那些文件。只需按 OK,因为可以安全地移除它们。
回答by Roy Selim
The error is caused by missing jar files. My simple solution is just by copying all the files that does not resolve to the C:\Program Files\Android\Android Studio\gradle\m2repository... (your file path may be different). I got around 6 dependencies that cannot be resolved, junit is one of them and the others are com and org and javax. Since I am behind a firewall, even if I added the code for the repository, the Android Studio doesn't seem to access it. I went to the maven repository and downloaded all the files that will not resolve. Problem solved and the Gradle sync without error. But I am a certified newbie with only 1 day experience so I do not not if this is the proper way to do it.
该错误是由缺少 jar 文件引起的。我的简单解决方案是将所有无法解析的文件复制到 C:\Program Files\Android\Android Studio\gradle\m2repository...(您的文件路径可能不同)。我有大约 6 个无法解析的依赖项,junit 是其中之一,其他的是 com、org 和 javax。由于我在防火墙后面,即使我添加了存储库的代码,Android Studio 似乎也无法访问它。我去了maven存储库并下载了所有无法解析的文件。问题解决了,Gradle 同步没有错误。但我是一名经过认证的新手,只有 1 天的经验,所以如果这是正确的做法,我不会。
Example error: Cannot resolve com:squareup:javawriter:2.1.1
示例错误:无法解析 com:squareup:javawriter:2.1.1
Step 1: I created a path C:\Program Files\Android\Android Studio\gradle\m2repository\com\squareup\javawriter\2.1.1\
第 1 步:我创建了一个路径 C:\Program Files\Android\Android Studio\gradle\m2repository\com\squareup\javawriter\2.1.1\
Step 2: I downloaded all the jar files from the maven repository: http://repo1.maven.org/maven2/com/squareup/javawriter/2.1.1/
第2步:我从maven仓库下载了所有的jar文件:http: //repo1.maven.org/maven2/com/squareup/javawriter/2.1.1/
Step 3: I manually downloaded all the .jar files from this directory
第 3 步:我从该目录手动下载了所有 .jar 文件
Step 4: I copied them to C:\Program Files\Android\Android Studio\gradle\m2repository\com\squareup\javawriter\2.1.1\
第 4 步:我将它们复制到 C:\Program Files\Android\Android Studio\gradle\m2repository\com\squareup\javawriter\2.1.1\
I repeated this to all the files that did not resolve including the junit.
我对所有未解析的文件(包括junit)重复此操作。
For junit, here is the directory I created: C:\Program Files\Android\Android Studio\gradle\m2repository\junit\junit\4.12 and then I copied the junit-4.12.jar inside this directory and the junit error disappeared.
对于junit,这是我创建的目录:C:\Program Files\Android\Android Studio\gradle\m2repository\junit\junit\4.12 然后我将junit-4.12.jar 复制到该目录中,junit 错误消失了。
回答by saibaba vali
Mostly this issue will come when ever you update your SDK, at the time of project creation Gradle build fails. If you add the mavenand jcenterrepositories to your build.gradle(Module:app)your problem will solved.
大多数情况下,这个问题会在您更新 SDK 时出现,在项目创建时 Gradle 构建失败。如果您将maven和jcenter存储库添加到您的build.gradle(Module:app) 中,您的问题将得到解决。
repositories {
maven { url 'http://repo1.maven.org/maven2' }
jcenter { url "http://jcenter.bintray.com/" }
}
These are the two line you have to add after
这些是您必须在之后添加的两行
apply plugin: 'com.android.application'
This is how your code look like after adding those two lines
这是添加这两行后您的代码的样子
apply plugin: 'com.android.application'
repositories {
maven { url 'http://repo1.maven.org/maven2' }
jcenter { url "http://jcenter.bintray.com/" }
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.okedroid.myapplication"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
回答by Tasko
My installation of Android Studio is completely offline. To resolve these kind of problems I have used the Process Monitor. It showed that a few files are missing within "Android Studio\gradle\m2repository". I have downloaded all files from
我的 Android Studio 安装是完全离线的。为了解决这些问题,我使用了 Process Monitor。它表明“Android Studio\gradle\m2repository”中缺少一些文件。我已经下载了所有文件
- http://repo1.maven.org/maven2/junit/junit/4.12/
- http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/
- repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.3/ (I can only post two links...;-))
- http://repo1.maven.org/maven2/junit/junit/4.12/
- http://repo1.maven.org/maven2/org/hamcrest/hamcrest-core/
- repo1.maven.org/maven2/org/hamcrest/hamcrest-parent/1.3/(我只能发布两个链接...;-))
and copied these files in "Android Studio\gradle\m2repository". You have to create the structure below "m2repository" as it is below "maven2". Maybe there are more missing files, but at this time at least Gradle build is finished without errors.
并将这些文件复制到“Android Studio\gradle\m2repository”中。您必须在“m2repository”下面创建结构,因为它在“maven2”下面。也许还有更多丢失的文件,但此时至少 Gradle 构建已完成且没有错误。
回答by Vaisakh Venugopal
回答by Hasaan Ali
Go to File -> Project Structure. Following window will open:
转到文件 - > 项目结构。将打开以下窗口:
From there:
从那里:
- remove the junit library (select junit and press "-" button below ) and
- add it again (select "+" button below, select "library dep.", search for "junit", press OK ). Press OK to apply changes. After around 30 seconds your gradle sync should work fine.
- 删除junit库(选择junit并按下面的“-”按钮)和
- 再次添加(选择下面的“+”按钮,选择“library dep.”,搜索“junit”,按“确定”)。按确定应用更改。大约 30 秒后,您的 gradle 同步应该可以正常工作。
Hope it works for you too :D
希望它也适用于你:D
回答by Oliver Muthomi
Simply try to compile and run you project while having active internet connection.
只需尝试在具有活动互联网连接的情况下编译和运行您的项目。
it worked for me in Android studio 1.5 Ubuntu 15.04
它在 Android Studio 1.5 Ubuntu 15.04 中对我有用
回答by Gaurav
add repository in your build.gradle file
在 build.gradle 文件中添加存储库
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
回答by Naveed Jamali
Just remove the "testCompile 'junit:junit:4.12'" from build.gradle file:
只需从 build.gradle 文件中删除“testCompile 'junit:junit:4.12'”:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'//remove this line and sync again... worked for me
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'}) //if available, also delete these two lines
compile 'com.android.support:appcompat-v7:23.0.1'
}
worked for me and I am Happy! :)
为我工作,我很高兴!:)
Remember that this is not the actual answer of this question. I mentioned this because it works when you remove above lines.
请记住,这不是这个问题的实际答案。我提到这一点是因为当你删除上面的行时它会起作用。