Is there a safe way to manage API keys?

1. Store api keys in an xml file Put xml file “api_keys.xml” in the directory “res/values/”. api_keys.xml <?xml version=”1.0″ encoding=”utf-8″?> <resources> <string name=”THE_MOVIE_DB_API_TOKEN”>XXXXX</string> </resources> use api keys in java code context.getString(R.string.THE_MOVIE_DB_API_TOKEN); 2. Store API keys with help of Gradle and the gradle.properties file Example_0 Example_1 Add the following line to [USER_HOME]/.gradle/gradle.properties For Windows OS, an … Read more

Define buildconfigfield for an specific flavor AND buildType

Loop the variants and check their names: productFlavors { vanilla {} chocolate {} } applicationVariants.all { variant -> println(“Iterating variant: ” + variant.getName()) if (variant.getName() == “chocolateDebug”) { variant.buildConfigField “boolean”, “VARIABLE”, “true” } else { variant.buildConfigField “boolean”, “VARIABLE”, “false” } }

Android using Gradle Build flavors in the code like an if case

BuildConfig.FLAVOR gives you combined product flavor. So if you have only one flavor dimension: productFlavors { normal { } admin { } } Then you can just check it: if (BuildConfig.FLAVOR.equals(“admin”)) { … } But if you have multiple flavor dimensions: flavorDimensions “access”, “color” productFlavors { normal { dimension “access” } admin { dimension “access” … Read more

How to get the build variant at runtime in Android Studio?

Look at the generated BuildConfig class. public final class BuildConfig { public static final boolean DEBUG = Boolean.parseBoolean(“true”); public static final String APPLICATION_ID = “com.example.app”; public static final String BUILD_TYPE = “debug”; public static final String FLAVOR = “”; public static final int VERSION_CODE = 1; public static final String VERSION_NAME = “”; }

How to set gradle home while importing existing project in Android studio

The gradle plugin (which contains a bundled version of gradle) should be already installed in where/you/installed/android-studio/plugins/gradle so you shouldn’t need to manually download it. Depending on the version of Android Studio, that last directory may be where/you/installed/android-studio/gradle/gradle-x.y.z (x.y.z is the version, so check your filesystem for the exact path). If you intend on doing gradle … Read more