AdvancedAndroid_TeaTime icon indicating copy to clipboard operation
AdvancedAndroid_TeaTime copied to clipboard

Cannot resolve symbol AndroidJUnit4

Open ashu879 opened this issue 6 years ago • 17 comments

Upon adding libs to gradle I am getting cannot resolve AndroidJUnit4 error.

ashu879 avatar May 07 '18 23:05 ashu879

Got the same error.

Adding in app's build.gradle the following worked for me:

testCompile 'com.android.support.test:rules:1.0.1'
testCompile 'com.android.support.test:runner:1.0.1'

but still, I'm not able to run the test. Even though I upgraded all dependencies to the latest.

jppasa avatar May 20 '18 01:05 jppasa

Please step through each step carefully and in order. Allow each one time to build before going to the next:

(1) Delete your current project folder and create a new clone of the AdvancedAndroid_TeaTime repository and open the branch you are on.

(2) Do not upgrade your gradle version, click on 'do not remind me again for this project'

(3) Add these dependencies to your app level 'build.gradle' file:

    compile 'com.android.support.test.espresso:espresso-idling-resource:2.2.2'

    androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'

(4) Add this to your app level 'build.gradle' file outside of any brackets:

configurations.all {
    resolutionStrategy {
        forcedModules = ['com.android.support:support-annotations:25.1.0']
    }
}

(5) Add maven.google repository in your project level build.gradle file:

example:

buildscript {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }
}

(6) Rebuild the project

(7) Under the gradle menu on the right expand 'AdvancedAndroid_TeaTime(root)' and under Tasks > build, click on assembleAndroidTest

(8) Try to add AndroidJUnit4.class to a test class.

stevenberdak avatar Jul 15 '18 23:07 stevenberdak

@ashu879 @jppasa

stevenberdak avatar Jul 15 '18 23:07 stevenberdak

@StevenBerdak Thank you! you're a life Saver. 👍

arrmixer avatar Jul 17 '18 14:07 arrmixer

Just a quick update, the above instructions seems to work well but you can also try this if you like:

https://gwgnanodegrees.slack.com/archives/CB5HPB7G8/p1531768330000478

Pedram [12:12 PM]
commented on Rakesh Praneeth K(AND)’s file Espresso Errors.png
I had the same problem and I was able to fix it by adding these to my app build.gradle. Note the exclude group.
Pedram [12:12 PM]
testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
   androidTestImplementation 'com.android.support.test:runner :1.0.2'
   androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
       exclude group: 'com.android.support', module: 'support-annotations'
   })

stevenberdak avatar Jul 17 '18 15:07 stevenberdak

@StevenBerdak Thank you so much for the solution. Works perfectly. Had I not found this, I was on the verge of re-installing Android Studio itself!

thekaranacharya avatar Sep 21 '18 01:09 thekaranacharya

still not working

sidDarthVader31 avatar Oct 29 '18 09:10 sidDarthVader31

same here --> it's not working

silentLOL avatar Nov 05 '18 21:11 silentLOL

Did you start from a fresh clone?

stevenberdak avatar Nov 05 '18 21:11 stevenberdak

I found it. All I had to do was running the tests. Sorry, my bad.

silentLOL avatar Nov 05 '18 21:11 silentLOL

Exactly, steps followed by @StevenBerdak is already done and if you see AndroidJUnit4 error, just click on the green play button on left of public class IdlingResourceMenuActivityTest and it works after it installs itself on your phone :)

DroidPulkit avatar Nov 09 '18 01:11 DroidPulkit

Waheguru ji Works great!

cingh-jasdeep avatar Nov 10 '18 08:11 cingh-jasdeep

@StevenBerdak Thanks a lot! I lost my patience on this problem for the last two days. You are my saviour!!!

nirmaljeffrey avatar Feb 03 '19 14:02 nirmaljeffrey

@stevenberdak please could you explain this step i cant understand it
(7) Under the gradle menu on the right expand 'AdvancedAndroid_TeaTime(root)' and under Tasks > build, click on assembleAndroidTest

wafaaHilmy avatar Nov 20 '19 18:11 wafaaHilmy

I haven't used Android Studio in awhile as I do web dev now, and I also know Android Studio has had updates but... There was a Gradle menu that popped out from the right side of the screen with Gradle tasks. It should be somewhere in the program if it is no longer there... and you would click on the task mentioned above (assembleAndroidTest) to trigger Gradle to perform the task which would complete the step. Hope this helps!

stevenberdak avatar Nov 25 '19 21:11 stevenberdak

Currently (in the version androidx.test:runner:1.3.0) androidx.test.runner.AndroidJUnit4 is deprecated and thus is recommended to use androidx.test.ext.junit.runners.AndroidJUnit4 instead.

In order to setup this dependency declare androidTestImplementation 'androidx.test.ext:junit:1.1.2'.

You can also rely on the guide to set up a project with AndroidX Test.

filipebezerra avatar Dec 08 '20 19:12 filipebezerra

@filipebezerra I tried to apply your advice to a usual Android project and got an exception when ran tests:

Test running failed: Instrumentation run failed due to 'java.lang.NoSuchMethodException'

androidx.test.runner.AndroidJUnit4 is not deprecated. :)

CoolMind avatar Dec 23 '20 15:12 CoolMind