javaandroidgradlekapt

How to configure kapt to generate Java17 Java stubs in Android Gradle build file


My current Android project is displaying the following build messages:-

> Task :shared:resource:kaptGenerateStubsProductionDebugKotlin
'compileProductionDebugJavaWithJavac' task (current target is 17) and 'kaptGenerateStubsProductionDebugKotlin' task (current target is 1.8) jvm target compatibility should be set to the same Java version.
By default will become an error since Gradle 8.0+! Read more: https://kotl.in/gradle/jvm/target-validation
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain

How do you configure kapt to generate stubs in a specific version of java?

I have tried...

tasks.withType(org.jetbrains.kotlin.gradle.tasks.KaptGenerateStubs).configureEach {
    kotlinJavaToolchain.jdk.use(
            "/usr/local/opt/openjdk@17/libexec/openjdk.jdk/Contents/Home",
            JavaVersion.VERSION_17
    )
}

and

kapt {
    javacOptions {
        option("--target", 17)
    }
}

none of which made any difference

Is it possible to control the java version for stubs generated by kapt in an Android project?

Having enabled verbose logging in kapt3 I can now see I have configure target correctly

Javac options: {--target=17, --source=17}

and in addition

[INFO] All Javac options: {-Adagger.fastInit=enabled=-Adagger.fastInit=enabled, -Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true=-Adagger.hilt.android.internal.disableAndroidSuperclassValidation=true, -Adagger.hilt.android.internal.projectType=LIBRARY=-Adagger.hilt.android.internal.projectType=LIBRARY, -ASomeKaptArgument=ArgumentValue=-ASomeKaptArgument=ArgumentValue, -Akapt.kotlin.generated=/Users/frank/github/mobile-android-practiceupdate/shared/resource/build/generated/source/kaptKotlin/productionDebug=-Akapt.kotlin.generated=/Users/frank/github/mobile-android-practiceupdate/shared/resource/build/generated/source/kaptKotlin/productionDebug, -Adagger.hilt.internal.useAggregatingRootProcessor=false=-Adagger.hilt.internal.useAggregatingRootProcessor=false, --target=17, --source=17, -proc:=only, accessInternalAPI=true,.....

however I still see the above build message

why is kapt ignoring my javacOptions?

To recreate this issue:-

Main project gradle

plugins {
    id 'com.android.application' version '8.0.0-alpha11' apply false
    id 'com.android.library' version '8.0.0-alpha11' apply false
    id 'org.jetbrains.kotlin.android' version '1.8.0' apply false
    id 'com.google.dagger.hilt.android' version '2.44.2' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle wrapper

#Tue Oct 25 07:38:32 BST 2022
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

Gradle JDK

enter image description here

then use kapt for say room or hilt in any app or sub module in your project with java version set to 17

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
        id 'dagger.hilt.android.plugin'
        id "org.jetbrains.kotlin.kapt"
    }
    
    android {
        kapt {
            javacOptions {
                option("--target", "17")
            }
        }
        kotlinOptions {
            jvmTarget = '17'
            freeCompilerArgs += [
                    "-Xcontext-receivers",
                    "-opt-in=androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi",
                    "-opt-in=kotlinx.coroutines.ObsoleteCoroutinesApi"]
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_17
            targetCompatibility JavaVersion.VERSION_17
            coreLibraryDesugaringEnabled true
        }
        namespace 'com.my.app'
        compileSdk 33
        buildToolsVersion "33.0.1"
    
        defaultConfig {
            applicationId "com.my.app"
            minSdk 26
            targetSdk 33
            versionCode 3
            versionName "3.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            vectorDrawables {
                useSupportLibrary true
            }
        }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions "default"
    productFlavors {
        development {
            dimension "default"
        }
        staging {
            dimension "default"
        }
        production {
            dimension "default"
        }
    }
     buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.4.0-dev-k1.8.0-33c0ad36f83'
    }
    packagingOptions {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.0'

    implementation 'org.conscrypt:conscrypt-android:2.5.2'

    implementation 'com.google.dagger:hilt-android:2.44.2'
    kapt 'com.google.dagger:hilt-android-compiler:2.44.2'
    kapt 'androidx.hilt:hilt-compiler:1.0.0'

}

these are my gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx8192m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.defaults.buildfeatures.buildconfig=true
kapt.verbose=true
# positive value will enable caching
# use the same value as the number of modules that use kapt
kapt.classloaders.cache.size=5

