Home > Net >  How to use a module properties file in a Android project
How to use a module properties file in a Android project

Time:09-29

I've got an Android module where we use a .properties file.

I use the content of the properties file in the build.gradle for one of the manifestPlaceholders and in the Kotlin codebase too via BuildConfig.

So in my build.gradle I load the file with:

rootProject.file("src/main/java/com/MyCompany/myModuleName/myProperties.properties").withInputStream { myProperties.load(it) }

Then I load the properties in the BuildConfig and use them with manifestPlaceholders:

android.defaultConfig {
        buildConfigField "String", 'MY_URL', "\"${myProperties.getProperty('MY_URL')}\""

        manifestPlaceholders = [
                "foo": myProperties.getProperty('fooProperty')
        ]
}

When I compile the module, it works fine, I can see the content of the BuildConfig and use the properties in my codebase with:

BuildConfig.MY_URL

The problem is when I use the module in my Android project, gradle complains because it can't find the property file /Users/myUser/pathToProject/android/src/main/java/com/myCompany/myModuleName/myProperties.properties (No such file or directory)

Any ideas? thanks

CodePudding user response:

I don't know why you want to use properties file from module but here it is you can do it

def myFile = new File("$projectDir/src/main/java/com/MyCompany/myModuleName/myProperties.properties")
def myProperties = new Properties()
myProperties.load(new FileInputStream(myFile))

You need to use projectDir instead of rootProject

  • Related