Android 抽象处理器未生成类

Android 抽象处理器未生成类,android,annotations,kotlin-android-extensions,kotlinpoet,Android,Annotations,Kotlin Android Extensions,Kotlinpoet,我正在构建一个视图绑定器,它使用KotlinPoet为我的视图生成一些样板代码。但不知何故,我的注释处理器没有生成视图所需的代码,因此每当我尝试运行演示应用程序时,就会抛出ClassNotFoundException 这是我的处理器 @AutoService(Processor::class) @SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedOptions(KAPT_KOTLIN_GENERATED) class Proces

我正在构建一个视图绑定器,它使用KotlinPoet为我的视图生成一些样板代码。但不知何故,我的注释处理器没有生成视图所需的代码,因此每当我尝试运行演示应用程序时,就会抛出
ClassNotFoundException

这是我的处理器

@AutoService(Processor::class)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedOptions(KAPT_KOTLIN_GENERATED)
class Processor : AbstractProcessor() {
    private lateinit var helper: ProcessorHelper


    override fun getSupportedSourceVersion(): SourceVersion {
        return SourceVersion.latest()
    }

    override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
       // if (!roundEnv.processingOver()) {
            //Find all the classes that uses annotations
            val typeElements: Set<TypeElement> =
                ProcessingUtils.getTypeElementsToProcess(roundEnv.rootElements, annotations)

            //Create a wrapper for each such class
            for (typeElement in typeElements){
                this.helper = ProcessorHelper()
                val typeName : String = typeElement.simpleName.toString()


                val packageName : String = processingEnv.elementUtils.getPackageOf(typeElement).qualifiedName.toString()
                //Use the package name and the type name to form a class name
                val className = ClassName(packageName, typeName)

                //Get the generated class name
                val generatedClassName = ClassName(packageName, NameSpaceStore.getGeneratedClassName(typeName))

                //Define the wrapper class
                val classBuilder = TypeSpec.classBuilder(generatedClassName)
                    .addModifiers(KModifier.PUBLIC)
                    .addAnnotation(Keep::class.java)

                //Add Constructor
                classBuilder.addFunction(FunSpec.constructorBuilder()
                    .addModifiers(KModifier.PUBLIC)
                    .addParameter( NameSpaceStore.Variable.ANDROID_ACTIVITY, className)
                    .addStatement("%N(%N)", NameSpaceStore.Method.BIND_ONCLICKS,
                        NameSpaceStore.Variable.ANDROID_ACTIVITY)
                        .addStatement("%N(%N)", NameSpaceStore.Method.BIND_ONCLICKS,
                            NameSpaceStore.Variable.ANDROID_ACTIVITY)
                        .build())

                //Add method that map the with with the ID
                val bindViewMethodBuilder = helper.with(className).
                    privateFunctionBuilder(NameSpaceStore.Method.BIND_VIEWS,NameSpaceStore.Variable.ANDROID_ACTIVITY)

                for (variableElement in ElementFilter.fieldsIn(typeElement.enclosedElements)){
                    val bindView = variableElement.getAnnotation(BindView::class.java)
                    if (null != bindView){
                        //Start finding every views
                        bindViewMethodBuilder.addStatement("%N.%N = (%T)%N.findViewById(%L)",
                            NameSpaceStore.Variable.ANDROID_ACTIVITY,
                            variableElement.simpleName.toString(),
                            variableElement,
                            NameSpaceStore.Variable.ANDROID_ACTIVITY,
                            bindView.value
                        )

                    }
                }

                //Finally build methods
                classBuilder.addFunction(bindViewMethodBuilder.build())

                //Add method that attaches onClickListener()
                val androidClickClassName = ClassName(NameSpaceStore.Package.ANDROID_VIEW,
                    NameSpaceStore.Class.ANDROID_VIEW,
                    NameSpaceStore.Class.ANDROID_VIEW_ONCLICK_LISTENER)

                //Map to Android view class name
                val androidViewClassName = ClassName(NameSpaceStore.Package.ANDROID_VIEW,
                    NameSpaceStore.Class.ANDROID_VIEW)

                val bindOnClickMethodBuilder = helper.with(className).privateFunctionBuilder(NameSpaceStore.Method.BIND_ONCLICKS,
                    NameSpaceStore.Variable.ANDROID_ACTIVITY/*, Modifier.FINAL*/)


                for (executableElement in ElementFilter.methodsIn(typeElement.enclosedElements)){
                    val onClick = executableElement.getAnnotation(OnClick::class.java)
                    if (onClick !=null){
                        val onClickListenerClass = TypeSpec.anonymousClassBuilder()
                            .addSuperinterface(androidClickClassName)
                            .addFunction(
                                helper.with(androidViewClassName).publicFunctionBuilder(NameSpaceStore.Method.ANDROID_VIEW_ONCLICK,
                                    NameSpaceStore.Variable.ANDROID_VIEW)
                                .addStatement("%N.%N(%N)", NameSpaceStore.Variable.ANDROID_ACTIVITY,
                                    executableElement.simpleName.toString(),
                                    NameSpaceStore.Variable.ANDROID_VIEW)
                                .returns(Void::class.java)
                                .build())

                        bindOnClickMethodBuilder.addStatement("%N.findViewById(%L).setOnClickListener(%L)",
                            NameSpaceStore.Variable.ANDROID_ACTIVITY, onClick.id, onClickListenerClass)
                    }
                }
                classBuilder.addFunction(bindOnClickMethodBuilder.build())

                //Write the define file to java file
                val file = File(KAPT_KOTLIN_GENERATED)

                try{

                    FileSpec.builder(packageName, generatedClassName::class.java.name)
                        .addType(classBuilder.build())
                        .build()
                        .writeTo(System.out)

                }catch (io : IOException){
                    processingEnv.messager.printMessage(Diagnostic.Kind.ERROR, io.toString(), typeElement)
                }
          //  }

        }

