Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use resValue?

Tags:

android

gradle

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?

like image 678
BArtWell Avatar asked Jan 23 '15 11:01

BArtWell


People also ask

What is BuildConfig in Android?

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 .

How do I get value from 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.

What can I do with gradle?

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.

What is manifestPlaceholders?

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"] }


1 Answers

For Integer You Have to Use Like

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

Or

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")

like image 151
Mallikarjuna Avatar answered Oct 09 '22 19:10

Mallikarjuna