udacity / AdvancedAndroid_TeaTime

115 stars 307 forks source link

Cannot resolve symbol AndroidJUnit4 #14

Open ashu879 opened 6 years ago

ashu879 commented 6 years ago

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

jppasa commented 6 years ago

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.

stevenberdak commented 6 years ago

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 commented 6 years ago

@ashu879 @jppasa

arrmixer commented 6 years ago

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

stevenberdak commented 6 years ago

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'
   })
thekaranacharya commented 6 years ago

@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!

sidDarthVader31 commented 6 years ago

still not working

silentLOL commented 6 years ago

same here --> it's not working

stevenberdak commented 6 years ago

Did you start from a fresh clone?

silentLOL commented 6 years ago

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

DroidPulkit commented 6 years ago

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 :)

cingh-jasdeep commented 6 years ago

Waheguru ji Works great!

nirmaljeffrey commented 5 years ago

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

wafaaHilmy commented 4 years ago

@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

stevenberdak commented 4 years ago

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!

filipebezerra commented 3 years ago

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.

CoolMind commented 3 years ago

@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. :)