Here is the absolute path of the file:
AppName – Instead of real app name
clientname – Instead of client name
/Users/ha/Documents/AppName/AppName-android/AppName/app/clients/clientname/Build/Build.properties
Absoulte path to Java file where I am trying to read the value.
/Users/ha/Documents/AppName/AppName-android/AppName/app/src/main/java/com/AppName/android/consumer/helper/OneSignalManager.java
Build.Properties has a variable called “OneSignal_ID”. Need to read this value in OneSignalManager.java class.
private void getOneSignalId(){
try {
Properties properties = new Properties();
InputStream fl = new FileInputStream("Build.properties");
properties.load(fl);
String OneSignalID = properties.getProperty("ONESIGNAL_APP_ID");
Log.d("PringLog: OneSignalId",""+OneSignalID);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Please advise!
1
If you didn’t find any solution yet, then try this detailed tutorial.
Fetch values from properties file
The best aproach is to use BuildConfig.
Shortly, you read your build.properties file in your gradle file, read the properties you are interested in and put them to build config.
I have created example application presenting it: https://github.com/luskan/ExampleBuildFileShowApp
In this file ExampleBuildFileShowApp/app/build.gradle.kts, you can see gradle code to read the property and put it in the BuildConfig:
// Prepare path to properties file
val buildProperties = project.layout.projectDirectory.dir(
"clients/clientname/Build/Build.properties"
).asFile
print("buildPropertiesPath: $buildProperties")
// Load properties file
val properties = Properties()
file(buildProperties).inputStream().use { properties.load(it) }
// Get the property
val appId = properties.getProperty("ONESIGNAL_APP_ID")
defaultConfig {
// Add the property to BuildConfig
buildConfigField("String", "ONESIGNAL_APP_ID", ""$appId"")
}
You will also need to add buildFeatures.buildConfig = true
in android {}
section of your gradle file (its in the example code on github).
And in app/src/main/java/biz/progmar/examplebuildfileshowapp/MainActivity.kt is a simple code to read from BuildConfig.java the above property value by logging it:
Log.d("MainActivity", "ONESIGNAL_APP_ID: ${BuildConfig.ONESIGNAL_APP_ID}")