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

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

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

How to set buildConfigField for module from outside for android?

I am developing in Android, and I have a submodule in my project.
I have set buildConfigField like the following in build.gradle of Module
buildTypes {
release {
minifyEnabled false
buildConfigField "String", "TestId", '"48"'
}
dev {
buildConfigField "String", "TestId", '"88"'
}
}
And I use TestId in module.
But I want to change the TestId from outside, like build.gradle of app.
How to set the buildConfigField of module from build.gradle of app for Android ?
Thanks in advance.
Your question is a little confusing for me. I don't really get your point. But I want to point out. The buildConfigField will generate corresponding field in BuildConfig. And these fields are decorated by final keyword. So they can't be changed.

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

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.

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.

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