Can Gradle tests live peacefully with old style test projects
android, ant, automated-tests, gradle, testing
Solution
do this:
android {
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest {
java.srcDirs = ['tests/src']
resources.srcDirs = ['tests/src']
aidl.srcDirs = ['tests/src']
renderscript.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}
}
}
Then drop your test manifest in `tests/`. Gradle will ignore it, but Ant/ADT can use it.
Create `tests/project.properties` and point the tested project to `../`
In Eclipse, you can open a project that's inside another one, so that shouldn't be a problem.
Problem
So in the olden days, when you wanted to do tests in Android, you used to make a new test project like: ``` RealProject |-- AndroidManifest.xml |-- src | |-- com | | |-- example | | | |-- Foo.java `-- tests `-- AndroidManifest `-- src `-- com `-- example `-- Foo.java ``` Meaning the tests were in a project that lived somewhere, by convention in the tests folder. In the new build system, if you are migrating from an existing android project (and must support developers who are using Eclipse and thus ant), you're suggested by the New Build System User Guide to add the following so it maintains the project structure from the old version. ``` android { sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } instrumentTest.setRoot('tests') } } ``` This is all fine and good until you try to write the tests. Given the above structure, you're meant to put tests in ``` RealProject `-- tests `-- java `-- com `-- example `-- FooTest.java ``` The difference between the two is that Gradle expects the subdirectory in `tests` to be `java` but ant expects it to be `src`. So how can I merge these two systems together. The goal being that I can run unit tests from either Gradle or ant. Gradle seems like it should be more flexible than the ant test system but no combination of things i've tried to put in the sourceSets seems to work. It almost looks like instrumentTest is not a normal sourceSet or something like that. I've tried: - Symlink `tests/java` to `tests/src` - Add a block like `instrumentTest { java.srcDirs = ['src'] }` with trying every combination of the `*.srcDirs` I could make from the main sourceSet example.