Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a release test apk for Android with Gradle?

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?

like image 743
ReactiveMax Avatar asked Sep 26 '16 13:09

ReactiveMax


People also ask

How do you generate a release APK?

Create a Signed APK FileCreate the project in Android Studio. Select Build > Signed Bundle/APK from the toolbar menu. Configure the settings for your APK file and possibly create a new key store and key.

How do you run a test case using gradle command?

You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.


2 Answers

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
like image 74
Alireza A. Ahmadi Avatar answered Oct 14 '22 17:10

Alireza A. Ahmadi


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

like image 23
KnowIT Avatar answered Oct 14 '22 16:10

KnowIT