访问位于android中src文件中app文件夹中的属性文件 [英] access Properties file located in app folder in src file in android
问题描述
我想从模块根目录访问属性文件,但未能找到任何合适的示例来访问属性文件,因为许多示例仅说明如何从 android 中的资产进行访问.我的应用结构如下,
I want to access a property file from a module root, but failed to find any suitable example to access the property file as many only tells how to access from asset in android. My app structure is as follows,
├── app
│ ├── key.properties <--It is my property file, which contains app keys,to be used in app
│ └── src <-- Here i want to access the key.properties to access some keys
├── build.gradle
├── gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── local.properties
推荐答案
Mike M 是正确的,因为您应该使用 gitignore 将文件从 git 中隐藏,这样它就不会被推送到您的存储库中.
Mike M is correct in that you should use gitignore to hide your file from git so it won't be pushed on your repository.
为了扩展一点,这里有一个解决方案,让 gradle 在构建期间加载您的属性,并通过 BuildConfig 将它们提供给您的应用.
To expand a bit on this, here's a solution to have gradle load your properties during the build, and make them available to your app via BuildConfig.
1/app/build.gradle
在android { ... }
插件前,添加:
Properties props = new Properties()
try {
props.load(file('keys.properties').newDataInputStream())
} catch (Exception ex) {
throw new GradleException("Missing keys.properties file.");
}
2/app/build.gradle
在您的 buildTypes
配置中,添加
In your buildTypes
config, add
buildTypes {
debug {
buildConfigField "String", "KEY_MY_PROP", "\"${props.getProperty("myPropKey")}\""
}
release {
buildConfigField "String", "KEY_MY_PROP", "\"${props.getProperty("myPropKey")}\""
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
3/应用中的任何类
您的密钥可作为 BuildConfig.KEY_MY_PROP
这篇关于访问位于android中src文件中app文件夹中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!