2

我正在构建一个视图活页夹,它使用 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)
    }

}

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

4

1 回答 1

0

我有同样的问题。每次我运行构建时都没有生成任何内容。我发现问题出在我的注释处理器模块的 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
}

于 2019-01-23T23:25:40.547 回答