        return true
    }



    override fun getSupportedAnnotationTypes(): MutableSet<String?>  {
        return mutableSetOf(BindView::class.java.name,
            Keep::class.java.name, OnClick::class.java.name)
    }

}
@AutoService(处理器::类)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@支持选项(KAPT_KOTLIN_生成)
类处理器:AbstractProcessor(){
私有lateinit变量帮助程序:ProcessorHelper
覆盖getSupportedSourceVersion():SourceVersion{
返回SourceVersion.latest()
}
覆盖有趣的过程(注释:MutableSet,roundEnv:RoundEnvironment):布尔值{
//如果(!roundEnv.processingOver()){
//查找所有使用注释的类
val typeElements:集=
ProcessingUtils.getTypeElementsToProcess(roundEnv.rootElements,注释)
//为每个这样的类创建一个包装器
for(类型元素中的类型元素){
this.helper=ProcessorHelper()
val typeName:String=typeElement.simpleName.toString()
val packageName:String=processingEnv.elementUtils.getPackageOf(typeElement).qualifiedName.toString()
//使用包名和类型名形成类名
val className=className(packageName,typeName)
//获取生成的类名
val generatedClassName=ClassName(packageName,NameSpaceStore.getGeneratedClassName(typeName))
//定义包装器类
val classBuilder=TypeSpec.classBuilder(generatedClassName)
.addModifiers(KModifier.PUBLIC)
.addAnnotation(Keep::class.java)
//添加构造函数
classBuilder.addFunction(FunSpec.constructorBuilder()
.addModifiers(KModifier.PUBLIC)
.addParameter(NameSpaceStore.Variable.ANDROID_活动,类名)
.addStatement(“%N(%N)”,NameSpaceStore.Method.BIND\u ONCLICKS,
NameSpaceStore.Variable.ANDROID_活动)
.addStatement(“%N(%N)”,NameSpaceStore.Method.BIND\u ONCLICKS,
NameSpaceStore.Variable.ANDROID_活动)
.build())
//添加用ID映射的方法
val bindViewMethodBuilder=helper.with(className)。
privateFunctionBuilder(NameSpaceStore.Method.BIND_视图、NameSpaceStore.Variable.ANDROID_活动)
for(ElementFilter.fieldsIn(typeElement.enclosedElements)中的variableElement){
val bindView=variableElement.getAnnotation(bindView::class.java)
如果(null!=bindView){
//开始查找每个视图
bindViewMethodBuilder.addStatement(“%N.%N=(%T)%N.findViewById(%L)”,
NameSpaceStore.Variable.ANDROID_活动,
variableElement.simpleName.toString(),
可变元素,
NameSpaceStore.Variable.ANDROID_活动,
bindView.value
)
}
}
//最后构建方法
classBuilder.addFunction(bindViewMethodBuilder.build())
//添加附加onClickListener()的方法
val androidClickClassName=ClassName(NameSpaceStore.Package.ANDROID_视图,
NameSpaceStore.Class.ANDROID_视图,
NameSpaceStore.Class.ANDROID\u VIEW\u ONCLICK\u LISTENER)
//映射到Android视图类名
val androidViewClassName=类名(NameSpaceStore.Package.ANDROID_视图,
NameSpaceStore.Class.ANDROID_视图)
val bindOnClickMethodBuilder=helper.with(className).privateFunctionBuilder(NameSpaceStore.Method.BIND_ONCLICKS,
NameSpaceStore.Variable.ANDROID_ACTIVITY/*,Modifier.FINAL*/)
for(ElementFilter.Methods中的executableElement(typeElement.enclosedElements)){
val onClick=executableElement.getAnnotation(onClick::class.java)
如果(onClick!=null){
val onClickListenerClass=TypeSpec.anonymousClassBuilder()
.addSuperinterface(androidClickClassName)
.addFunction(
with(androidViewClassName).publicFunctionBuilder(NameSpaceStore.Method.ANDROID\u VIEW\u ONCLICK,
NameSpaceStore.Variable.ANDROID_视图)
.addStatement(“%N.%N(%N)”,NameSpaceStore.Variable.ANDROID\u活动,
executableElement.simpleName.toString(),
NameSpaceStore.Variable.ANDROID_视图)
.returns(Void::class.java)
.build())
bindOnClickMethodBuilder.addStatement(“%N.findViewById(%L).setOnClickListener(%L)”,
NameSpaceStore.Variable.ANDROID_活动,onClick.id,onClickListenerClass)
}
}
classBuilder.addFunction(bindOnClickMethodBuilder.build())
//将define文件写入java文件
val文件=文件(生成的KAPT_KOTLIN_)
试一试{
FileSpec.builder(packageName,generatedClassName::class.java.name)
.addType(classBuilder.build())
.build()
.writeTo(系统输出)
}捕获(io:IOException){
processingEnv.messager.printMessage(Diagnostic.Kind.ERROR,io.toString(
apply plugin: 'java-library'
apply plugin: 'kotlin'//make sure you add this line
apply plugin: 'kotlin-kapt'//make sure you add this line

sourceCompatibility = 1.8//version must be 8 in all modules
targetCompatibility = 1.8//version must be 8 in all modules

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    //use kotlin-stdlib-jdk8 (instead of ...-jdk7)
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

    //make sure you include annotation 
    implementation project(':annotations')

    //libraries needed to process the annotated elements
    implementation 'com.squareup:kotlinpoet:1.0.0'//if you are using kotlinpoet
    implementation "com.google.auto.service:auto-service:1.0-rc4"//make sure you add this line
    kapt "com.google.auto.service:auto-service:1.0-rc4"//make sure you add this line
}

apply plugin: 'java-library'
apply plugin: 'kotlin'//make sure you add kotlin plugin

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"//make sure version is 8 everywhere
}

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'// make sure you add this line

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "ir.alirezaisazade.annotationprocessing"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    // make sure you add this line
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    // make sure you add srcDir for generated files
    sourceSets {
        main {
            java {
                srcDir "${buildDir.absolutePath}/generated/source/kaptKotlin/"
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"// make sure version is 8 everywhere
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //like Glide library that needs a dependency to be implemented in our project
    //and an annotation processor to process the annotation of that library
    implementation project(":annotations")//make sure you add this line
    kapt project(":aprocessor")//make sure you add this line
}