In my build.gradle
file in Android Studio, I'm creating a new APK file name with the following:
def newApkName = rootProject.name + "-" + variant.versionName + variant.buildType.versionNameSuffix
This is causing the following to print out: app-2.2.0-debug-debug
However if I do the following instead:
def newApkName = rootProject.name + "-" + variant.versionName
newApkName.concat(variant.buildType.versionNameSuffix)
It correctly prints out: app-2.2.0-debug
I'm new to gradle, so I'm very confused why using the plus operator to concat is causing a dupe string to appear...where as using the concat method doesn't. Any insight on why this is?
+ operator could take any type of input and convert it to a string before append to the target string. The concat method would create new string object as output after appending only if output string has length greater than zero otherwise return the same target string as an output object.
In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.
In concat method we are bound to while adding strings as we have to pass the string in the parameter while in other case of '+' operator, it is simply adding up strings likely mathematics, so there is no bound we can add either side. Takes only one argument of string and concatenates it with another string.
Operator + is used to concatenate strings, Example String s = “i ” + “like ” + “java”; String s contains “I like java”.
Why should you be careful about String concatenation (+) operator in loops using Java? Strings are used to store a sequence of characters in Java, they are treated as objects. The String class of the java.lang package represents a String.
In Java, a String is a sequence of characters. There are often programming situations where you will need to concatenate Strings. There are several ways in which you can achieve this. The + operator is also known as the concatenation operator. It can be used to concatenate Strings. The following code demonstrates this:
The abstract definition of grouping is to provide a mapping of labels to the group name. To concatenate string from several rows using Dataframe.groupby (), perform the following steps: Group the data using Dataframe.groupby () method whose attributes you need to concatenate.
If you print out variant.versionName
, you'll see that variant.buildType.versionNameSuffix
has already been appended as suffix, which makes sense given variant.buildType.versionNameSuffix
's function.
The reason why
def newApkName = rootProject.name + "-" + variant.versionName
newApkName.concat(variant.buildType.versionNameSuffix)
produces different result is because Gradle(Groovy) Strings are immutable, same as Java, and since the result of the concat is discarded, newApkName
remains to be the result of rootProject.name + "-" + variant.versionName
only.
Therefore the correct statement should just be:
def newApkName = rootProject.name + "-" + variant.versionName
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