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

Time:01-18

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"
    }
}

CodePudding user response:

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

  • Related