Cannot resolve symbol 'AndroidJUnit4'












109















Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be



import android.support.test.runner.AndroidJUnit4;


When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".



Background



I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:



configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}


This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?



Relevent sections from `./gradlew :app:dependencies



androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| --- org.easytesting:fest-assert-core:2.0M10
| --- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | --- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| --- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| --- com.android.support.test:runner:0.2 (*)
--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| --- com.android.support:support-v4:22.2.0
| --- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| --- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
--- com.jakewharton:butterknife:5.1.2









share|improve this question

























  • That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

    – CommonsWare
    Jun 2 '15 at 18:16











  • @CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

    – Code-Apprentice
    Jun 2 '15 at 18:23













  • According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

    – CommonsWare
    Jun 2 '15 at 18:26






  • 1





    Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

    – Roy Falk
    Jun 11 '18 at 7:17
















109















Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be



import android.support.test.runner.AndroidJUnit4;


When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".



Background



I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:



configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}


This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?



Relevent sections from `./gradlew :app:dependencies



androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| --- org.easytesting:fest-assert-core:2.0M10
| --- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | --- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| --- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| --- com.android.support.test:runner:0.2 (*)
--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| --- com.android.support:support-v4:22.2.0
| --- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| --- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
--- com.jakewharton:butterknife:5.1.2









share|improve this question

























  • That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

    – CommonsWare
    Jun 2 '15 at 18:16











  • @CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

    – Code-Apprentice
    Jun 2 '15 at 18:23













  • According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

    – CommonsWare
    Jun 2 '15 at 18:26






  • 1





    Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

    – Roy Falk
    Jun 11 '18 at 7:17














109












109








109


16






Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be



import android.support.test.runner.AndroidJUnit4;


When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".



Background



I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:



configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}


This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?



Relevent sections from `./gradlew :app:dependencies



androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| --- org.easytesting:fest-assert-core:2.0M10
| --- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | --- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| --- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| --- com.android.support.test:runner:0.2 (*)
--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| --- com.android.support:support-v4:22.2.0
| --- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| --- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
--- com.jakewharton:butterknife:5.1.2









share|improve this question
















Obviously I need the correct import statment to solve this problem. According to the docs for AndroidJUnit4, this should be



import android.support.test.runner.AndroidJUnit4;


When I do that, Android Studio highlights runner in red and complains "Cannot resolve symbol 'runner'".



Background



I got to this point by following the tutorials on the Android Developer site for setting up tests using UI Automator. The first problem I encountered was that com.android.support:support-v4:22.2.0 and com.android.support.test:runner:0.2 depend on different versions of com.android.support:support-annotations. I followed the suggestions from this Android bug report and added the following to allprojects in my project's build.gradle:



configurations.all {
resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}


This solved the immediate error, but I suspect it lead to my current problems. Does anyone have any suggestions about how to fix this?



Relevent sections from `./gradlew :app:dependencies



androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
| --- org.easytesting:fest-assert-core:2.0M10
| --- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
| +--- junit:junit-dep:4.10
| | --- org.hamcrest:hamcrest-core:1.1
| +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
| --- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
| --- com.android.support.test:runner:0.2 (*)
--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
| --- com.android.support:support-v4:22.2.0
| --- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
| --- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
--- com.jakewharton:butterknife:5.1.2






android gradle build.gradle android-uiautomator testing-support-library






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 10 '16 at 3:45







Code-Apprentice

















asked Jun 2 '15 at 18:10









Code-ApprenticeCode-Apprentice

47.9k1490176




47.9k1490176













  • That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

    – CommonsWare
    Jun 2 '15 at 18:16











  • @CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

    – Code-Apprentice
    Jun 2 '15 at 18:23













  • According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

    – CommonsWare
    Jun 2 '15 at 18:26






  • 1





    Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

    – Roy Falk
    Jun 11 '18 at 7:17



















  • That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

    – CommonsWare
    Jun 2 '15 at 18:16











  • @CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

    – Code-Apprentice
    Jun 2 '15 at 18:23













  • According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

    – CommonsWare
    Jun 2 '15 at 18:26






  • 1





    Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

    – Roy Falk
    Jun 11 '18 at 7:17

















That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

– CommonsWare
Jun 2 '15 at 18:16





That suggests that your build is missing com.android.support.test:runner outright. Can you confirm via gradle dependencies that you're still pulling this in after the resolutionStrategy change?

– CommonsWare
Jun 2 '15 at 18:16













@CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

– Code-Apprentice
Jun 2 '15 at 18:23







