Log listener failed after configuration cache enabled

Hi I have problem on my gradle script after enabling configuration cache.

The script used to listen log after certain task executed, and if some word exist on the log, it will throw error and failed the build. Below is the full script that cause the problem only if config cache enabled:


void processKAPTWarnings(Task task) {
    onTaskOutput(task) {
        // check only in main source code
            parcelKAPTWarning(it)
    }
}

void processCompileKotlinWarnings(Task task) {
    onTaskOutput(task) {
        // check only in main source code

            parcelCompileKotlinWarning(it)
            deprecatedCompileKotlinWarning(it)

    }
}

static void deprecatedCompileKotlinWarning(String taskOutput) {
    if (taskOutput.contains("'ResourceUtil' is deprecated")) {
        throw new GradleException(
                """
            There are warnings: 'ResourceUtil' is deprecated. See build output for details.
            Please use ResourceProvider instead.
            """.stripIndent()
        )
    }
    if (taskOutput.contains("'getter for appBarDelegate: CoreAppBarDelegate?' is deprecated")) {
        throw new GradleException(
                """
            There are warnings: 'getter for appBarDelegate: CoreAppBarDelegate?' is deprecated. See build output for details.
            Please use baseAppBarDelegate or getBaseAppBarDelegate instead.
            """.stripIndent()
        )
    }
}


static void parcelKAPTWarning(String taskOutput) {
    if (taskOutput.contains('warning: Parceler: Reflection is required')) {
        throw new GradleException(
                """
            There are warnings: 'Parceler: Reflection is required', please fix them. See build output for details.
            If the problem inside kotlin class, please use @Parcelize.
            If not possible, please use @Parcel(Parcel.Serialization.BEAN) or manually tag getter setter with @ParcelProperty.
            """.stripIndent()
        )
    }
    if (taskOutput.contains('unknown enum constant Parcel$Serialization.BEAN')) {
        throw new GradleException(
                """
            There are warnings: 'unknown enum constant Parcel\$Serialization.BEAN', please fix them. See build output for details.
            To solve this, you can add `implementation libraries.parceler_api` to module dependencies.
            """.stripIndent()
        )
    }
}

static void parcelCompileKotlinWarning(String taskOutput) {
    if (taskOutput.contains('Property would not be serialized into')) {
        throw new GradleException(
                """
            There are warnings: 'Property would not be serialized into a 'Parcel'.', please fix them. See build output for details.
            Add '@IgnoredOnParcel' annotation to properties in class body to fix this error.
            If the property should be serialized, please move it to constructor.
            """.stripIndent()
        )
    }
}

static void onTaskOutput(Task task, Action<String> action) {
    List<String> outputList = []
    StandardOutputListener listener = { outputList.add(it) }
    task.doFirst {
        task.logging.addStandardOutputListener(listener)
    }
    task.doLast {
        task.logging.removeStandardOutputListener(listener)
        outputList.forEach {
            action.execute(it)
        }
    }
}

static void onTaskExists(Project project, String taskName, Action<Task> action) {
    Task task = project.tasks.findByName(taskName)
    if(task != null) {
        action.execute(task)
    }
}

subprojects { Project subProject ->
    afterEvaluate {
        def variants = getModuleVariants(subProject)
        if (variants != null) {
            variants.all { variant ->
                afterEvaluate {
                    onTaskExists(subProject, "kapt${variant.name.capitalize()}Kotlin", this.&processKAPTWarnings)
                    onTaskExists(subProject, "compile${variant.name.capitalize()}Kotlin", this.&processCompileKotlinWarnings)
                }
            }
        }
    }
}

static def getModuleVariants(Project project) {
    if(project.plugins.hasPlugin('com.android.application') ||
            project.plugins.hasPlugin('com.android.dynamic-feature')) {
        return project.android.applicationVariants
    }
    if(project.plugins.hasPlugin('com.android.library')) {
        return project.android.libraryVariants
    }
    return null
}

This is the error if configuration cache used (on second build):
No signature of method: compile_warnings_7efo6z1scjf8dzh41bjehq8ci$_processKAPTWarnings_closure2.parcelKAPTWarning() is applicable for argument types: (String) values: [:rental-model:kaptGeneralDebugKotlin]

I am not sure how to fix this error and why it failed when configuration cache enabled.
Anyone have advice about how to fix it, or have better alternative way to achieve what i need ?
Thank you in advance.