Gradle Android Studio 中的任务未知 属性

Gradle Task in Android Studio is unknown property

我正在移动一个在其原始项目中运行的旧项目。但是,gradle 在新项目中给我带来了问题。问题是 preBuild 调用无法识别我的 gradle 任务。

我试过用'copyAppFiles'将它们变成引用或者直接命名它们而不用单引号。

Build file 'C:\Users\ZBC\AndroidStudioProjects\MyYapApp\app\build.gradle' line: 88

A problem occurred evaluating project ':app'.
> Could not get unknown property 'copyAppFiles' for project ':app' of type org.gradle.api.Project.

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating project ':app'.
    ...
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'copyAppFiles' for project ':app' of type org.gradle.api.Project.
...
at build_9ohkvz8w7llkrdjeut3507o3h.run(C:\Users\ZBC\AndroidStudioProjects\MyYapApp\app\build.gradle:88)

我的Gradle文件

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.squareup.sqldelight'
}

android {

    defaultConfig {
        compileSdkVersion 29
        applicationId setPropertyValue('applicationId', 'com.flicast.jw')
        targetSdkVersion setPropertyValue('targetSdkVersion', 29)
    }

    buildTypes {
        release {...
        }
        debug {
            debuggable true
            resValue "bool", "DEBUG", "true"
        }
    }

    //list flavorDimensions in override priority order
    flavorDimensions 'platform', 'theme'

//assign each productFlavor a corresponding flavorDimension
productFlavors {
    mobile {
        dimension 'platform'
        minSdkVersion setPropertyValue('mobileMinSdkVersion', 22)
    }
    light {
        dimension 'theme'
    }
    dark {
        dimension 'theme'
    }
}

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
}

dependencies {
    api fileTree(include: ['*.jar'], dir: 'libs')
    api project(':xyzacore-android')
    api project(':app-bridge')
}

task applyAppFiles {
    if (!fileTree('../../app-config/src').isEmpty()) {
        //copy app-config files to jw-app/src
        task copyAppFiles(type: Copy) {              //<-------------
            from ("../../app-config/src") {
                //exclude ""
            }
            into "src"
        }
    }
}

def setPropertyValue(parameterName, defaultValue) {
    if (rootProject.ext[parameterName]) {
        return rootProject.ext[parameterName]
    } else {
        return defaultValue
    }
}

preBuild.dependsOn(copyAppFiles) //<----------------
preBuild.dependsOn(applyAppFiles)

顶级 gradle 设置包括

classpath 'com.android.tools.build:gradle:3.6.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.squareup.sqldelight:gradle-plugin:1.2.1"

项目结构设置为

Gradle Plugin Version 3.6.1
Gradle Version 6.3

任何帮助都是有用的。

我认为没有必要在 applyAppFiles 任务中注册 copyAppFiles
Task 文档指出:

Each task belongs to a Project. Each task has a name, which can be used to refer to the task within its owning project, and a fully qualified path, which is unique across all tasks in all projects.

在这里,你封装了copyAppFiles任务applyAppFiles任务里面不正确.

您可以简单地注册您的复制任务如下:

task copyAppFiles(type: Copy) {
if (!fileTree('../../app-config/src').isEmpty()) {
    //copy app-config files to jw-app/src
    // <-------------
        from ("../../app-config/src") {
            //exclude ""
        }
        into "src"
    }
}

然后将其称为依赖于 preBuild 为:

preBuild.dependsOn(copyAppFiles)

我也认为你不需要检查 app-config/src 文件树是否为空。如果它是空的,则不会复制任何内容。当然,除非你有一个特殊的用例来检查它。