24

I know, with the Gradle command assembleAndroidTest I can build a test APK. But I can use this test APK only for debug builds of my app, right? If I use it with a release build, I get error messages like "[SDR.handleImages] Unable to find test for com.xxx.xxx (packagename)"

How can I build a test APK in release mode with Gradle?

ReactiveMax
  • 455
  • 1
  • 6
  • 15
  • have u tried changing build varient? did you make realse buil varient? – DevKRos Sep 26 '16 at 13:06
  • 1
    I use Jenkins for building both, the app apk and the test apk. I have no build variants. I use `assembleRelease` for building the app apk and `assembleAndroidTest` to build the test apk. Then I use the Spoon runner to execute the tests on a device. But this only works, when using a debug app apk. If the app apk is a release build, executing the tests does not work. – ReactiveMax Sep 26 '16 at 13:13
  • 1
    I am having exactly the same issue, have you managed to solve this? – Loebre Feb 28 '17 at 16:21
  • 1
    Why would you want to have test APK? – blackHawk May 06 '17 at 19:37
  • 1
    @blackHawk Firebase Test Lab for Android at Google Play allows to launch Android instrumentation tests on physical and virtual devices. But it requires both "App apk" and "Test apk" to be signed by the same release keys. – yvolk May 27 '17 at 19:45
  • Yes I am having the same issue as well, have you managed to resolve this yet? – savi Aug 16 '17 at 21:37
  • Are you looking at testing your application against different end points [server] for develop environment vs actual production environment? If not, then why can't you use debug version of both Application/Test APK? – Rams_QA Sep 12 '17 at 04:56
  • Why don't you use different flavors? You will then get different gradle tasks to assemble the apk you want. – Javier Mendonça Oct 18 '17 at 09:51
  • check this post : https://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle – pathik devani Oct 25 '17 at 06:56

4 Answers4

12

Add this line to your defaultConfig.

android {
    defaultConfig {
        ...
        testBuildType System.getProperty('testBuildType', 'debug')
     }
}

Now you can run below command to get the release version of test apk

./gradlew assembleAndroidTest -DtestBuildType=release
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
8

To build a release test apk from gradle first you need to add followin test config in your build.gradle file:

android {
    ..
    ..
    buildTypes {
      release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-test-rules.pro'
        signingConfig signingConfigs.release
      }
    }
    testBuildType "release"
}

After this you will start to see the assembleReleaseAndroidTest command. Through which you can run tests on release build.

In the process you may also need to create a proguard-test-rules.pro file, for test proguard rules

KnowIT
  • 2,392
  • 1
  • 18
  • 16
1

You can also try

if (project.hasProperty('androidTestRelease')) {
        testBuildType 'release'
    }else {
        testBuildType 'debug'
    }

and to run tests in release build just add

-PandroidTest

to your command

-3

You can Create a Flavor to achieve this.

kukku
  • 489
  • 5
  • 17