This content originally appeared on DEV Community and was authored by Kiolk
Very often, we should store secrets that we need to build android application. One of the most common cases are storing key alias, key password and store password, that are need to build production release. It is not problem when you develop application in solo in your own private repository. If your team grows to two developers, or you want to move the project to open source, you should store this secrets outside of version control.
The best candidate for this is Gradle local.properties file, that doesn't track by git by default. In this file, you can store key-value pairs by very simple syntax. In our example, it looks like this:
#sign configuration
key_alias=SomeAlias
key_password=SomeKeyPassword
store_password=SomeStorePassword
After, you can use it for signing configuration in build.gradle.kts
file, you need only read these values and store in variables:
val localProperties = Properties()
val localPropertiesFile = rootProject.file("local.properties")
localProperties.load(FileInputStream(localPropertiesFile))
val aliasKey: String = localProperties.getProperty("key_alias")
val passwordKey: String = localProperties.getProperty("key_password")
val passwordStore: String = localProperties.getProperty("store_password")
Late, you can simply use it in places where they need:
signingConfigs {
create("release") {
keyAlias = aliasKey
keyPassword = passwordKey
storePassword = passwordStore
storeFile = rootProject.file("keystore/release.keystore")
}
}
If you need to use these secrets in code, you can simply store it in variables of BuildConfig . But this way is not very secure, because they will be visible after revers engineering of your application.
buildConfigField("String", "PRIVATE_ACCESS_TOKEN", "\"${privateAccessToken}\""
It is all. After this simple manipulation, you can feel itself safety. Also, I like to add information about required local variables in README with pointing where you can find it for saving time of developer who will join to projec.
This content originally appeared on DEV Community and was authored by Kiolk
Kiolk | Sciencx (2024-08-13T21:29:54+00:00) Simple way to store secrets in Android Project.. Retrieved from https://www.scien.cx/2024/08/13/simple-way-to-store-secrets-in-android-project/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.