javaandroidandroid-studio

Cannot Create Testing Folders In Android Studio


I have a project where I have created both a Java and an Android library.

Within the Android library, a testing folder was created, see below:

enter image description here

However, I want to add testing in the main application module, not the Android library. The main application was code provided by someone else, and the testing folders were not added.

I originally tried copying and pasting the folders, but now I am seeing that has caused problems as my new testing folders are not indexing. In other words, I cannot access any outside classes, and the Android Studio / IntelliJ editor is not responding:

enter image description here

enter image description here

Full Source Code:https://github.com/troy21688/ud867


Solution

  • Android gradle setup follows the standard maven folder structure, i.e, src/main/java, src/test/java, etc...

    Your production code will go into src/main/java,
    Your JVM unit test code goes into src/test/java,
    Your tests that needs Android device goes into src/androidTest/java.

    These are standard folder names. Gradle be default recognizes these folder names. If you need to provide your own folder names (you should try to avoid these), you need to add the below config in your build.gradle.

    android {
        sourceSets {
            androidTest.java.srcDirs += "src/androidEndpointTest/java"
        }
    }
    

    Now, inside the src/main/java or src/androidTest/java you can create any Java file you want. But you cant have the same Java file with the same package inside both src/main/java and src/androidTest/java. For example, you can't have src/main/java/com/example/SomeFile.java and src/androidTest/java/com/example/SomeFile/java. This is because Gradle will merge these folders during running tests, and you can't have two files with same name in the same folder.