Build string into Android .apk but keep out of source control - android

How do I take an environment variable at build-time and make it available as R.string.api_key at app runtime?

A typical pattern is to put the string in gradle.properties:
API_KEY=whatever-it-is
If you really want it to be a string resource, you can then use resValue in build.gradle:
defaultConfig {
// other stuff here
resValue "string", "api_key", API_KEY
}
(as values in gradle.properties get exposed as global variables to your Gradle script)
Or, if you need the value in Java code, you could use buildConfigField:
defaultConfig {
// other stuff here
buildConfigField "String", "API_KEY", '"'+API_KEY+'"'
}
then reference it as BuildConfig.API_KEY.
And, of course, do not check gradle.properties into version control.

Related

Is it possible "def" value in build.gradle file

I am generating multiple apk's based on different config files.
So i defined a variable on build.gradle file
def isPRODBuild = false
I want to change this variable in productFlavors scope, so i did following.
productFlavors {
dev {
isPRODBuild= false
}
prod {
isPRODBuild= true
}
}
But it seems like that its not changing the value of isPRODBuild value.
Do in need to use some other type of variable in build.gradle file?
At build time, Gradle generates the BuildConfig class so your app code
can inspect information about the current build. You can also add
custom fields to the BuildConfig class from your Gradle build
configuration file using the buildConfigField() method and access
those values in your app's runtime code. Likewise, you can add app
resource values with resValue().
So you could simply use buildConfigField to define your variable.
productFlavors {
dev {
buildConfigField "boolean", "isPRODBuild", "false"
}
prod {
buildConfigField "boolean", "isPRODBuild", "true"
}
}
Then to access the aforementioned variable simply use BuildConfig.isPRODBuild

Is here anyway to access gradle ext properties in java code?

This is project level gradle.build file:
buildscript {
ext {
server_test = 'http://192.618..'
server_main = "https://68.5..."
another_value = "test"
}
ext.kotlin_version = '1.3.41'
....
....
I'm using these values in my app level gradle.build file.
Is there ay way to access these variables in my Java Project files also!.
if you are trying to explicitly access gradle extensions in java code, the answer is no: it's not possible, see this : Android - Read build.gradle properties inside class, and also see this Is it possible to declare a variable in Gradle usable in Java?
using buildConfigFields in gradle (app level, not root level), you can :)
buildConfigField "String", "variable_name", "variable_value"
as a complete example :
productFlavors {
dev {
dimension "your_dimension_name"
buildConfigField "String", "SERVER_TEST", "\"http://192.618..\""
buildConfigField "String", "SERVER_MAIN", "\"https://68.5...\""
buildConfigField "String", "ANOTHER_VALUE", "test"
}
you can then access these variables throughout your project by using :
BuildConfig.SERVER_TEST
Have a look at this link for a more complete answer if this didn't help :
How to generate buildConfigField with String type

In Android Gradle build.gradle What is "it" in it.buildConfigField?

I have search Gradle official website and Android Developer official website, but could not find an answer to this.
In android build.gradle, what is the "it." in the below buildConfigField method? Is this an instance of an object?
I found that moving the buildConfigField method up to defaultConfig lets me use the method without the "it."
What is the difference between the two? Why might I use one method over the other?
android {
...
defaultConfig {
...
buildConfigField 'String', 'API_KEY', MyApiKey
}
buildTypes {
release {
...
}
}
buildTypes.all {
...
}
buildTypes.each {
...
it.buildConfigField 'String', 'API_KEY', MyApiKey
}
}
Gradle build scripts are enhanced Groovy scripts. When you see "it" in a Groovy script or a Gradle build script, it represents the object that was passed into a closure. In your example, the closure is the "{...}" that was passed to "each". So, it iterates through the "buildTypes" collection (or something that is iterable), passes each entry to the closure, and you referenced the passed object as "it". You can change the name of the object passed to the closure, but it's "it" by default.

Automatically set local IP Address in build.gradle

I'm devolping an android app that heavily uses Rest Services.
For developing and debugging, I run the Rest server locally (on my notebook). At home, I have static IP Adresses and therefore, I can put
a static String in my build.gradle.
But if I work from somewhere else, I always have to check my notebook's ip address and edit my build.gradle.
Now I'm curious: Is there a way to insert the current local IP address into my build.gradle automatically?
android {
...
buildTypes {
debug {
...
resValue "string", "host_name", "192.168.0.102" // <--- should be set automatically
}
release {
...
resValue "string", "host_name", "example.com/rest/"
}
}
You can use Groovy methods to find the local IP address:
resValue "string", "host_name", InetAddress.localHost.canonicalHostName
Alternatively you also could use hostAddress instead of canonicalHostName.
I'm adding an awser because I'll probably need this in the future
In build.graddle of the project add into default config the buildConfigField line :
defaultConfig {
applicationId "com.foo.bar"
minSdkVersion 25
targetSdkVersion 25
buildConfigField 'String', 'IP_LOCAL_SERVER', "\"${InetAddress.localHost.hostAddress}\""
}
Then in you code you can call
localIP = BuildConfig.IP_LOCAL_SERVER;

How to use a property from gradle.properties inside android activity?

How to use a property from gradle.properties inside android activity? Whenever I build the code it throws error. Is there a particular way that the properties can be accessed inside the activities?
You can't access the gradle.properties from your Activity.
The gradle.properties is used while you are building the app, it doesn't exist in runtime or inside your apk.
However you can set some values inside the BuildConfig class from your build.gradle script (reading from the gradle.properties for example).
Just use somenthing like:
buildConfigField "boolean", "MY_FLAG", "true"
buildConfigField "String" , "MY_KEY" , "\"XXXXX-XXXXX-XXX\""
in gradle.properties:
SIMPLE_STRING=ABC
in build.gradle:
android {
buildTypes {
debug {
buildConfigField 'boolean', 'PREPROD', 'true'
buildConfigField "String" , "MY_KEY" , SIMPLE_STRING
}
}
}
you can use buildConfigField
for example:
android {
buildToolsVersion "22.0.1"
defaultConfig {
buildConfigField "String", "USED_TOOLS", "\"${android.getBuildToolsVersion()}\""
}
}
will add field USED_TOOLS to your application BuildConfig.java
public static final String USED_TOOLS = "22.0.1";

Categories

Resources