dotenv-android
Give access to .env
environment variables file within your Android projects.
dotenv-android
is a simple Gradle plugin that generates a source code file in your Android project. This allows you to reference values in your project's .env
file. This tool was inspired by the twelve-factor app to make environmental changes in your app simple.
Getting started
- In your Android app's
build.gradle
file (probably located atapp/build.gradle
), add the dotenv-android plugin:
plugins {
id "earth.levi.dotenv-android" version "<version-here>"
}
Replace <version-here
with the latest plugin version.
You also need to configure the plugin in your build.gradle
file:
dotenv {
// the package name that is added to the top of your source code file: `import X.Y.Z`
packageName = "com.foo.bar"
// the path to the source code in your Android app module. This is probably `src/main/java` but could be something else like `src/main/kotlin`
sourcePath = "src/main/java/"
}
Tip: See
androidApp/
in this project for an example Android app project that uses the plugin.
- Create a
.env
file in the root level of your Android app project.
Here is an example .env
file:
ENABLE_LOGS=true
API_KEY=XXX-YYY-ZZZ
- In your Kotlin or Java source code of your Android app, reference
.env
values by using this syntax:
val enableLogs = Env.enableLogs == "true"
Env.apiKey
At first, you will see your IDE (Android Studio) complain to you that it cannot find Env.enableLogs
or Env.apiKey
. That's expected behavior because this Gradle plugin has not executed yet. Build your Android app then you should see Android Studio find Env.enableLogs
and Env.apiKey
successfully.
Tip: When you build your app, this Gradle plugin outputs some debug information to help you in case you run into any issues. Like this:
Wrote file to: /code/dotenv-android/androidApp/app/build/generated/source/dotenv/debug/earth/levi/dotenv/Env.java
. Open this file on your computer to view what the Gradle plugin generated.
Development
The easiest way to get started developing this project is to import the root level build.gradle
file using Intellij CE. This project includes a very slim Android app in the androidApp/
directory of this project. It's recommended to import the androidApp/build.gradle
file into Android Studio.
- Get both projects open on your computer using Intellij and Android Studio.
- Write code for the Gradle plugin in Intellij. When you're ready to test out the plugin, run this command in the root level of this project:
./gradlew clean build publishToMavenLocal
Then, perform a Gradle sync in Android Studio for the Android app.
- You're now ready to test the plugin inside of Android Studio. You can perform a build of the Android app within Android Studio to quickly test it.
If you need to debug the plugin, the best way is to enable logging for the Android app compiling.
From the androidApp/
directory, run the command: ./gradlew :app:generateDebugDotenv --debug
. This will enable the log level info (there are many other log levels including --debug). You can read all of your log statements here to debug the plugin.
Deployment
We deploy to the Gradle Plugin Portal to make using the plugin very easy.
-
Register for an account with the Gradle Plugin Portal to get your set of API keys.
-
Set some environment variable secrets for GitHub Actions:
-
GRADLE_PUBLISH_KEY
API key you received after registering with Gradle Plugin Portal. -
GRADLE_PUBLISH_SECRET
API key secret you received after registering with Gradle Plugin Portal. -
REPO_PUSH_TOKEN
- GitHub personal access token withrepos
scope. This allows CI server to push git commits to the GitHub repository.
- Done! The semantic-release tool will now deploy your gradle plugin to the portal every time you deploy a new version of the project.
Author
- Levi Bostian - GitHub, Twitter, Website/blog
Contribute
dotenv-android is open for pull requests. Check out the list of issues for tasks I am planning on working on. Check them out if you wish to contribute in that way.
Want to add features? Before you decide to take a bunch of time and add functionality to the library, please, create an issue stating what you wish to add. This might save you some time in case your purpose does not fit well in the use cases of this project.