Issue
I am having a problem that seems to be quite popular on this plattform, yet no answer seems to work for me. I am facing a similar problem to this:
Error:(23, 17) Failed to resolve: junit:junit:4.12
But none of the 43 answers are applicaple to my problem. (I hope this won't be marked as duplicate)
I have tried importing the org.junit package to the test class manually, but it couldn't resolve it. I figured that the reason for this is that Gradle can't load the JUnit library, but I have no clue how to change that.
Even upon creating a new android studio project, I have problems setting up JUnit. Not even the example tests can be ran:
package de.ckc.azubis.fleetparkmanagement;
import static junit.framework.Assert.assertEquals;
/**
* Example local unit test, which will execute on the development machine
(host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
When I try to import the Test annotation, the JUnit dependency gets added to the gradle file, but does not get imported to the test class.
here my module level build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "de.ckc.azubis.fleetparkmanagement"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support:cardview-v7:27.0.2'
}
and my project level build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I have tried everything i can think of, including reconfiguring the dependencies, manually adding every repository i could think of (mavenCentral, jcenter...), checked my proxy settings and much more.
Solution
There was a problem with caching.
I fixed it by clicking on file -> Invalidate Caches
and then restart
Answered By - garritfra