How to use resValue?

I need to calculate the integer value in build.gradle and then use it in my Java code. I'm trying to:

build.gradle:

android { defaultConfig { resValue "int", "MY_VAR_NAME", "123" } } 

preprocess.xml in the values ​​directory:

 <integer name="my_int_value">MY_VAR_NAME</integer> 

And I get the Cannot resolve symbol MY_VAR_NAME error message.

How to use it? Is there any instruction?

+6
source share
1 answer

For integers you need to use

resValue "integer", "MY_VALUE", "123"

or

Define your values ​​in the 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 application 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")

+1
source

Source: https://habr.com/ru/post/981439/


All Articles