简体   繁体   中英

Android Studio Test Folder not appearing for Unit Test Artifact

The androidTest appeared on my Android Studio when the Test Artifact was switched to Android Instrumentation Test. However, when the Test Artifact was switched back to Unit Test. The entire androidTest folder disappear. This project was a migration from Eclipse. I have multiple flavor for my project.

Gradle.build

sourceSets {
    main {
       // manifest, java, renderscript, aidl, assets, res detail etc etc
    }
    flavour1 {
       // manifest, java, renderscript, aidl, assets, res detail etc etc
    }
    flavour2 {
       // manifest, java, renderscript, aidl, assets, res detail etc etc
    }
    flavour3 {
       // manifest, java, renderscript, aidl, assets, res detail etc etc
    }
    flavour4 {
       // manifest, java, renderscript, aidl, assets, res detail etc etc
    }
    androidTest.setRoot('tdd/test')
    androidTest {
         java.srcDirs = ['tdd/test/java']
    }
}

I don't understand why it disappear for Unit Test but appear for Android Instrumentation Test.

I believe this is the answer. I managed to create the test folder even in Unit Test Artifact. When there are multiple variants of the application, Gradle doesn't know the project structure causing it to lose the ability to detect Unit Test and Instrumentation Test. So in order to implement Unit Test, add test method within the android method in build.gradle; as for Android Instrumentation Test, add androidTest method within the android method in build.gradle;

Example:

build.gradle

sourceSets {
     test.setRoot('anydir') // for Unit Test
     androidTest.setRoot('anydir') // for Android Instrumentation Test
}

The above code is only needed if Android Studio is unable to recognised your test folder since you have multiple variants and you changed the directory of your java code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM