当前位置: 首页 > 知识库问答 >
问题:

升级kotlin或gradle时房间kapt错误

聂煜
2023-03-14

我一直有这个问题:我无法在Android Studio中升级我的应用程序中的gradle版本和kotlin gradle插件,我能想到的唯一解决方案是从头开始一个项目,并在每个类中逐个移动。

我的应用程序使用Room,因此kapt作为注释过程。有了这个版本的kotlin和gradle,一切都完美地工作:

classpath 'com.android.tools.build:gradle:4.0.2'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50'

我的应用 gradle 文件

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
//Kapt is the annotation processor of kotlin, all the generated code Ex. @Room depends of it
apply plugin: 'kotlin-kapt'



kapt {
    generateStubs = true
    correctErrorTypes = true
    mapDiagnosticLocations = true

    javacOptions {
        // Increase the max count of errors from annotation processors.
        // Default is 100.
        option("-Xmaxerrs", 700)
    }
}

android {
    signingConfigs {
        Default {
            ...
        }
    }
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.android.expenses"
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        signingConfig signingConfigs.Default

        javaCompileOptions {
            annotationProcessorOptions {
                arguments += ["room.schemaLocation":
                                      "$projectDir/schemas".toString()]
            }
        }
    }
    sourceSets {
        // Adds exported schema location as test app assets.
        androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
    }
    buildTypes {
        debug {
            ....
        }

        release {
            ....
        }

    }
    
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
        //To avoid duplicate class clash
        exclude group: 'com.google.guava', module: 'listenablefuture'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    //Para parcelize y autobinding in kotlin
    androidExtensions {
        experimental = true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }

    //De databinding, y kotlinx
    /*dataBinding {
        enabled = true
    }*/
    //Substitule to 3 line above with this
    android.buildFeatures.dataBinding true
}

