【问题标题】:Gradle Task in Android Studio is unknown propertyAndroid Studio 中的 Gradle 任务是未知属性
【发布时间】:2021-10-05 17:09:48
【问题描述】:

我正在移动一个在其原始项目中运行的旧项目。但是,它的 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

任何帮助都会很有用。

【问题讨论】:

  • 为什么在task 中需要task
  • @Jay 我需要检查我正在复制的文件是否已经可用,然后执行复制。
  • 请检查我更新的答案。

标签: android gradle android-gradle-plugin build.gradle gradle-task


【解决方案1】:

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

每个任务都属于一个项目。每个任务都有一个名称,可用于引用其所属项目中的任务,以及一个完全限定的路径,该路径在所有项目中的所有任务中都是唯一的。

在这里,您将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 文件树是否为空。如果它为空,则不会复制任何内容。当然,除非您有特殊的用例需要检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-15
    • 2021-08-05
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    相关资源
    最近更新 更多