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

3

15

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"
    }
}
Ryannryazan answered 13/1, 2023 at 13:17 Comment(2)
Can you maybe provide a minimal reproducible example or at least more details on your Gradle setup that results in the quoted message? – Olivenite
This would not even be a problem if Jetbrains hadn't broken the kotlinOptions.jvmTarget configuration support for kapt in Kotlin 1.8.0 and refuses to fix it: youtrack.jetbrains.com/issue/KT-55947/… – Cladoceran
A
19

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:

  • contributeArguments does a lot of setup
    • args.jvmTarget is null initially
    • compilerOptions.jvmTarget.get() is 17 (compilerOptions in this case is kotlinc task)
    • (compilerOptions as KotlinJvmCompilerOptionsDefault).fillDefaultValues(args) clears args.jvmTarget (was already cleared)
    • compilerOptions.fillCompilerArguments(args) sets args.jvmTarget = compilerOptions.jvmTarget.orNull?.target (wohoo, we have 17!)
  • (compilerOptions as KotlinJvmCompilerOptionsDefault).fillCompilerArguments(args)
    • sets args.jvmTarget = compilerOptions.jvmTarget.orNull?.target (wtf, we are on null again)

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.

Adverbial answered 18/1, 2023 at 11:20 Comment(4)
thanks for looking at this i have "discovered" a similar fix, see updated question – Ryannryazan
iam not sure if this is correct youtrack.jetbrains.com/issue/KTIJ-24311/… – Ryannryazan
Bit more detail would help triage, but it links here so I guess there's no need to duplicate all this text there. Note: this is a bug in KGP which is tracked in the KT project, KTIJ is for the IntelliJ problems, this is a Gradle issue (it reproduces in console). – Adverbial
It is indeed a bug in KGP, but Jetbrains refuses to fix (so far). Guess it's just a matter of how annoyed Kotlin devs get over it: youtrack.jetbrains.com/issue/KT-55947/… To me it sounds like they ran into an issue during their 1.8.0 refactoring, and thought the easy way out would be to break kotlinOptions.jvmTarget support in kapt. – Cladoceran
R
1

I have updated my version Android Studio to

Android Studio Giraffe | 2022.3.1 Canary 1
Build #AI-223.4884.69.2231.9486165, built on January 13, 2023
Runtime version: 17.0.5+0-17.0.5b653.23-9410051 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
    ide.images.show.chessboard=true

Non-Bundled Plugins:
    com.android.aas (3.5.1)

and upgraded to gradle

plugins {
    id 'com.android.application' version '8.1.0-alpha01' apply false
    id 'com.android.library' version '8.1.0-alpha01' 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
}

tasks.register('clean') {
    delete rootProject.buildDir
}

and gradle wrapper

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

all these actions have resulted in the resolution of my issue

Ryannryazan answered 19/1, 2023 at 15:21 Comment(0)
P
0

I fixed this problem this way: in project module build.gradle I have this plugins:

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

Those plugins just for information. I didn't do anything with them.
In app module build.gradle
I didn't touched this compileOptions

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

I Just added this annotation

        kotlin {
                // Or shorter:
                jvmToolchain(8)
        }

And commented this annotation

//    kotlinOptions {
//        jvmTarget = '1.8'
//    }

And in settings.gradle I added this code in the middle of file:

plugins {
    id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0'
}

Found this pieces of code in different sources

Perfuse answered 27/4, 2023 at 14:55 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.