我目前的Android项目显示以下的构建信息:
> 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
如何配置kapt以在特定版本的Java中生成存根?
我已经尝试过...
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
)
}
并且
kapt {
javacOptions {
option("--target", 17)
}
}
这些都没有任何影响
在Android项目中,是否可以控制由kapt生成的存根的Java版本?
在启用了kapt3的详细日志记录后,我现在可以看到我已经正确配置了目标
Javac options: {--target=17, --source=17}
另外
[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,.....
然而,我仍然看到上面的构建消息。
为什么kapt忽略了我的javacOptions
?
要重现此问题:
主项目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包装器
#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
然后在您的项目中设置Java版本为17,使用kapt来在任何应用程序或子模块中使用room或hilt。
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'
}
这些是我的 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
Android Studio的版本是:
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)
我已经创建了这个bug 报告,并附上了一个示例android项目。
更新
通过将以下内容添加到我的项目级别的build.gradle
文件中,消息消失了:
subprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions.jvmTarget = "17"
}
}