keymusicman
keymusicman

Reputation: 1291

gradle: compile android junit test apk file

Can I compile Android JUnit test apk file by using gradle script? Now my test class is:

    public class main extends ActivityInstrumentationTestCase2<LoginWindow> {
        public main() {
        super("com.tecomgroup.handifox", LoginWindow.class);
        }
        ...
    }

and gradle says that he cannot find class LoginWindow. Should I add the LoginWindow.java to dependencies {} block? Will such test work? Or may be there is another way to compile test apk file?

Upvotes: 3

Views: 2651

Answers (1)

Grzegorz Żur
Grzegorz Żur

Reputation: 49171

When using Gradle Android plugin, you no longer need to have a separate project for testing. Production sources should be put into src/main/java directory, test sources should be in src/instrumentTest/java. The same applies to resources.

From Android Gradle plugin User Guide on project structure.

Project Structure

The basic build files above expect a default folder structure. Gradle follows the concept of convention over configuration, providing sensible default option values when possible.

The basic project starts with two components called “source sets”. The main source code and the test code. These live respectively in:

src/main/
src/instrumentTest/

Inside each of these folders exists folder for each source components. For both the Java and Android plugin, the location of the Java source code and the Java resources:

java/
resources/

For the Android plugin, extra files and folders specific to Android:

AndroidManifest.xml
res/
assets/
aidl/
rs/
jni/

Note: src/instrumentTest/AndroidManifest.xml is not needed as it is created automatically.

You can change the standard project layout

sourceSets {
    instrumentTest {
        java {
            srcDir '../other/src/java'
        }
        resources {
            srcDir '../other/src/resources'
        }
    }
}

Upvotes: 2

Related Questions