Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Why plus operator causes dupe strings to concat?

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?

like image 795
Jay Soyer Avatar asked Apr 10 '15 15:04

Jay Soyer


People also ask

What is the major difference between concat () method and operator?

+ 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.

Can you use += with strings Java?

In Java, two strings can be concatenated by using the + or += operator, or through the concat() method, defined in the java. lang. String class.

How is concat different from adding strings?

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.

Which operator can be used in string concatenation?

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?

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.

How to concatenate strings in Java?

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:

How to concatenate string from several rows in a Dataframe?

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.


1 Answers

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
like image 78
Kai Avatar answered Sep 28 '22 09:09

Kai