[FIXED] Define buildConfigField for androidTest

Issue

I’m defining a particular field in the BuildConfig for getting the URL during runtime. So, for each build type, I use a different string:

    prod {
        buildConfigField "String", "BASE_URL", "\"abc.com\""
    }

    debug {
        buildConfigField "String", "BASE_URL", "\"efg.com\""
    }

Is it possible to define a different URL while running the android tests? I tried putting this setting under sourceSets->androidTest, but it’s not accepted.

Solution

You have to pass it as a parameter to connectedAndroidTest task.

android {
    ...
    buildTypes {
        prod {
            buildConfigField "String", "BASE_URL", "\"${getBaseUrl("abc.com")}\""
        }
        debug {
            buildConfigField "String", "BASE_URL", "\"${getBaseUrl("efg.com")}\""
        }
    }
}

def getBaseUrl(String fallback) {
    return project.hasProperty("base_url") ? project.getProperties().get("base_url") : fallback
}

Then passing parameters via -P:

./gradlew connectedDebugAndroidTest -Pbase_url="xxx.com"
./gradlew connectedProdAndroidTest  -Pbase_url="yyy.com"

Answered By – azizbekian

Answer Checked By – Gilberto Lyons (FixeMe Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *