25

I'm setting up Android app structure with Gradle and Android Studio and Espresso UI testing for a project.

No matter what I try, the androidTest folder never appears in AndroidStudio's project structure.

Project (root) build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

App build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'
    }
}

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.0"

    defaultConfig {
        applicationId "es.unizar.vv.mobile.catmdedit.app"
        minSdkVersion 16
        targetSdkVersion 16
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'

            java {
                srcDir 'src/main/java'
            }
            resources {
                srcDir 'src/main/resources'
            }
            res.srcDirs = ['res']
        }

        test.setRoot("test")

        androidTest.setRoot("androidTest")
    }
}

dependencies {
    androidTestCompile 'com.android.support.test:runner:0.2'
    androidTestCompile 'com.android.support.test:rules:0.2'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
}

How project structure looks:

enter image description here

How project structure actually is:

enter image description here

RecuencoJones
  • 2,747
  • 3
  • 22
  • 21

7 Answers7

27

Change the Test Artifact within your Build Variants to Android Instrumentation Tests.

The Build Variants tab can be found in the bottom left side of the Android Studio window above Favorites. Clicking on the tab should open a pane with your modules and build variants for those modules. Above the modules is a Test Artifact dropdown which you should change to Android Instrumentation Tests.

Joel
  • 14,861
  • 3
  • 27
  • 31
  • I did as you said, there is the pane and the app module with build variant either debug or release, test artifact: android instrumentation tests, but in my android project structure the androidTest folder doesn't yet appear. – RecuencoJones May 18 '15 at 09:21
  • I'm not sure then. My suggestion helped me with my similar problem. – Joel May 18 '15 at 09:32
  • 4
    Omg, I'm sorry, actually you were right, I only needed to add this to gradle: `androidTest.setRoot('src/androidTest') androidTest { java{ srcDirs 'src/androidTest/java' } }` And then in build variants > Android Instrumentation Tests it appears! Thank you! – RecuencoJones May 21 '15 at 18:28
  • 9
    This has changed in a later version of Android Studio. The accepted answer isn't applicable any more, the pane isn't there any more. 6uitar6reat6od's answer does work. – Christine Dec 03 '16 at 11:14
20

This was happening for me because I had my current variant set to release. Changing it back to debug and doing a Gradle sync caused androidTest folders to show up again.

Matt Robertson
  • 2,928
  • 5
  • 34
  • 62
17

You just need to add these line in build.gradle file

After doing lots of R&D I found proper solution. Now it's working..

android {
    sourceSets {
        main { java.srcDirs = ['src/main/java'] }
        test { java.srcDirs = ['src/test/java'] }
        androidTest { java.srcDirs = ['src/androidTest/java'] }
    }
}

After adding above code "androidTest" folder is now appearing!!

Shiv Kumar
  • 635
  • 7
  • 13
  • still not generated – Manoj Behera Sep 05 '18 at 05:48
  • 1
    @ManojBehera maybe you have to create the folder yourself. Because this solution did work for me (I had created a *androidTest* folder manually before trying this solution). So after this, I was able to see my *androidTest* folder in Android Studio. – Sufian Mar 26 '19 at 11:17
  • @Sufian That fixed it for me; thanks. :-) – Yasper Apr 08 '22 at 10:00
7

Just in case somebody is still stuck with this issues, I will suggest you just recreate the folders your self which is extremely easy than I thought! here: follow the steps in the photo.enter image description here

don't forget to switch from android to project and like that you will have the same directory tree like in the picture below and simple create the folder and files, after which you can then switch back to android and it will give you the same directory tree as on the right side of the image.

and your ExampleInstrumentedTest class can look like this:

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

}

and the ExampleUnitTest class can look like this:

public class ExampleUnitTest {
    @Test
    int addition(){
        return 60;
    }
}
Wale
  • 1,644
  • 15
  • 31
1

Just open the Gradle pane on the right side.

Under app > Tasks > build double click assembleAndroidTest and it'll generate test packages.

:app:packageDebugAndroidTest
:app:assembleDebugAndroidTest
:app:assembleAndroidTest

Here is it all:

Davide Cannizzo
  • 2,826
  • 1
  • 29
  • 31
eular
  • 53
  • 7
0
androidTest.setRoot("androidTest")

This path is relative to the build.gradle file. Just remove it and gradle will pick your src/androidTest folder automatically as your project follows the default file structure.

Be_Negative
  • 4,892
  • 1
  • 30
  • 33
-1

You just need to add these lines to your build.gradle file :

androidTest.setRoot('src/androidTest')

androidTest  { 
    java { 
        srcDirs 'src/androidTest/java' 
    } 
}
jherran
  • 3,337
  • 8
  • 37
  • 54
Apurv Pandey
  • 217
  • 2
  • 7