blay
blay

Reputation: 464

Get data from gradle at runtime

We develop some modules and use the same connector class. Mow we use at static data. calls to server is different between the application and between develop/production.

I have some questions:

  1. Is it is possible to get data from gradle at runtime?

  2. Is it possible to generate/set data at variable at my classes when gradle builds the module (by use at build types) and gradle.properties?

Upvotes: 0

Views: 1076

Answers (2)

Dodge
Dodge

Reputation: 8307

you can also use buildConfigField in your build.gradle

buildTypes {
    release {
        // ...

        buildConfigField "boolean", "CHROMECAST", "false"
        buildConfigField "boolean", "NOTIFICATION_COVER", "false"
        buildConfigField "String", "API_URL", "\"https://your/production/url/\""
    }
    debug {
        // ...

        buildConfigField "boolean", "CHROMECAST", "false"
        buildConfigField "boolean", "NOTIFICATION_COVER", "true"
        buildConfigField "String", "API_URL", "\"https://your/development/url\""
    }
}

in your app code you can access these variables via e.g. BuildConfig.CHROMECAST or BuildConfig.API_URL

for resources you can create specific resources for your debug build in src/debug/res e.g. a special app icon or override the app name.

Upvotes: 5

Konrad Krakowiak
Konrad Krakowiak

Reputation: 12365

I think that productFlavors is exactly what you want:

To use them you have to add to you build.gradle in your module the code below :

android{
    ...
    productFlavors {
        test1 {
            applicationId "your.package.application.test1"
        }
        test2 {

        }
    }
    ...
}

After then you have to create folder in src folder with the same name as build variants: for example for test1 you have to create test1 folder.

In the next step, you have to add res folder to test1 folder and for example add values/strings.xml

To test you can add:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Your app 2</string>
</resources>

Now when you choose test1 build variant your application will has name "Your app 2"

To change build variants you have to use tasks or you can set it in view below:

enter image description here

Upvotes: 0

Related Questions