0

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 .

Diaz diaz
  • 284
  • 1
  • 7
  • 21

1 Answers1

3

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.

deadfish
  • 11,996
  • 12
  • 87
  • 136
  • Fair enough . Where should i use `Properties versionProps = new Properties()`? Can you pls add the whole code block . – Diaz diaz Jun 07 '18 at 03:40
  • Also where should i put `config.properties` file . I used assets for it and i am getting `unable to resolve class android.content.res.AssetManager` in build.gradle? – Diaz diaz Jun 07 '18 at 04:45
  • 1
    Ok So i put `config.properties` under `app` folder . Its working in gradlle . Thx to You Bud . How can i access it in a java class ? And yeah Congratulations on 6K mission accomplish . – Diaz diaz Jun 07 '18 at 04:58
  • Can i use this file in assets ? If yes then how will i load it in `build.gradle` – Diaz diaz Jun 07 '18 at 05:26
  • `config.conf` is used only for `build.gradle` at the same folder level. Declared field from `buildConfigField` can be used in code by calling `BuildConfig.{YOUR_FIELD}`. You must firstly rebuild project. – deadfish Jun 07 '18 at 10:46
  • 1
    I used it in assets. Its working . Using `versionProps.load(new FileInputStream(file('/src/main/assets/configs/config.properties')))` . Thx for your support . I need to one question here please look into [this question](https://stackoverflow.com/questions/50733808/how-to-read-a-config-file-which-is-under-app-folder). – Diaz diaz Jun 07 '18 at 10:51