[英]Kotlin Annotation processor + AutoService
我遇到了以下问题:-创建了一些实现Component
类的模块,并用@AutoService(Component::class)
注释-我的Android应用正在使用ServiceLoader
来检索这些类。 但是由于某些原因, kapt
没有在META-INF/services/...
生成文件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"
}
我的应用程序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类,它将立即生成它。 任何猜测为什么?
我做了一个变通办法以使其工作(不感到骄傲),我必须实例化自己的Processor并重写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);
}
}
任何更好的建议都欢迎!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.