简体   繁体   中英

Kotlin Annotation processor + AutoService

I'm having the following problem: - Created few Modules which implement a Component class and it's annotated with @AutoService(Component::class) - My Android app is using ServiceLoader to retrieve those classes. But for some reason kapt is not generating the files inside META-INF/services/...

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

My App build.gradle file:

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()
}

I'm not sure why, but kapt is basically not generating those files. If I use Java classes it generate it instantly. Any guess why?

You could try this

kapt { correctErrorTypes = true }

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

I did a workaround to make it work (not proud of it 😂 ) I had to instantiate my own Processor and override the processingOver to always return false. It seems the kapt kept a cache of my resource and after clean the build it doesn't generate the files anymore.

Here it is the code:

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);
    }
}

Any better suggestion is welcome!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM