Diaz diaz
Diaz diaz

Reputation: 284

Getting app configuration from a file

I need to get some Config from a file probably an xml file to create build . I can not use buildVarient because build varients are not fixed. Below are the attributes i need to import.

  1. Application ID:- In build.gradle.
  2. BASE URL :- base url for network request.
  3. App Name :- Application name in manifest

Requirement is one apk will be created for one person . SO the build procedure should be simple. Thats why i am thinking about to create a config file and reference all 3 attributes above from file. Is it even possible for all of 3? If yes how? . Please help .

Upvotes: 0

Views: 1835

Answers (1)

deadfish
deadfish

Reputation: 12304

I am using in build.gradle in flavors something like that:

Properties versionProps = new Properties()
versionProps.load(new FileInputStream(file('config.conf')))
def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
def properties_versionName = versionProps['VERSION_NAME']

versionName properties_versionName
versionCode properties_versionCode

Please take a look at 2nd, 3rd and 4th line.

Here is the config.conf file:

VERSION_NAME=1.0.0
VERSION_CODE=100

Both files should be placed in the same folder level.


For setting application names and base url I would use separate project folders, more discussion about it here.


You could also create custom build fields so it would be visible in code by calling BuildConfig.{FIELD}.

defaultConfig {
    ...
    buildConfigField "String", "OS", '"android"'
}

Then the BuildConfig looks like this:

public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("true");
    public static final String APPLICATION_ID = "com.example.app";
    public static final String BUILD_TYPE = "debug";
    public static final String FLAVOR = "";
    public static final int VERSION_CODE = 1;
    public static final String VERSION_NAME = "1.0";
    // Fields from default config.
    public static final String OS = "android";
}

More here.

Upvotes: 3

Related Questions