override fun run(){ //获取 META-INF/services/javax.annotation.processing.Processor 的依赖 val annotationProcessors = detectAnnotationProcessors( parameters.annotationProcessorClassNames.get(), parameters.annotationProcessorArtifacts.get() ) //输出到CardControl/app/build/intermediates/annotation_processor_list/debug/annotationProcessors.json writeAnnotationProcessorsToJsonFile( annotationProcessors, parameters.annotationProcessorListFile.get().asFile ) } }
const val ANNOTATION_PROCESSORS_INDICATOR_FILE = "META-INF/services/javax.annotation.processing.Processor" const val INCREMENTAL_ANNOTATION_PROCESSORS_INDICATOR_FILE = "META-INF/gradle/incremental.annotation.processors" fun detectAnnotationProcessors( artifacts: Collection<SerializableArtifact> ): Map<SerializableArtifact, Boolean> { // TODO We assume that an artifact has an annotation processor if it contains // ANNOTATION_PROCESSORS_INDICATOR_FILE, and the processor is incremental if it contains // INCREMENTAL_ANNOTATION_PROCESSORS_INDICATOR_FILE. We need to revisit this assumption as the // processors may register as incremental dynamically. val processors = mutableMapOf<SerializableArtifact, Boolean>()
for (artifact in artifacts) { val artifactFile = artifact.file // 文件夹 if (artifactFile.isDirectory) { if (File(artifactFile, ANNOTATION_PROCESSORS_INDICATOR_FILE).exists()) { processors[artifact] = File(artifactFile, INCREMENTAL_ANNOTATION_PROCESSORS_INDICATOR_FILE) .exists() } } elseif (artifactFile.isFile) { //文件 一般都是jar包 try { JarFile(artifactFile).use { jarFile -> //如果包含 META-INF/services/javax.annotation.processing.Processor if (jarFile.getJarEntry(ANNOTATION_PROCESSORS_INDICATOR_FILE) != null) { processors[artifact] = jarFile.getJarEntry( INCREMENTAL_ANNOTATION_PROCESSORS_INDICATOR_FILE ) != null } } } catch (e: IOException) { // Can happen when we encounter a folder instead of a jar; for instance, in // sub-modules. We're just displaying a warning, so there's no need to stop the // build here. See http://issuetracker.google.com/64283041. } } }