Kotlin-dsl-samples: Setting sourceCompatibility and targetCompatibility raise error on Android build project

Created on 18 Sep 2018  路  4Comments  路  Source: gradle/kotlin-dsl-samples

I was trying to integrate kotlin-dsl to my opensource project but apparently I couldn't use

android {
  ...
  compileOptions {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
  }
  ...
}

there as it raises this issue,

Script compilation errors:

  Line 35:     sourceCompatibility = JavaVersion.VERSION_1_8
               ^ Val cannot be reassigned

  Line 36:     targetCompatibility = JavaVersion.VERSION_1_8
               ^ Val cannot be reassigned
bug google

Most helpful comment

The following works:

android {
    compileOptions {
        setSourceCompatibility(JavaVersion.VERSION_1_8)
        setTargetCompatibility(JavaVersion.VERSION_1_8)
    }
}

But this is indeed a little bit wired because normally we can use the property access syntax here 馃
The com.android.build.gradle.internal.CompileOptions (from the AGP) have the following signatures:

    @Nullable
    private JavaVersion sourceCompatibility;

    /** @see #getSourceCompatibility() */
    public void setSourceCompatibility(@NonNull Object sourceCompatibility) {
        this.sourceCompatibility = convert(sourceCompatibility);
    }

    @NonNull
    public JavaVersion getSourceCompatibility() {
        return sourceCompatibility != null ? sourceCompatibility : defaultJavaVersion;
    }

Why isn't that recognized by Kotlin? 馃

Update

Ah - maybe because the set have a different signature as the get.
You can put a Object into the setter but receive a JavaVersion in the getter.
This makes Kotlin not able to use the access syntax here.

All in all.
This is not a Kotlin DSL issue 馃檭

All 4 comments

The following works:

android {
    compileOptions {
        setSourceCompatibility(JavaVersion.VERSION_1_8)
        setTargetCompatibility(JavaVersion.VERSION_1_8)
    }
}

But this is indeed a little bit wired because normally we can use the property access syntax here 馃
The com.android.build.gradle.internal.CompileOptions (from the AGP) have the following signatures:

    @Nullable
    private JavaVersion sourceCompatibility;

    /** @see #getSourceCompatibility() */
    public void setSourceCompatibility(@NonNull Object sourceCompatibility) {
        this.sourceCompatibility = convert(sourceCompatibility);
    }

    @NonNull
    public JavaVersion getSourceCompatibility() {
        return sourceCompatibility != null ? sourceCompatibility : defaultJavaVersion;
    }

Why isn't that recognized by Kotlin? 馃

Update

Ah - maybe because the set have a different signature as the get.
You can put a Object into the setter but receive a JavaVersion in the getter.
This makes Kotlin not able to use the access syntax here.

All in all.
This is not a Kotlin DSL issue 馃檭

Fun fact:
Google have "fixed that issue" with the latest alpha plugin:
https://androidstudio.googleblog.com/2018/09/android-studio-33-canary-11-available.html

_Sent from my Nexus 5X using FastHub_

Thank you for following up @StefMa!

Thanks guys! :)

Was this page helpful?
0 / 5 - 0 ratings