Search code examples
javaandroidkotlinversion

Java string concatenation operation fails with "symbol not found: StringConcatFactory.makeConcatWithConstants"


I have the following Java snippet:

public static DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption getRadioOptionFromList(List items, int position) {
    Object item = items.get(position);
    if(item instanceof DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption) {
        return (DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption) item;
    } else if(item instanceof com.google.gson.internal.LinkedTreeMap) {
        return new DetailChecklistFormDescriptor.ChecklistItemRadioField.RadioOption(
                (String) ((com.google.gson.internal.LinkedTreeMap) item).get("radioKey"),
                (String) ((com.google.gson.internal.LinkedTreeMap) item).get("radioValue")
        );
    }
    throw new IllegalArgumentException("Could not deserialize radio option from list" + Arrays.toString(items.toArray())); // <-- !!!
}

Specifically this line:

"Could not deserialize radio option from list" + Arrays.toString(items.toArray())

I'm in the process of upgrading Gradle to 8.2, Compose to 1.5.0, Kotlin to 1.9.0 (this feels like a possible culprit), Android Gradle Plugin to 8.1, and had to make updates to the Java/Kotlin JVM target.

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_17 // <-- was 1.8
}
kotlinOptions {
    jvmTarget = "17" // <-- was 1.8
    languageVersion = '1.9'
}

Now I can obviously replace the code with

    throw new IllegalArgumentException(new StringBuilder()
        .append("Could not deserialize radio option from list")
        .append(Arrays.toString(items.toArray()))
        .toString());

Same in other Java code that has String concatenation

        throw new IllegalArgumentException(new StringBuilder()
            .append(getClass())
            .append(" cannot deserialize to ")
            .append(typeOfT)
            .toString());

But it seems somewhat eerie that ALL string concatenation operations done in Java with the + operator would suddenly just "start failing".

Any clues what needs to be configured in order to prevent this error?

error: cannot find symbol, 

symbol: method makeConcatWithConstants(Lookup,String,MethodType,String), 

location: interface StringConcatFactory

Solution

  • Setting a higher source compatibility (JavaVersion.VERSION_1_9) allows for the code to compile.

    Afterwards, it is important to enable core library desugaring, as Android supports Java 8 from SDK 26, and Java 9+ only at higher API levels, so core library desugaring must be enabled.

    Result is:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_9
        targetCompatibility JavaVersion.VERSION_17
    
        coreLibraryDesugaringEnabled true
    }
    kotlinOptions {
        jvmTarget = "17"
        languageVersion = '1.9' 
    }
    
    dependencies {
        coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'