# disable for caching to work
kapt.include.compile.classpath=false
kapt.incremental.apt=false

the version of android studio is

Android Studio Flamingo | 2022.2.1 Canary 11
Build #AI-222.4459.24.2221.9445173, built on December 30, 2022
Runtime version: 17.0.4.1+0-17.0.4.1b469.62-9127311 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.6.1
GC: G1 Young Generation, G1 Old Generation
Memory: 4096M
Cores: 12
Metal Rendering is ON
Registry:
    external.system.auto.import.disabled=true
    ide.text.editor.with.preview.show.floating.toolbar=false
    gradle.version.catalogs.dynamic.support=true
    ide.images.show.chessboard=true

Non-Bundled Plugins:
    com.android.aas (3.5.1)
    com.jetbrains.kmm (0.5.1(222)-30)

I have created this bug report with attached example android project

UPDATE

by adding this to my project level build.gradle file the messages disappeared:-

subprojects {
    tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
        kotlinOptions.jvmTarget = "17"
    }
}

Solution

  • Repro

    (note: the Google issuetracker doesn't allow downloading the ZIP, did you check some checkbox there to hide it? There's nothing secret in the zip 😉)

    So, using the example provided in the semi-related Gradle issue I was able to reproduce with gradlew assemble.

    Investigation

    At this point I'm looking for how kapt task gets the Java version, because it should pick up kotlinOptions { jvmTarget = '17' } without question.

    Source code

    For this I'll need sources, which are not available when using .gradle files. Konversion of a build.gradle file is necessary so that Android Studio imports the JARs on the plugin classpath under "External Libraries > Kotlin Script dependencies". This means we can browse the sources of Kotlin, AGP, Gradle. So I changed the root build.gradle to build.gradle.kts (which imports all related plugins) so it reads:

    plugins {
        id("com.android.application") version "8.0.0-alpha11" apply false
        id("org.jetbrains.kotlin.android") version "1.8.0" apply false
    }
    

    (Tip: one AGP plugin declaration is enough to lock in the version of all com.android.* plugins.)

    Reading code

    The problematic task is kaptGenerateStubsDebugKotlin, so the task class name should contain GenStub in it, and indeed there's org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask, which extends KotlinCompile, hello old friend! KaptGenerateStubsTask.setupCompilerArgs looks like a good place to start.

    Debug setup

    At this point it's easier to read the code while executing as well to see the values, so I started a debugging session:

    gradlew cleanKaptGenerateStubsDebugKotlin assemble --no-daemon -Dorg.gradle.debug=true
    

    Then in Android Studio > Run > Edit Configurations... > + > Remote JVM Debug (defaults are good) > OK. And then select the just-created "Unnamed" run config and press the green bug (Debug) button.

    Debugging notes

    Behavior of setupCompilerArgs:

    Conclusion

    So at this point we know that the problem is that Kapt's jvmTarget doesn't inherit from the global kotlinOptions, this sounds like a KGP bug that only manifests with different targets as in your repro.

    I was curious where this is set, and searching for jvmTarget.set yielded AndroidProjectHandler which sounds pretty relevant. Looking at the calls to wireExtensionOptionsToCompilation they only set up the kotlinCompile, not the kapt task.

    Workaround

    Armed with this knowledge the only option I see is

    // TODO remove this once https://youtrack.jetbrains.com/issue/KT-.... is fixed
    tasks.withType(org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask).configureEach {
        kotlinOptions.jvmTarget = '17'
    }
    

    (If you don't want to reference internal classes use the Kapt tasks's superclass KotlinCompile instead; that will configure more though, not just the problematic part.)

    Report

    Please report this to YouTrack! Only JetBrains is able to fix this at the moment. Sadly I don't have high hopes for a fix, because Google is in the process of taking over the Android part of KGP (early stages); fingers crossed they won't reimplement the same problem.

    Fix

    Following the suggestion from JetBrains, this will also solve the issue, however it forces users to use the same JDK and target bytecode version:

    kotlin {
        jvmToolchain(17)
    }
    

    Note

    Kotlin 1.8 added new DSL for setting these things, but only to tasks, it's not supporting the module level yet, see https://kotlinlang.org/docs/whatsnew18.html#limitations and the big green note above that section.