Java – How do I change certain values in a .java file from build.gradle?

How do I change certain values in a .java file from build.gradle?… here is a solution to the problem.

How do I change certain values in a .java file from build.gradle?

My app comes in many flavors. For the former.
Development, phases, production style.

Therefore, dev will point to development server URLs, development application IDs, and so on, as well as for staging and production.
For the application IDs present in my strings.xml, I can replace them with the following code –

variant.mergeResources.doLast {
        File valuesFile = file("${buildDir}/res/all/${variant.dirName}/values/values.xml")
        println("Replacing app id in " + variant.dirName)
        String content = valuesFile.getText('UTF-8')
        def appid;
        String variantDirName = variant.dirName;
        if (variantDirName.contains("dev")) {
            appid = '1234_dev'
        } else if(variantDirName.contains("stage")) {
            appid = '1234_stage'
        } else if(variantDirName.contains("prod")) {
            appid = '1234_prod'
        } else {
            appid = '1234_unknown'
        }
        content = content.replaceAll(/some_app_id/, appid)
        valuesFile.write(content, 'UTF-8')
    }

Where is my strings.xml –

    <string name="app_id">some_app_id</string>

The server url is now stored as a constant in my Config.java file

public static final String BASE_URL = "http://dev.blahblah.com";

So the question is how to change this line in Config.java from my build.gradle file for different styles?

Solution

You can use BuildConfig like Xavier. Example:

Product taste

productFlavors {
    develop {
        applicationId "com.dexode.cree.develop"
        ext.enableCrashlytics = false

buildConfigField "String", "API_URL", "\"https://api.test.dexode.com/2.3\""
    }
    beta {
        applicationId "com.dexode.cree.beta"
        ext.enableCrashlytics = true

buildConfigField "String", "API_URL", "\"https://api.dexode.com/2.3\""
    }
    production {
        ext.enableCrashlytics = true
        buildConfigField "String", "API_URL", "\"https://api.dexode.com/2.3\""
    }

Now your BuildConfig looks like:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.dexode.cree.develop.debug";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "develop";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0.0.0_debug";
   Fields from product flavor: develop
  public static final String API_URL = "https://api.test.dexode.com/2.3";
}

and usage in code:

RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(BuildConfig.API_URL);

Remember to refresh the contents of the file BuildConfig. AndroidStudio loves caching files. You can click File -> to sync

Documentation about Flavors

Related issues< a href="https://stackoverflow.com/questions/22506290/buildconfigfield-depending-on-flavor-buildtype" rel="noreferrer noopener nofollow">buildConfigField depending on flavor + buildType

Related Problems and Solutions