Friday, October 17, 2014

Inconsistent Working Directory for Unit Tests Triggered from Command-Line Gradle and from Android Studio


With the introduction of the Android Studio, Gradle also gets popular as the default build system. However, there is some inconsistency to run Gradle from the command line and from the Android Studio interface which is quite annoying. Take unit tests in a mult-project for example. A unit test in a subproject accesses a file through a relative path. If triggered from the Android Studio interface menu (right click and run the test), the path is relative to the root project. However, if triggered by the Gradle from the command line, it is relative to the subproject. The default behavior is quite confusing. Fortunately, it is possible to set the working directory thanks to the reply by Jake Wharton. Just go to Run/Configurations and select the Defaults/JUnit. The above figure shows the settings, including the working directory. It is default to the root project. Changing it to $MODULE_DIR$ recovers the inconsistency.

No comments:

Post a Comment