Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android How to use string resource in gradle file

On this page, it is written:

versionName — A string used as the version number shown to users. This setting can be specified as a raw string or as a reference to a string resource.

I would like to use a resource as version number. Something like @string/version .

What's the way ?

Thanks

Julien

like image 945
Julien Avatar asked Aug 03 '16 21:08

Julien


People also ask

How do you add string resources?

android:text="@string/hello" />String string = getString (R. string. hello);

What is string resource in Android?

A string resource provides text strings for your application with optional text styling and formatting. There are three types of resources that can provide your application with strings: String. XML resource that provides a single string.

Which tells Android to look up a text value from the string resource file?

Let's start with the first part, @string . This is just a way of telling Android to look up a text value from a string resource file. In our case, Android Studio created a string resource file for us called strings.

What is extract string resource in Android?

In Android Studio while adding TextView to the layout xml file, it is very convenient to use "Extract String Resource" to add string name to strings.


1 Answers

I have create method using @Yaroslav's answer (https://stackoverflow.com/a/37432654/6711554). Here is code of setting package name from string file.

def getPackageName() {
  try {
     def stringsFile = file("./src/main/res/values/string.xml")
     return new XmlParser().parse(stringsFile).string.find { [email protected] 'your_package_name' }.text()
  }catch(e){
     println(e)
     return "default.packagename"
  }
}

and use it like

 applicationId getPackageName()

You can read any string in your gradle from your any resource file.

like image 120
jainish champaneria Avatar answered Oct 16 '22 13:10

jainish champaneria