Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define buildConfigField for androidTest

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.

like image 402
Rajath Avatar asked Nov 25 '15 05:11

Rajath


People also ask

What is buildConfigField?

What is buildConfigField? When you select a build variant at Android studio, it creates a build folder in your app's folder system. It's name is BuildConfig.java (You can see file place at bottom image) BuildConfig.java.

What is the function of the buildConfigField keyword in build gradle?

As I mentioned in above we can use buildConfigField to define custom fields. In this String is a data type and “S3_STORAGE” is the field name. Same as the custom field values we can define resource values inside build. gradle file.

What is BuildConfig DEBUG?

In recent versions of the Android Developer Tools (ADT) for Eclipse, there's a class called BuildConfig which is automatically generated by the build. This class is updated automatically by Android's build system (like the R class), and it contains a static final boolean called DEBUG, which is normally set to true.


1 Answers

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"
like image 102
azizbekian Avatar answered Sep 22 '22 15:09

azizbekian