Kotlin编译器找不到的生成的Java源代码

时间:2020-05-05 21:43:46

标签: java android kotlin code-generation gradle-plugin

我正在编写gradle插件来生成Java代码。我的插件生成的代码基于Android R类,因此它取决于

processVariantResources

任务。在我的插件中,我正在使用以下代码执行此操作:

class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {

    val extension = project.extensions.create<MyPluginExtension>(PLUGIN_NAME, MyPluginExtension::class.java)
    project.tasks.whenTaskAdded { task ->
        if (task.name.startsWith("process") && task.name.contains("Resources") && !task.name.contains("Test")) {
            val index = task.name.indexOf("Resources")
            val variantName = task.name.substring(7, index)

            //Create my task and add it to the project, make it dependent on processResources
            project.task("my${variantName}ResourceTask") {
                it.doLast {
                    generateSomeCodeForVariant(project, extension, variantName)
                }
                taskList.add(it)
            }.dependsOn(task)
        }
    }

    project.afterEvaluate {
            val appExtension = project.extensions.findByType(AppExtension::class.java)
            appExtension?.applicationVariants?.all { variant ->
                val myTask = project.tasks.getByName("my${variant.name.capitalize()}ResourcesTask")
                    val outputDir = "${project.buildDir}/generated/source/myplugin/${variant.name}"

                    //register my task as java generating
                    variant.registerJavaGeneratingTask(myTask, File(outputDir))
            }
    }
}

然后,在我使用此插件的项目的build.gradle中,我已添加

android {
    sourceSets {
        main {
            java.srcDirs += ['build/generated/source/myplugin']
            kotlin.srcDirs += ['build/generated/source/myplugin']
        }
    }
}

我的插件实际上生成了目录的源代码:

build/generated/source/myplugin/com/mygroup/myartifact

无论如何,代码会正确生成,并放置在正确的位置,但是我无法让编译器识别我生成的代码。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我重构了代码并实现了此处描述的策略:

我的新代码如下:

import sys
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *

class LoadingScreen(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setFixedSize(200,200)
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.CustomizeWindowHint)

        self.label_animation = QLabel(self)
        self.movie = QMovie("loading.gif")
        self.label_animation.setMovie(self.movie)

        timer = QTimer(self)
        self.startAnimation()
        timer.singleShot(20000, self.stopAnimation)

        self.show()

    def startAnimation(self):
        self.movie.start()

    def stopAnimation(self):
        self.movie.stop()
        self.close()

class demo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Loading Overlay with Selenium Problem")
        self.resize(500, 500)
        self.center()
        self.twitter_icon = QtWidgets.QLabel("")
        self.twitter_icon.setAlignment(Qt.AlignCenter)
        self.pixmap = QtGui.QPixmap("twitter.png")
        self.pixmap = self.pixmap.scaled(64, 64, Qt.KeepAspectRatio, Qt.FastTransformation)
        self.twitter_icon.setPixmap(self.pixmap)
        self.twt_btn = QtWidgets.QPushButton("Twitter")

        v_box = QtWidgets.QVBoxLayout()
        v_box.addStretch()
        v_box.addWidget(self.twitter_icon)
        v_box.addWidget(self.twt_btn)
        v_box.addStretch()

        self.setLayout(v_box)

        self.twt_btn.clicked.connect(self.clkdBtn)
        self.show()

    def clkdBtn(self):
        self.hide()
        self.loading = LoadingScreen()
        browser = webdriver.Chrome()
        browser.get("https://twitter.com/login")
        time.sleep(1)
        #do more stuff in project instead i add more url
        browser.get("https://twitter.com/explore")
        time.sleep(1)
        browser.get("https://twitter.com/login")
        time.sleep(1)
        browser.close()
        time.sleep(1)
        self.show()

    def center(self):
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

app = QApplication(sys.argv)
dm = demo()
app.exit((app.exec_()))

我还从客户端build.gradle文件中删除了sourceSets定义,并且 现在一切正常。