1

I am having a config fie config.properties inside app folder . Which i am using for build configurations in build.gradle. I need to read this file in java code . But i can figure out the path should i use . How can i read this file in java code . code below gives me FileNotFoundException

 try {
        Properties properties = new Properties();
        File inputStream=new File("/config.properties");
        properties.load(new FileInputStream(inputStream));
        return properties.getProperty("BASE_URL");
    }catch (IOException e){
        e.printStackTrace();
    }

I am using the same file in build.gradle and its working well . as below .

 defaultConfig {
    Properties versionProps = new Properties()
    versionProps.load(new FileInputStream(file('config.properties')))
    def properties_versionCode = versionProps['VERSION_CODE'].toInteger()
    def properties_versionName = versionProps['VERSION_NAME']
    def properties_appid= versionProps['APPLICATION_ID']


    applicationId properties_appid
    minSdkVersion 14
    targetSdkVersion 26
    versionCode properties_versionCode
    versionName properties_versionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

build.gradle part is working fine and i am able to assign properties . But i need to read the same file in a java class . What path should i use for File.

Config file looks like:

VERSION_NAME=1.0.0
VERSION_CODE=1
APPLICATION_ID=com.app.drecula
BASE_URL=https://reqres.in/
Diaz diaz
  • 284
  • 1
  • 7
  • 21

2 Answers2

1

Ideally you should put application specific file under assets folder and separate your gradle build configuration and application specific configuration. Add application specific configuration properties file under assets/configs folder. Then you can read it as follows:

  final Properties properties = new Properties();
  final AssetManager assetManager = getAssets();
  final InputStream inputStream= assetManager.open("configs/config.properties");
  properties.load(inputStream);

If you still want to proceed, then only way would be to put the file in assets folder and in your build.gradle use

versionProps.load(new FileInputStream(file('/src/main/assets/config/config.properties')))
Sagar
  • 23,903
  • 4
  • 62
  • 62
  • I said its working in `build.gradle` . I need to read it in java class . – Diaz diaz Jun 07 '18 at 05:58
  • I can not put file in assets . If i put file in assets then how will i load in `build.gradle`? – Diaz diaz Jun 07 '18 at 06:16
  • @Diazdiaz you cannot read file in both. If you want the file to be part of apk and use it, you have to put it in assets. Its not a great idea to use same file for gradle build config and application configuration. You should separate them. – Sagar Jun 07 '18 at 06:18
  • Thats kind of need . I need to have a single file both weather its in `assets` of any where else. is it impossible to have a single file? – Diaz diaz Jun 07 '18 at 06:20
  • @Diazdiaz you can check my answer from this [SO](https://stackoverflow.com/questions/50176769/how-to-set-buildconfigfield-parameters-dynamically-while-building-an-apk-through/50177203#50177203) Using this, you can write content to BuildConfig file and use it in your application – Sagar Jun 07 '18 at 06:21
  • `BuildConfig` file is auto generated and should not edit . I think it will regenerate on each clean-build . SO this is not what i am looking for . – Diaz diaz Jun 07 '18 at 06:30
  • @Diazdiaz Then there is only one solution. Put the file in `assets `folder and in your build.gradle use `versionProps.load(new FileInputStream(file('/src/main/assets/config/config.properties')))` – Sagar Jun 07 '18 at 06:32
  • Yeah that can be an alternative way to do this . Thx Add it in answer . Also please look for the initial question if you find an answer the post it here . – Diaz diaz Jun 07 '18 at 06:44
  • Accepted the answer . But i am still in dilemma why how can i access the file under app folder. If its not possible then why? Let me know if you find answer to this particular question . Thx for your time . – Diaz diaz Jun 07 '18 at 06:55
  • @Diazdiaz you can refer to [this](https://commonsware.com/Android/previews/assets-files-and-data-parsing) post. Basically you cannot access the file directly, unless you either put in res/raw or assets. The local directory on PC and directory for .apk are distinct – Sagar Jun 07 '18 at 06:58
1

Okay, let's back to basis. First You create properties in build.gradle and then You create buildConfigField with custom fields.

productFlavors {
    play {
        dimension "MyDimension"

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

        //...

        buildConfigField "int", "MY_VERSION_CODE", "$properties_versionCode"
        buildConfigField "String", "MY_VERSION_NAME", "\"$properties_versionName\""

    }
}

After You rebuild project, fields will be able to call from BuildConfig.{FIELD}.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //...

    Log.d(TAG, "Version Name: " + BuildConfig.MY_VERSION_NAME);
    Log.d(TAG, "Version Code: " + BuildConfig.MY_VERSION_CODE);

    //...
}

More info here.

deadfish
  • 11,996
  • 12
  • 87
  • 136
  • Is that Simple ? I am so dumb . I have already seen this solution but did not understand . Thanks . Now i get it . Going to write it on Stone . – Diaz diaz Jun 07 '18 at 11:24