Can "testBuildType" be conditional in the build.gradle file of an Android project?

I have 2 types of builds for my application: debug and release.

I want to run tests for both types of assembly.

But only one type of assembly is currently being tested. By default, this is the Build debugging type, but it can be reconfigured with: android {... testBuildType "release"}

I want to execute connectedDebugAndroidTest and connectedReleaseAndroidTest one by one without changing the gradle file.

Is it possible to make "testBuildType" conditional? So according to the build option in gradle, the task (connectedDebugAndroidTest and connectedReleaseAndroidTest), it will run tests on this assembly.

+5
source share
1 answer

I'm not sure, but it worked for me. If you want to execute the code according to the line (debug and release) in the application, you can do this with the following code.

This is for java activity file.

public void printMessage() { if (BuildConfig.DEBUG) { //App is in debug mode } else { //App is released } } 

If you want to check the build.gradle file, run the following code.

First way

 buildTypes { debug { buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"' } release { buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"' } mezzanine.initWith(buildTypes.release) mezzanine { buildConfigField "String", "SERVER_URL", '"http://stage.this-is-so-fake.com"' } } 

Second way

 android { testBuildType obtainTestBuildType() } def obtainTestBuildType() { def result = "debug"; if (project.hasProperty("testBuildType")) { result = project.getProperties().get("testBuildType") } result } 

See, this and this answer stackoverflow for more details.

I hope you get your decision.

+2
source

Source: https://habr.com/ru/post/1264000/


All Articles