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

1

16

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.

Galleass answered 9/2, 2017 at 5:51 Comment(0)
B
6

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.

Bingo answered 9/2, 2017 at 6:7 Comment(3)
hanks but I want to check in gradle file, not in java code. I checked that link but not worked for these gradle tasks.Galleass
Thanks for your help. Found solution from #21755213Galleass
its my pleasureBingo

© 2022 - 2024 — McMap. All rights reserved.