【问题标题】:Kotlin Annotation processor + AutoServiceKotlin 注释处理器 + AutoService
【发布时间】:2023-03-04 01:30:01
【问题描述】:

我遇到以下问题: - 创建了几个实现Component 类的模块,并用@AutoService(Component::class) 注释 - 我的 Android 应用程序正在使用 ServiceLoader 来检索这些类。但是由于某种原因kapt 没有在META-INF/services/... 中生成文件

我的模块 gradle.file:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"


    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation project(':common-dependencies')
    implementation project(':component')
    compileOnly 'com.google.auto.service:auto-service:1.0-rc3'
    kapt "com.google.auto.service:auto-service:1.0-rc3"

    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

我的 App build.gradle 文件:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.2"
    defaultConfig {
        applicationId "com.test.sampleapp"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
            }
        }
    }

    flavorDimensions 'required-notused'

    productFlavors {
        brandA {
            resValue "string", "app_name", "Brand A"
        }
        brandB {
            resValue "string", "app_name", "Brand B"
        }

        all { applicationIdSuffix ".${it.name.toLowerCase()}" }
    }

    buildTypes {
        debug

        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'

    implementation project(':common-dependencies')
    implementation project(':component')

    brandAImplementation project(':city-picker')
    brandAImplementation project(':profile')
    brandAImplementation project(':matches')
    brandAImplementation project(':chat')

    brandBImplementation project(':city-picker')
    brandBImplementation project(':chat')

    compileOnly 'com.google.auto.service:auto-service:1.0-rc3'
    kapt "com.google.auto.service:auto-service:1.0-rc3"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}

repositories {
    mavenCentral()
}

我不知道为什么,但kapt 基本上不会生成这些文件。如果我使用 Java 类,它会立即生成它。 猜猜为什么?

【问题讨论】:

    标签: android build module kotlin kapt


    【解决方案1】:

    你可以试试这个

    kapt { correctErrorTypes = true }

    https://kotlinlang.org/docs/reference/kapt.html

    【讨论】:

    • 这适用于已弃用的原始 kapt,将在 Kotlin 1.2 发布后不久删除。
    【解决方案2】:

    我做了一个变通办法让它工作(并不以此为荣?) 我必须实例化自己的处理器并覆盖 processingOver 以始终返回 false。 似乎 kapt 保留了我的资源缓存,并且在清理构建后它不再生成文件。

    代码如下:

    public class CustomAutoServiceProcessor extends AutoServiceProcessor {
    
        public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
            try {
                Field processingOver = roundEnv.getClass().getDeclaredField("processingOver");
                processingOver.setAccessible(true);
                processingOver.set(roundEnv, false);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return super.process(annotations, roundEnv);
        }
    }
    

    欢迎提出更好的建议!

    【讨论】:

    • 这听起来像一个错误。如果您可以在较小的项目中重现此问题,请将其报告给 Kotlin 问题跟踪器 (kotl.in/issue)。
    猜你喜欢
    • 1970-01-01
    • 2020-05-04
    • 2017-05-22
    • 1970-01-01
    • 2018-01-28
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多