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
android:text="@string/hello" />String string = getString (R. string. hello);
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.
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.
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.
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.
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