Run Unit tests with gradlew and custom JRE

Viewed 445

I am trying to run my project's unit tests using the Gradle wrapper in order to integrate the development with Jenkins. The problem is that I have to run the tests using the JRE option in the GUI to Android-28 but the project is compiled with Android-26 due to some limitations.

How can I select the JRE using the Gradle wrapper? Is there something like:

./gradlew test --api 28
2 Answers

You can create a build variant for testing.

e.g. in you app level gradle file, add a product flavour say jenkins,

buildTypes {
  release {...}
  debug {...}
}

flavorDimensions "version"
productFlavors {
   jenkins {
      dimension "version"
      minSdkVersion 28 
      ... 
   }
}

Then call:

./gradlew testJenkinsDebugUnitTest

For more info refer Test from the command line

Two ways

  1. In gradle.properties in the .gradle directory in your HOME_DIRECTORY set org.gradle.java.home=/path_to_jdk_directory

or:

  1. In your build.gradle

    compileJava.options.fork = true
    compileJava.options.forkOptions.executable = /path_to_javac
    

and if you don't want to depend on concrete path then maybe third approach.

3.) If you add JDK_PATH in **gradle.properties** your build become dependent on on that particular path. Instead Run **gradle task** with following command line parametemer

gradle build -Dorg.gradle.java.home=/JDK_PATH

This way your build is not dependent on some concrete path.

Related