dependencies {
    //version definition
    def room_version = "2.2.5"
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    //androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {

    //    exclude group: 'com.android.support', module: 'support-annotations'

    //})
    testImplementation 'junit:junit:4.12'

    // Required for instrumented tests
    androidTestImplementation 'com.android.support:support-annotations:28.0.0'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'

    //Architecture
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.core:core:1.6.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
    implementation 'com.google.android.material:material:1.4.0'

    implementation 'com.google.code.gson:gson:2.8.6'
    implementation 'joda-time:joda-time:2.7'
    implementation 'org.joda:joda-money:0.10.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.neovisionaries:nv-i18n:1.15'
    implementation 'de.hdodenhof:circleimageview:2.2.0'

    //ColorPicker
    implementation 'com.jaredrummler:colorpicker:1.1.0'

    //Image Cropper
    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.0'

    // Google Sheet API y Google Drive
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'pub.devrel:easypermissions:3.0.0'
    implementation('com.google.api-client:google-api-client-android:1.23.0') {
        exclude group: 'org.apache.httpcomponents'

    }

    // Google Sheet API
    implementation('com.google.apis:google-api-services-sheets:v4-rev504-1.23.0') {
        exclude group: 'org.apache.httpcomponents'

    }

    // Google Drive Rest
    implementation('com.google.apis:google-api-services-drive:v3-rev102-1.23.0') {
        exclude group: 'org.apache.httpcomponents'
    }

    //Google Drive Android
    implementation "com.google.android.gms:play-services-drive:$rootProject.playServices_version"

    //Google Maps
    implementation "com.google.android.gms:play-services-location:$rootProject.playServices_version"
    implementation 'com.google.android.gms:play-services-maps:16.1.0'

    //Google Maps Utils
    implementation 'com.google.maps.android:android-maps-utils:0.5'

    //Google Places
    implementation "com.google.android.gms:play-services-places:$rootProject.playServices_version"


    //MultiViewAdapter para mejores recyclerviews
    implementation 'com.github.devahamed:multi-view-adapter:1.2.6'
    implementation 'com.github.devahamed:multi-view-adapter-databinding:1.2.6'

    //CardView
    implementation 'androidx.cardview:cardview:1.0.0'

    //PagerSlidingTabStrip
    implementation 'com.jpardogo.materialtabstrip:library:1.1.1'

    //Room
    implementation "androidx.room:room-runtime:$room_version"
    //annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - Kotlin Extensions and Coroutines support for Room
    //implementation "androidx.room:room-ktx:$room_version"

    // Test helpers
    androidTestImplementation "androidx.room:room-testing:$room_version"
    kapt "androidx.room:room-compiler:$room_version"

    // Lifecycle components
    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.3.1'
    kapt 'androidx.lifecycle:lifecycle-compiler:2.3.1'

    //Kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

    // Coroutines
    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")

    // RxJava support for Room
    implementation 'androidx.room:room-rxjava2:2.0.0'

    //Android Palette API
    implementation 'androidx.palette:palette:1.0.0'

    //Welcome Activity
    implementation 'com.github.AppIntro:AppIntro:6.1.0'

    //Charts
    implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

    //TableView
    implementation 'com.evrencoskun.library:tableview:0.8.8'

    //Anko Commons Library for easier development
    implementation "org.jetbrains.anko:anko-commons:$anko_version"

    //Search Dialog
    implementation 'com.github.mirrajabi:search-dialog:1.2.4'

    //FAB Button Speed Dial
    implementation 'com.leinardi.android:speed-dial:3.2.0'

    //WorkManager
    implementation('androidx.work:work-runtime-ktx:2.0.1') {
        exclude group: 'com.google.guava', module: 'listenablefuture'
    }

    //PagingList
    implementation 'androidx.paging:paging-runtime:2.1.0'

    //Calendar
    implementation 'com.github.prolificinteractive:material-calendarview:2.0.1'

    //Image Compressing
    implementation 'id.zelory:compressor:3.0.1'

    //New Places: Old one is deprecated
    implementation 'com.google.android.libraries.places:places:2.4.0'

    // Koin for Android
    implementation 'org.koin:koin-android:2.0.0-rc-2'
    implementation 'org.koin:koin-android-viewmodel:2.0.0-rc-2'

    // KTX
    implementation 'androidx.core:core-ktx:1.3.2'

    // Rx
    implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'

    // Moshi
    implementation 'com.squareup.moshi:moshi:1.8.0'
    kapt 'com.squareup.moshi:moshi-kotlin-codegen:1.8.0'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.retrofit2:converter-moshi:2.5.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.14.1'

    // Picasso
    implementation 'com.squareup.picasso:picasso:2.71828'


    //SQLCipher
    implementation "net.zetetic:android-database-sqlcipher:4.4.0@aar"
....

}
repositories {
    mavenCentral()
}

// For Firebase
apply plugin: 'com.google.gms.google-services'  // Google Play services Gradle plugin

我试图更新到每个的当前最新版本,尽管这个问题存在于不太新的版本中

classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.20'

为了进行此更改,我做了以下操作:

  1. 将 Java jdk 更新到 11
  2. 将 kotlin-android-extensions 替换为 'kotlin-parcelize' 和 viewBinding = true
  3. 移除 androidExtensions: experimental=true
  4. 将 parcelize 导入替换为 kotlinx.parcelize.Parcelize

尽管如此,我仍然有这个错误

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

app:kaptDebugKotlin下面有消息,但是它们是对非@Entity类的多个构造函数的警告,不应该引起崩溃

这真的很令人沮丧,因为我不知道还能做什么,也不知道如何获得更多信息(我也用- info运行了一次,但我只从kapt得到了相同的输出)

提前感谢您的时间和帮助

更新

这是我的顶级毕业生

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    //ext.kotlin_version = '1.4.10'
    //Working version
    ext.kotlin_version = '1.3.50'
    //Latest version
    //ext.kotlin_version = '1.5.20'

    //ext.kotlin_version = '1.3.41'

    repositories {
        mavenCentral()
        google()
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
        google()
        //jcenter()
    }
    dependencies {
        // working version, if change modify distributionUrl in gradle-wrapper.properties
        classpath 'com.android.tools.build:gradle:4.0.2'
        // to upgrade
        //classpath 'com.android.tools.build:gradle:7.0.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.10'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }


}



allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
        maven {
            url 'https://maven.google.com/'
            name 'Google'

        }
        //De MaterialChipsInput y Search Dialog
        maven {
            url "https://jitpack.io"
        }
    }
}

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

ext{
    archLifecycleVersion = '1.1.1'
    anko_version='0.10.5'
    support_version = '28.0.0'
    playServices_version = '16.0.0'
}

快速更新,当我尝试仅将房间版本升级到2.3.0时,出现错误:

[kapt] An exception occurred: java.lang.NoSuchMethodError: kotlin.jvm.internal.FunctionReferenceImpl.<init>(ILjava/lang/Object;Ljava/lang/Class;Ljava/lang/String;Ljava/lang/String;I)V
    at androidx.room.log.RLog$CollectingMessager$writeTo$printMessage$1.<init>(RLog.kt)
    at androidx.room.log.RLog$CollectingMessager.writeTo(RLog.kt:92)
    at androidx.room.solver.TypeAdapterStore.findRowAdapter(TypeAdapterStore.kt:520)
    at androidx.room.solver.TypeAdapterStore.findQueryResultAdapter(TypeAdapterStore.kt:459)
    at androidx.room.solver.binderprovider.InstantQueryResultBinderProvider.provide(InstantQueryResultBinderProvider.kt:29)
    at androidx.room.solver.TypeAdapterStore.findQueryResultBinder(TypeAdapterStore.kt:391)
    at androidx.room.processor.DefaultMethodProcessorDelegate.findResultBinder(MethodProcessorDelegate.kt:140)
    at androidx.room.processor.InternalQueryProcessor.getQueryMethod(QueryMethodProcessor.kt:204)
    at androidx.room.processor.InternalQueryProcessor.processQuery(QueryMethodProcessor.kt:135)
    at androidx.room.processor.QueryMethodProcessor$process$1.invoke(QueryMethodProcessor.kt:67)
    at androidx.room.processor.QueryMethodProcessor$process$1.invoke(QueryMethodProcessor.kt:37)
    at androidx.room.processor.Context.collectLogs(Context.kt:133)
    at androidx.room.processor.QueryMethodProcessor.process(QueryMethodProcessor.kt:61)
    at androidx.room.processor.DaoProcessor.process(DaoProcessor.kt:99)
    at androidx.room.processor.DatabaseProcessor.doProcess(DatabaseProcessor.kt:100)
    at androidx.room.processor.DatabaseProcessor.process(DatabaseProcessor.kt:51)
    at androidx.room.DatabaseProcessingStep.process(DatabaseProcessingStep.kt:47)
    at androidx.room.compiler.processing.JavacProcessingStepDelegate.process(XProcessingStep.kt:111)
    at com.google.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:330)
    at com.google.auto.common.BasicAnnotationProcessor.process(BasicAnnotationProcessor.java:181)
    at org.jetbrains.kotlin.kapt3.base.incremental.IncrementalProcessor.process(incrementalProcessors.kt)
    at org.jetbrains.kotlin.kapt3.base.ProcessorWrapper.process(annotationProcessing.kt:147)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.callProcessor(JavacProcessingEnvironment.java:794)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.discoverAndRunProcs(JavacProcessingEnvironment.java:705)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.access$1800(JavacProcessingEnvironment.java:91)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment$Round.run(JavacProcessingEnvironment.java:1035)
    at com.sun.tools.javac.processing.JavacProcessingEnvironment.doProcessing(JavacProcessingEnvironment.java:1176)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1170)
    at com.sun.tools.javac.main.JavaCompiler.processAnnotations(JavaCompiler.java:1068)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:79)
    at org.jetbrains.kotlin.kapt3.base.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:35)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.runAnnotationProcessing(Kapt3Extension.kt:230)
    at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:188)
    at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:99)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:96)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:106)
    at org.jetbrains.kotlin.cli.jvm.compiler.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:81)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:555)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.invoke(KotlinToJVMBytecodeCompiler.kt:82)
    at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:107)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:546)
    at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:177)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:164)
    at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:84)
    at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:42)
    at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:104)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1558)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:323)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$256(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

知道为什么吗?这有关系吗?

解决方案:如果有人在寻找类似的东西,我最终重做了这个项目,这个问题与moshi kapt有关。我把库升级到1.12.0,一切正常

共有3个答案

楮景明
2023-03-14

在//Room注释下方的依赖项中添加以下内容:

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-ktx:$room_version"

并确保您使用的是最新的room_version = [2.3.0]

扈翰
2023-03-14

将房间升级到2.4.0-beta02将解决您的问题。因为它修复了我的。

我们添加了一个新的 TypeConverter 分析器,该分析器将类型中的可空性信息考虑在内。由于此信息仅在 KSP 中可用,因此默认情况下仅在 KSP 中处于打开状态。如果它导致任何问题,可以通过将 room.useNullAwareTypeAnalysis=false 传递给注释处理器来将其关闭。如果发生这种情况,请一个文件错误,因为此标志将在将来被删除。使用此新的 TypeConverter 分析器,建议仅提供非空接收 TypeConverters,因为新分析器能够使用空检查来包装它们。请注意,这对使用 KAPT 或 Java 作为注释处理器的用户没有影响(与 KSP 不同),在类型中没有可空性信息。(Ia88f9, b/193437407)

https://developer . Android . com/jetpack/Android x/releases/room # 2 . 4 . 0-beta 02

米飞龙
2023-03-14

在我的例子中,我在升级“kotlin-gradle-plugin:从1.5.31到1.6.10”的时候也遇到了同样的问题。

我已经通过遵循官方文件将Gradle版本升级到最新版本解决了这个问题。

在Android Studio得到了文件

我认为,当我们更新“kotlin-gradle-plugin”并且其他依赖项不是最新版本时,就会发生这种情况。

此外,请检查您的JDK版本,以便您可以遵循官方文档。

 类似资料:
  • 在添加com.google.dagger:dagger-android-support依赖项后,我得到了这个gradle错误。 这是我的构建的一部分

  • 我想将我的基于vertx的项目移到https://github.com/sczyh30/vertx-blueprint-microservice.git模板。blueprint项目使用annotations@vertxgen等在编译期间生成代码。

  • Android官方文档指出,不建议使用,因为它可能会长时间锁定UI并触发ANR。但是Kotlin协同程序使我们能够在主线程中执行一些操作,而不会有效地阻塞UI。 所以我要问:使用并在主线程上运行的Couroutine作用域中访问数据库是否安全?如下所示: 或者我们应该坚持不允许主线程查询和在另一个线程中执行数据库查询的旧方法? 我这样问是因为前一种方式(使用): 更具可读性(我可以在访问数据库的函

  • 自从升级到Gradle 3.3以来,由于缺少翻译错误,我在构建代码时遇到了麻烦: 错误:xxx没有翻译成“af”(南非荷兰语)、“am”(阿姆哈拉语)、“ar”(阿拉伯语)、“az”(阿塞拜疆)、“az-az”(阿塞拜疆:阿塞拜疆)、“be”(白俄罗斯)、“bg”(保加利亚)、“ca”(加泰罗尼亚)、[……],“zh TW”(中文:台湾)、“zu”(祖鲁)[缺失翻译] 报告的大多数语言都是我的项目

  • 如何购买维护计划? Navicat 软件维护计划让 Navicat 用户在受保护的期间可以得到优先的电子邮件支持、接收软件升级和接收错误修复版本而无需额外的收费。 你可以在购买软件授权完成时或在你的购买日期 90 日内订购维护计划- 它不能在稍后的日期加入到一个以前购买的产品。详细信息,请点击这里。 如何升级你的 Navicat? 如果你想升级已安装的 Navicat 副本到最新版本,请选择 帮助

  • 迁移 Navicat Data Modeler 到新的电脑 在 Navicat Data Modeler,选择“Navicat Data Modeler”->“注册”。 【永久许可证】点击“取消激活”以在线取消激活许可证密钥。 【订阅方案】点击“退出”以退出你的 Navicat ID。 在现有的电脑解除安装 Navicat Data Modeler。 在新的电脑重新安装 Navicat Data