3

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?

3
  • 1
    The project structure in Gradle Android plugin is different from Ant Android structure. Tests are within project in src/test/java directory, not in another project. Do you have tests in another project? Commented Aug 23, 2013 at 7:22
  • My application and junit test for it are two different projects (if I understand your question correctly) Commented Aug 23, 2013 at 7:29
  • @user2172741 Then you have to add that project's jar as compiletest dependency. Commented Aug 26, 2013 at 7:50

1 Answer 1

2

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' } } } 
Sign up to request clarification or add additional context in comments.

7 Comments

I don't think that teamleader will accept such actions with source code. May be there is another way to compile test apk file? Or may be run it by some way with link to main project folder?
@user2172741 You can change the directory of test source files. Check the update. But remember 'convention over configuration'!
Grzegorz, the chapter "23.12.1. Test execution" says "Tests are executed in a separate JVM, isolated from the main build process.". As I understand, I can't run UI tests on device or emulator by this way, can I?
@user2172741 When your are compiling Android project you use android plugin that executes instrumented tests on emulator or device. It is different from standard Java plugin of Gradle. tools.android.com/tech-docs/new-build-system/…
thanks. Now I can build both project and test apk files (as you recommended, there is only one project with default folders), but I can not sign test apk file (so I can not test release version of project) and running tests says that there are 0 tests TBD
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.