android - AbstractProcessor 不生成类

标签 android annotations kotlin-android-extensions kotlinpoet

我正在构建一个 View 绑定(bind)器,它使用 KotlinPoet 为我的 View 生成一些样板代码。但不知何故,我的注释处理器没有生成 View 所需的代码,因此每当我尝试运行演示应用程序时都会抛出 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)
    }

}

如果您需要有关此问题的更多信息,请告诉我。提前致谢。

最佳答案

我遇到了同样的问题。每次我运行构建时,都没有生成任何东西。 我发现问题出在我的注释处理器模块的 build.gradle 文件上。

因此,如果您有注释处理器模块,请确保检查这些:

注解处理器模块build.gradle

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
}

可能也想检查其他构建文件

注解build.gradle

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
}

应用构建.gradle

确保在主 sourceSet 中添加 srcDir

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
}

关于android - AbstractProcessor 不生成类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54336934/

相关文章:

hibernate - [HV000030 : No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'

java - 创建一个@Stub注释,充当Java中的@Deprecated

android - 使用@Parcelize时如何在数据类中分割构造函数以外的成员变量

安卓工作室 3 : Outdated Kotlin Runtime

android - 添加注解处理器时,Android Studio会引发重复的类错误

android - 是否有适用于 Android 开发人员的 Python 绑定(bind)?

android - React 原生组件在运行时如何作为 View 、ViewGroup 或 Fragments 运行?

java - 如何将 HashMap 值获取到 Android 中 Listview 的 CustomlistAdapter

即使进行了所有优化,Android Emulator 也会延迟

java - Scala:自定义编译器警告