简体   繁体   中英

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?

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

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

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

你可以创建一个 Flavor 来实现这一点。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM