I need to calculate integer value in build.gradle and then use it in my Java code. I try:
build.gradle:
android {
defaultConfig {
resValue "int", "MY_VAR_NAME", "123"
}
}
preprocess.xml in values directory:
<integer name="my_int_value">MY_VAR_NAME</integer>
And I get an error Cannot resolve symbol MY_VAR_NAME
.
How to use it? Is there manual?
BuildConfig is a class containing static constants that is included in every Android application. BuildConfig includes some default fields such as DEBUG and FLAVOR but you can also add custom values via build. gradle .
If you're creating the original value using resValue then you don't have to read it back from resources. You already have the value in your build. gradle script. Just assign it to a local variable and use it to create a resource for each variant.
Gradle makes it easy to build common types of project — say Java libraries — by adding a layer of conventions and prebuilt functionality through plugins. You can even create and publish custom plugins to encapsulate your own conventions and build functionality.
If you need to insert variables into your AndroidManifest.xml file that are defined in your build.gradle file, you can do so with the manifestPlaceholders property. This property takes a map of key-value pairs, as shown here: android { manifestPlaceholders = [hostName:"www.example.com"] }
resValue "integer", "MY_VALUE", "123"
Define your Values in gradle.properties
file , Like this !
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# Your Values
MY_VALUE="123"
MY_VALUE1="124"
MY_VALUE2="125"
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
#http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Access from Your App build.gradle
file resValue "integer", "my_value", (project.findProperty("MY_VALUE") ?: "0")
resValue "integer", "my_value1", (project.findProperty("MY_VALUE1") ?: "0")
resValue "integer", "my_value2", (project.findProperty("MY_VALUE2") ?: "0")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With