@CommonsWare Added dependencies output to my question. I don't know what the (*) means after com.android.support.test:runner:0.2.

– Code-Apprentice
Jun 2 '15 at 18:23















According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

– CommonsWare
Jun 2 '15 at 18:26





According to the Gradle folks, the asterisk "means that the tree view of the dependency graph is cut short at this point because that part of the graph was already listed earlier." I'm stumped as to why this isn't working for you.

– CommonsWare
Jun 2 '15 at 18:26




1




1





Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

– Roy Falk
Jun 11 '18 at 7:17





Note that @stemadsen 's answer from 2018 is potentially more relevant than the others. Someone once wrote about a test that kept the questions the same year after year, but the answers changed...

– Roy Falk
Jun 11 '18 at 7:17












10 Answers
10






active

oldest

votes


















158














Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:



enter image description here






share|improve this answer





















  • 3





    Thanks, you saved me

    – Sofa
    May 27 '16 at 13:10






  • 6





    Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

    – Chantell Osejo
    Jun 23 '16 at 23:01






  • 19





    Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

    – WIllJBD
    Jun 30 '16 at 17:28








  • 2





    I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

    – Gautham C.
    Jul 18 '16 at 15:25






  • 1





    wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

    – Karoly
    Jul 27 '17 at 21:53





















94














I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.






share|improve this answer


























  • In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

    – Teo Inke
    Aug 10 '16 at 10:58



















59














Ok so here is your mistake and mine!



If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.



Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)






share|improve this answer


























  • Thanks a lot buddy , it worked for me

    – Zia
    Sep 16 '16 at 7:30











  • This is working thanks a lot.

    – Lava Sangeetham
    Sep 26 '16 at 6:30











  • I am using Maven, what then?

    – JohnyTex
    Feb 1 '17 at 11:32






  • 2





    @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

    – Sepehr Behroozi
    Feb 4 '17 at 13:42



















26














Update



The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.



Original Answer



I found here that there are newer versions of the Testing Support Library than what I was using:



dependencies {
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}


Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.






share|improve this answer





















  • 2





    now the runner is updated to 0.4

    – herbertD
    Dec 9 '15 at 8:21






  • 26





    aaaand, still doesn't work

    – Karoly
    Mar 27 '16 at 16:32






  • 1





    I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

    – Nom1fan
    Jul 27 '18 at 11:04













  • @Karoly I still have problem. Did you find any solution?

    – Kenji
    Jan 13 at 15:38



















18














I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:



debugImplementation 'com.android.support.test:runner:1.0.1'


or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.



I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.



Project structure






share|improve this answer


























  • This solution worked for me, thx! Used debugImplementation

    – Michał Dobi Dobrzański
    May 4 '18 at 10:42













  • Note that this will compile the testing support library into the APK.

    – Code-Apprentice
    Oct 1 '18 at 18:37



















3














In my case this helped for release variant:



android {
...
testBuildType "release"
}





share|improve this answer


























  • Can you give a more complete solution. I don't understand the context of what you did.

    – Code-Apprentice
    Feb 17 '18 at 17:03











  • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

    – Andrew Glukhoff
    Feb 17 '18 at 20:12











  • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

    – Code-Apprentice
    Feb 17 '18 at 20:46



















2














put this code in your Dependencies



compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})





share|improve this answer



















  • 1





    When I wrote this question, I had not yet added espresso as a dependency for unit tests.

    – Code-Apprentice
    Jan 4 '17 at 13:08











  • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

    – Tony D
    Jan 13 '17 at 22:11






  • 2





    Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

    – Khay
    Jul 21 '17 at 19:03



















2














If anyone still having this issue:




Cannot resolve symbol 'AndroidJUnit4'




and using API 27, in the build.gradle which is in the app module, add the following lines:



testImplementation 'junit:junit:4.12'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'





