Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I want to execute tests on both build types.

But currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with: android { ... testBuildType "release" }

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

Is it possible to make "testBuildType" conditional ? So that according to build variant in gradle task (connectedDebugAndroidTest and connectedReleaseAndroidTest), it will execute tests on that build.

like image 547
Khushbu Shah Avatar asked Feb 09 '17 05:02

Khushbu Shah


1 Answers

I am not sure but this is worked for me. If you want to execute code according to building variable (debug and released) in app then you can do by using following code.

This is for Activity java file.

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

If you want to check in build.gradle file then do by 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
}

For detail please check this, this and this stackoverflow answer.

I hope you will get your solution.

like image 148
Shailesh Avatar answered Sep 23 '22 22:09

Shailesh