share|improve this answer

































    0














    Adding



    compile com.android.support.test:runner:0.5'


    resolved this exact issue for me.






    share|improve this answer
























    • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

      – Code-Apprentice
      Mar 31 '18 at 16:40











    • Is androidTestCompile same as compile?

      – donlys
      Apr 1 '18 at 20:04











    • compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

      – Code-Apprentice
      Apr 1 '18 at 20:09



















    0














    As the list of answers demonstrate, this can be caused by a few things. One more for the list:



    I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.



    Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).



    Next, I need to remove the tests from LINT - or at least disable this warning.



    Edit: @Code-Apprentice, the lines that were missing were:



    import org.junit.After;
    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.Test;
    import org.junit.runner.RunWith;


    import static junit.framework.Assert.assertNotNull;
    import static junit.framework.Assert.assertNull;
    import static org.junit.Assert.assertEquals;
    import static org.junit.Assert.assertTrue;


    So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.






    share|improve this answer


























    • Can you show an example test where this happened for you?

      – Code-Apprentice
      Oct 20 '18 at 2:15











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30603487%2fcannot-resolve-symbol-androidjunit4%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    10 Answers
    10






    active

    oldest

    votes








    10 Answers
    10






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    158














    Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:



    enter image description here






    share|improve this answer





















    • 3





      Thanks, you saved me

      – Sofa
      May 27 '16 at 13:10






    • 6





      Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

      – Chantell Osejo
      Jun 23 '16 at 23:01






    • 19





      Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

      – WIllJBD
      Jun 30 '16 at 17:28








    • 2





      I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

      – Gautham C.
      Jul 18 '16 at 15:25






    • 1





      wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

      – Karoly
      Jul 27 '17 at 21:53


















    158














    Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:



    enter image description here






    share|improve this answer





















    • 3





      Thanks, you saved me

      – Sofa
      May 27 '16 at 13:10






    • 6





      Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

      – Chantell Osejo
      Jun 23 '16 at 23:01






    • 19





      Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

      – WIllJBD
      Jun 30 '16 at 17:28








    • 2





      I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

      – Gautham C.
      Jul 18 '16 at 15:25






    • 1





      wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

      – Karoly
      Jul 27 '17 at 21:53
















    158












    158








    158







    Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:



    enter image description here






    share|improve this answer















    Make sure your app in debug build variant. Go to Build > Select Build Variant... and the following should show up:



    enter image description here







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 12 '18 at 20:25









    Willi Mentzel

    10.1k114770




    10.1k114770










    answered May 22 '16 at 6:12









    oriumorium

    2,72421526




    2,72421526








    • 3





      Thanks, you saved me

      – Sofa
      May 27 '16 at 13:10






    • 6





      Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

      – Chantell Osejo
      Jun 23 '16 at 23:01






    • 19





      Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

      – WIllJBD
      Jun 30 '16 at 17:28








    • 2





      I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

      – Gautham C.
      Jul 18 '16 at 15:25






    • 1





      wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

      – Karoly
      Jul 27 '17 at 21:53
















    • 3





      Thanks, you saved me

      – Sofa
      May 27 '16 at 13:10






    • 6





      Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

      – Chantell Osejo
      Jun 23 '16 at 23:01






    • 19





      Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

      – WIllJBD
      Jun 30 '16 at 17:28








    • 2





      I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

      – Gautham C.
      Jul 18 '16 at 15:25






    • 1





      wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

      – Karoly
      Jul 27 '17 at 21:53










    3




    3





    Thanks, you saved me

    – Sofa
    May 27 '16 at 13:10





    Thanks, you saved me

    – Sofa
    May 27 '16 at 13:10




    6




    6





    Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

    – Chantell Osejo
    Jun 23 '16 at 23:01





    Bless you. The documentation never states "this stuff will only work in debug". So frustrating.

    – Chantell Osejo
    Jun 23 '16 at 23:01




    19




    19





    Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

    – WIllJBD
    Jun 30 '16 at 17:28







    Wow. Up-Vote x 1million. This is the answer I have been searching for for days. Also found this so you can change which build type to use for it. android { testBuildType "staging"}

    – WIllJBD
    Jun 30 '16 at 17:28






    2




    2





    I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

    – Gautham C.
    Jul 18 '16 at 15:25





    I am going to have to file a bug report for this with the Android Studio Tools team. Even build types that inherit from debug don't work unless explicitly set to "debug"

    – Gautham C.
    Jul 18 '16 at 15:25




    1




    1





    wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

    – Karoly
    Jul 27 '17 at 21:53







    wow, I am using a cutsom buildType with initWith(buildTypes.debug) and still fails. Only and only if I am using the debug directly works!

    – Karoly
    Jul 27 '17 at 21:53















    94














    I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.






    share|improve this answer


























    • In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

      – Teo Inke
      Aug 10 '16 at 10:58
















    94














    I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.






    share|improve this answer


























    • In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

      – Teo Inke
      Aug 10 '16 at 10:58














    94












    94








    94







    I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.






    share|improve this answer















    I made the mistake to put the test classes at src/test. After moving them to src/androidTest/java/ the dependency was resolved.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 12 '18 at 20:23









    Willi Mentzel

    10.1k114770




    10.1k114770










    answered Apr 5 '16 at 12:54









    mikesmikes

    1,463108




    1,463108













    • In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

      – Teo Inke
      Aug 10 '16 at 10:58



















    • In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

      – Teo Inke
      Aug 10 '16 at 10:58

















    In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

    – Teo Inke
    Aug 10 '16 at 10:58





    In my case my test folder was src/test for some reason, then I had to rename it to src/androidTest and it solved my problem, after 3 hours...

    – Teo Inke
    Aug 10 '16 at 10:58











    59














    Ok so here is your mistake and mine!



    If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.



    Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)






    share|improve this answer


























    • Thanks a lot buddy , it worked for me

      – Zia
      Sep 16 '16 at 7:30











    • This is working thanks a lot.

      – Lava Sangeetham
      Sep 26 '16 at 6:30











    • I am using Maven, what then?

      – JohnyTex
      Feb 1 '17 at 11:32






    • 2





      @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

      – Sepehr Behroozi
      Feb 4 '17 at 13:42
















    59














    Ok so here is your mistake and mine!



    If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.



    Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)






    share|improve this answer


























    • Thanks a lot buddy , it worked for me

      – Zia
      Sep 16 '16 at 7:30











    • This is working thanks a lot.

      – Lava Sangeetham
      Sep 26 '16 at 6:30











    • I am using Maven, what then?

      – JohnyTex
      Feb 1 '17 at 11:32






    • 2





      @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

      – Sepehr Behroozi
      Feb 4 '17 at 13:42














    59












    59








    59







    Ok so here is your mistake and mine!



    If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.



    Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)






    share|improve this answer















    Ok so here is your mistake and mine!



    If we are going to write a pice of code for Local Unit Testing we shouldn't use @RunWith(AndroidJUnit4.class) cause we do not use AndroidJUnit4 but we need Junit4. so we should write @RunWith(JUnit4.class). And of course your java test file is under app/src/test/java/your.package.name directory.



    Else if (!!) we want to write some Android Instrumented Unit Test we should put our test java files in app/src/androidTest/java/your.package.name directory and use annotation like @RunWith(AndroidJUnit4.class)







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Aug 6 '16 at 6:29

























    answered Apr 6 '16 at 8:17









    Sepehr BehrooziSepehr Behroozi

    1,2661127




    1,2661127













    • Thanks a lot buddy , it worked for me

      – Zia
      Sep 16 '16 at 7:30











    • This is working thanks a lot.

      – Lava Sangeetham
      Sep 26 '16 at 6:30











    • I am using Maven, what then?

      – JohnyTex
      Feb 1 '17 at 11:32






    • 2





      @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

      – Sepehr Behroozi
      Feb 4 '17 at 13:42



















    • Thanks a lot buddy , it worked for me

      – Zia
      Sep 16 '16 at 7:30











    • This is working thanks a lot.

      – Lava Sangeetham
      Sep 26 '16 at 6:30











    • I am using Maven, what then?

      – JohnyTex
      Feb 1 '17 at 11:32






    • 2





      @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

      – Sepehr Behroozi
      Feb 4 '17 at 13:42

















    Thanks a lot buddy , it worked for me

    – Zia
    Sep 16 '16 at 7:30





    Thanks a lot buddy , it worked for me

    – Zia
    Sep 16 '16 at 7:30













    This is working thanks a lot.

    – Lava Sangeetham
    Sep 26 '16 at 6:30





    This is working thanks a lot.

    – Lava Sangeetham
    Sep 26 '16 at 6:30













    I am using Maven, what then?

    – JohnyTex
    Feb 1 '17 at 11:32





    I am using Maven, what then?

    – JohnyTex
    Feb 1 '17 at 11:32




    2




    2





    @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

    – Sepehr Behroozi
    Feb 4 '17 at 13:42





    @JohnyTex Using Maven doesn't affect my answer. It only depends on your project file structure.

    – Sepehr Behroozi
    Feb 4 '17 at 13:42











    26














    Update



    The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.



    Original Answer



    I found here that there are newer versions of the Testing Support Library than what I was using:



    dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }


    Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.






    share|improve this answer





















    • 2





      now the runner is updated to 0.4

      – herbertD
      Dec 9 '15 at 8:21






    • 26





      aaaand, still doesn't work

      – Karoly
      Mar 27 '16 at 16:32






    • 1





      I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

      – Nom1fan
      Jul 27 '18 at 11:04













    • @Karoly I still have problem. Did you find any solution?

      – Kenji
      Jan 13 at 15:38
















    26














    Update



    The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.



    Original Answer



    I found here that there are newer versions of the Testing Support Library than what I was using:



    dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }


    Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.






    share|improve this answer





















    • 2





      now the runner is updated to 0.4

      – herbertD
      Dec 9 '15 at 8:21






    • 26





      aaaand, still doesn't work

      – Karoly
      Mar 27 '16 at 16:32






    • 1





      I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

      – Nom1fan
      Jul 27 '18 at 11:04













    • @Karoly I still have problem. Did you find any solution?

      – Kenji
      Jan 13 at 15:38














    26












    26








    26







    Update



    The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.



    Original Answer



    I found here that there are newer versions of the Testing Support Library than what I was using:



    dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }


    Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.






    share|improve this answer















    Update



    The Android Test Library is now part of AndroidX. Be sure to use the correct Gradle dependencies found in the official documentation.



    Original Answer



    I found here that there are newer versions of the Testing Support Library than what I was using:



    dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    }


    Note: Be sure to use the most recent versions of these libraries. This question is from a time when the Android Test Support Library was new and the version numbers here are very out of date.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 17 '18 at 16:54

























    answered Jun 3 '15 at 18:05









    Code-ApprenticeCode-Apprentice

    47.9k1490176




    47.9k1490176








    • 2





      now the runner is updated to 0.4

      – herbertD
      Dec 9 '15 at 8:21






    • 26





      aaaand, still doesn't work

      – Karoly
      Mar 27 '16 at 16:32






    • 1





      I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

      – Nom1fan
      Jul 27 '18 at 11:04













    • @Karoly I still have problem. Did you find any solution?

      – Kenji
      Jan 13 at 15:38














    • 2





      now the runner is updated to 0.4

      – herbertD
      Dec 9 '15 at 8:21






    • 26





      aaaand, still doesn't work

      – Karoly
      Mar 27 '16 at 16:32






    • 1





      I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

      – Nom1fan
      Jul 27 '18 at 11:04













    • @Karoly I still have problem. Did you find any solution?

      – Kenji
      Jan 13 at 15:38








    2




    2





    now the runner is updated to 0.4

    – herbertD
    Dec 9 '15 at 8:21





    now the runner is updated to 0.4

    – herbertD
    Dec 9 '15 at 8:21




    26




    26





    aaaand, still doesn't work

    – Karoly
    Mar 27 '16 at 16:32





    aaaand, still doesn't work

    – Karoly
    Mar 27 '16 at 16:32




    1




    1





    I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

    – Nom1fan
    Jul 27 '18 at 11:04







    I wonder why the documentation mentions nothing about this, unless I missed it. Would have saved me some grief.

    – Nom1fan
    Jul 27 '18 at 11:04















    @Karoly I still have problem. Did you find any solution?

    – Kenji
    Jan 13 at 15:38





    @Karoly I still have problem. Did you find any solution?

    – Kenji
    Jan 13 at 15:38











    18














    I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:



    debugImplementation 'com.android.support.test:runner:1.0.1'


    or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.



    I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.



    Project structure






    share|improve this answer


























    • This solution worked for me, thx! Used debugImplementation

      – Michał Dobi Dobrzański
      May 4 '18 at 10:42













    • Note that this will compile the testing support library into the APK.

      – Code-Apprentice
      Oct 1 '18 at 18:37
















    18














    I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:



    debugImplementation 'com.android.support.test:runner:1.0.1'


    or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.



    I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.



    Project structure






    share|improve this answer


























    • This solution worked for me, thx! Used debugImplementation

      – Michał Dobi Dobrzański
      May 4 '18 at 10:42













    • Note that this will compile the testing support library into the APK.

      – Code-Apprentice
      Oct 1 '18 at 18:37














    18












    18








    18







    I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:



    debugImplementation 'com.android.support.test:runner:1.0.1'


    or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.



    I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.



    Project structure






    share|improve this answer















    I solved the problem by making a small change in the app's build.gradle file. In the dependencies { ... } section, make sure to include the following line:



    debugImplementation 'com.android.support.test:runner:1.0.1'


    or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.



    I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.



    Project structure







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 6 '18 at 10:03

























    answered Feb 11 '18 at 18:28









    stemadsenstemadsen

    9032813




    9032813













    • This solution worked for me, thx! Used debugImplementation

      – Michał Dobi Dobrzański
      May 4 '18 at 10:42













    • Note that this will compile the testing support library into the APK.

      – Code-Apprentice
      Oct 1 '18 at 18:37



















    • This solution worked for me, thx! Used debugImplementation

      – Michał Dobi Dobrzański
      May 4 '18 at 10:42













    • Note that this will compile the testing support library into the APK.

      – Code-Apprentice
      Oct 1 '18 at 18:37

















    This solution worked for me, thx! Used debugImplementation

    – Michał Dobi Dobrzański
    May 4 '18 at 10:42







    This solution worked for me, thx! Used debugImplementation

    – Michał Dobi Dobrzański
    May 4 '18 at 10:42















    Note that this will compile the testing support library into the APK.

    – Code-Apprentice
    Oct 1 '18 at 18:37





    Note that this will compile the testing support library into the APK.

    – Code-Apprentice
    Oct 1 '18 at 18:37











    3














    In my case this helped for release variant:



    android {
    ...
    testBuildType "release"
    }





    share|improve this answer


























    • Can you give a more complete solution. I don't understand the context of what you did.

      – Code-Apprentice
      Feb 17 '18 at 17:03











    • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

      – Andrew Glukhoff
      Feb 17 '18 at 20:12











    • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

      – Code-Apprentice
      Feb 17 '18 at 20:46
















    3














    In my case this helped for release variant:



    android {
    ...
    testBuildType "release"
    }





    share|improve this answer


























    • Can you give a more complete solution. I don't understand the context of what you did.

      – Code-Apprentice
      Feb 17 '18 at 17:03











    • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

      – Andrew Glukhoff
      Feb 17 '18 at 20:12











    • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

      – Code-Apprentice
      Feb 17 '18 at 20:46














    3












    3








    3







    In my case this helped for release variant:



    android {
    ...
    testBuildType "release"
    }





    share|improve this answer















    In my case this helped for release variant:



    android {
    ...
    testBuildType "release"
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 17 '18 at 20:42









    Code-Apprentice

    47.9k1490176




    47.9k1490176










    answered Feb 17 '18 at 17:02









    Andrew GlukhoffAndrew Glukhoff

    56747




    56747













    • Can you give a more complete solution. I don't understand the context of what you did.

      – Code-Apprentice
      Feb 17 '18 at 17:03











    • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

      – Andrew Glukhoff
      Feb 17 '18 at 20:12











    • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

      – Code-Apprentice
      Feb 17 '18 at 20:46



















    • Can you give a more complete solution. I don't understand the context of what you did.

      – Code-Apprentice
      Feb 17 '18 at 17:03











    • I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

      – Andrew Glukhoff
      Feb 17 '18 at 20:12











    • I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

      – Code-Apprentice
      Feb 17 '18 at 20:46

















    Can you give a more complete solution. I don't understand the context of what you did.

    – Code-Apprentice
    Feb 17 '18 at 17:03





    Can you give a more complete solution. I don't understand the context of what you did.

    – Code-Apprentice
    Feb 17 '18 at 17:03













    I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

    – Andrew Glukhoff
    Feb 17 '18 at 20:12





    I encountered "Cannot resolve symbol 'AndroidJUnit4'" issue on my espresso test when I changed build variant to "release". As soon as I added this statement (testBuildType "release") to app-level build.gradle (taken from developer.android.com/studio/test/index.html#add_a_new_test) this issue disappeared.

    – Andrew Glukhoff
    Feb 17 '18 at 20:12













    I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

    – Code-Apprentice
    Feb 17 '18 at 20:46





    I formatted your answer the code in your answer. You should add more details, such as the link from your comment and describe where this block goes in your project.

    – Code-Apprentice
    Feb 17 '18 at 20:46











    2














    put this code in your Dependencies



    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })





    share|improve this answer



















    • 1





      When I wrote this question, I had not yet added espresso as a dependency for unit tests.

      – Code-Apprentice
      Jan 4 '17 at 13:08











    • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

      – Tony D
      Jan 13 '17 at 22:11






    • 2





      Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

      – Khay
      Jul 21 '17 at 19:03
















    2














    put this code in your Dependencies



    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })





    share|improve this answer



















    • 1





      When I wrote this question, I had not yet added espresso as a dependency for unit tests.

      – Code-Apprentice
      Jan 4 '17 at 13:08











    • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

      – Tony D
      Jan 13 '17 at 22:11






    • 2





      Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

      – Khay
      Jul 21 '17 at 19:03














    2












    2








    2







    put this code in your Dependencies



    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })





    share|improve this answer













    put this code in your Dependencies



    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
    })






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 4 '17 at 9:10









    paramatma sharan upadhyayparamatma sharan upadhyay

    588




    588








    • 1





      When I wrote this question, I had not yet added espresso as a dependency for unit tests.

      – Code-Apprentice
      Jan 4 '17 at 13:08











    • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

      – Tony D
      Jan 13 '17 at 22:11






    • 2





      Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

      – Khay
      Jul 21 '17 at 19:03














    • 1





      When I wrote this question, I had not yet added espresso as a dependency for unit tests.

      – Code-Apprentice
      Jan 4 '17 at 13:08











    • +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

      – Tony D
      Jan 13 '17 at 22:11






    • 2





      Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

      – Khay
      Jul 21 '17 at 19:03








    1




    1





    When I wrote this question, I had not yet added espresso as a dependency for unit tests.

    – Code-Apprentice
    Jan 4 '17 at 13:08





    When I wrote this question, I had not yet added espresso as a dependency for unit tests.

    – Code-Apprentice
    Jan 4 '17 at 13:08













    +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

    – Tony D
    Jan 13 '17 at 22:11





    +1 I actually figured it out right before I saw this, but yes, this is what I did and it solved my problem.

    – Tony D
    Jan 13 '17 at 22:11




    2




    2





    Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

    – Khay
    Jul 21 '17 at 19:03





    Follow the two answers below from @sepehr . AndroidJunit4 class is not in espresso package insted its present in android.support.test.runner package. and all you have to do is include @Runwith(AndroidJunit4.class) is write in above/for test case present in src/Androidtest/java , INSTED OF including in test cases written under src/test/java.

    – Khay
    Jul 21 '17 at 19:03











    2














    If anyone still having this issue:




    Cannot resolve symbol 'AndroidJUnit4'




    and using API 27, in the build.gradle which is in the app module, add the following lines:



    testImplementation 'junit:junit:4.12'

    // AndroidJUnitRunner and JUnit Rules
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'

    // Espresso dependencies
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'





    share|improve this answer






























      2














      If anyone still having this issue:




      Cannot resolve symbol 'AndroidJUnit4'




      and using API 27, in the build.gradle which is in the app module, add the following lines:



      testImplementation 'junit:junit:4.12'

      // AndroidJUnitRunner and JUnit Rules
      androidTestImplementation 'com.android.support.test:runner:1.0.2'
      androidTestImplementation 'com.android.support.test:rules:1.0.2'

      // Espresso dependencies
      androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'





      share|improve this answer




























        2












        2








        2







        If anyone still having this issue:




        Cannot resolve symbol 'AndroidJUnit4'




        and using API 27, in the build.gradle which is in the app module, add the following lines:



        testImplementation 'junit:junit:4.12'

        // AndroidJUnitRunner and JUnit Rules
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test:rules:1.0.2'

        // Espresso dependencies
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'





        share|improve this answer















        If anyone still having this issue:




        Cannot resolve symbol 'AndroidJUnit4'




        and using API 27, in the build.gradle which is in the app module, add the following lines:



        testImplementation 'junit:junit:4.12'

        // AndroidJUnitRunner and JUnit Rules
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test:rules:1.0.2'

        // Espresso dependencies
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 24 '18 at 4:12









        Randika Vishman

        4,71723958




        4,71723958










        answered Apr 7 '18 at 22:04









        tm81tm81

        814




        814























            0














            Adding



            compile com.android.support.test:runner:0.5'


            resolved this exact issue for me.






            share|improve this answer
























            • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

              – Code-Apprentice
              Mar 31 '18 at 16:40











            • Is androidTestCompile same as compile?

              – donlys
              Apr 1 '18 at 20:04











            • compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

              – Code-Apprentice
              Apr 1 '18 at 20:09
















            0














            Adding



            compile com.android.support.test:runner:0.5'


            resolved this exact issue for me.






            share|improve this answer
























            • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

              – Code-Apprentice
              Mar 31 '18 at 16:40











            • Is androidTestCompile same as compile?

              – donlys
              Apr 1 '18 at 20:04











            • compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

              – Code-Apprentice
              Apr 1 '18 at 20:09














            0












            0








            0







            Adding



            compile com.android.support.test:runner:0.5'


            resolved this exact issue for me.






            share|improve this answer













            Adding



            compile com.android.support.test:runner:0.5'


            resolved this exact issue for me.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 31 '18 at 15:53









            donlysdonlys

            187112




            187112













            • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

              – Code-Apprentice
              Mar 31 '18 at 16:40











            • Is androidTestCompile same as compile?

              – donlys
              Apr 1 '18 at 20:04











            • compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

              – Code-Apprentice
              Apr 1 '18 at 20:09



















            • This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

              – Code-Apprentice
              Mar 31 '18 at 16:40











            • Is androidTestCompile same as compile?

              – donlys
              Apr 1 '18 at 20:04











            • compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

              – Code-Apprentice
              Apr 1 '18 at 20:09

















            This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

            – Code-Apprentice
            Mar 31 '18 at 16:40





            This is already stated in the accepted answer. You should also see if there is a more recent version and use that instead.

            – Code-Apprentice
            Mar 31 '18 at 16:40













            Is androidTestCompile same as compile?

            – donlys
            Apr 1 '18 at 20:04





            Is androidTestCompile same as compile?

            – donlys
            Apr 1 '18 at 20:04













            compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

            – Code-Apprentice
            Apr 1 '18 at 20:09





            compile directives name dependencies used in the main app while androidTestComiple names dependencies used for testing. So the correct answer is to use androidTestCompile, not compile.

            – Code-Apprentice
            Apr 1 '18 at 20:09











            0














            As the list of answers demonstrate, this can be caused by a few things. One more for the list:



            I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.



            Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).



            Next, I need to remove the tests from LINT - or at least disable this warning.



            Edit: @Code-Apprentice, the lines that were missing were:



            import org.junit.After;
            import org.junit.Before;
            import org.junit.Rule;
            import org.junit.Test;
            import org.junit.runner.RunWith;


            import static junit.framework.Assert.assertNotNull;
            import static junit.framework.Assert.assertNull;
            import static org.junit.Assert.assertEquals;
            import static org.junit.Assert.assertTrue;


            So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.






            share|improve this answer


























            • Can you show an example test where this happened for you?

              – Code-Apprentice
              Oct 20 '18 at 2:15
















            0














            As the list of answers demonstrate, this can be caused by a few things. One more for the list:



            I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.



            Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).



            Next, I need to remove the tests from LINT - or at least disable this warning.



            Edit: @Code-Apprentice, the lines that were missing were:



            import org.junit.After;
            import org.junit.Before;
            import org.junit.Rule;
            import org.junit.Test;
            import org.junit.runner.RunWith;


            import static junit.framework.Assert.assertNotNull;
            import static junit.framework.Assert.assertNull;
            import static org.junit.Assert.assertEquals;
            import static org.junit.Assert.assertTrue;


            So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.






            share|improve this answer


























            • Can you show an example test where this happened for you?

              – Code-Apprentice
              Oct 20 '18 at 2:15














            0












            0








            0







            As the list of answers demonstrate, this can be caused by a few things. One more for the list:



            I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.



            Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).



            Next, I need to remove the tests from LINT - or at least disable this warning.



            Edit: @Code-Apprentice, the lines that were missing were:



            import org.junit.After;
            import org.junit.Before;
            import org.junit.Rule;
            import org.junit.Test;
            import org.junit.runner.RunWith;


            import static junit.framework.Assert.assertNotNull;
            import static junit.framework.Assert.assertNull;
            import static org.junit.Assert.assertEquals;
            import static org.junit.Assert.assertTrue;


            So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.






            share|improve this answer















            As the list of answers demonstrate, this can be caused by a few things. One more for the list:



            I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.



            Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).



            Next, I need to remove the tests from LINT - or at least disable this warning.



            Edit: @Code-Apprentice, the lines that were missing were:



            import org.junit.After;
            import org.junit.Before;
            import org.junit.Rule;
            import org.junit.Test;
            import org.junit.runner.RunWith;


            import static junit.framework.Assert.assertNotNull;
            import static junit.framework.Assert.assertNull;
            import static org.junit.Assert.assertEquals;
            import static org.junit.Assert.assertTrue;


            So the first error in the file was with @RunWith(AndroidJUnit4.class) at the beginning of my test class.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 20 '18 at 3:33

























            answered Oct 20 '18 at 2:01









            winwaedwinwaed

            6,35762573




            6,35762573













            • Can you show an example test where this happened for you?

              – Code-Apprentice
              Oct 20 '18 at 2:15



















            • Can you show an example test where this happened for you?

              – Code-Apprentice
              Oct 20 '18 at 2:15

















            Can you show an example test where this happened for you?

            – Code-Apprentice
            Oct 20 '18 at 2:15





            Can you show an example test where this happened for you?

            – Code-Apprentice
            Oct 20 '18 at 2:15


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30603487%2fcannot-resolve-symbol-androidjunit4%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Costa Masnaga

            Fotorealismo

            Sidney Franklin