Search code examples
androidgradleandroid-gradle-pluginbuild.gradlegradle-task

Gradle Task in Android Studio is unknown property


I am moving an old project which runs in it's original project. But, it's gradle has been giving me issues in a new project. The issue is that my gradle task is not recognized by the preBuild call.

I have tried turning them into references with 'copyAppFiles' or directly naming them without single quotes.

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)

My Gradle file

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)

Top level gradle settings include

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"

Project structure settings are

Gradle Plugin Version 3.6.1
Gradle Version 6.3

Any help would be useful.


Solution

  • I do not think it's necessary to register copyAppFiles inside applyAppFiles task.
    The Task documentation states:

    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.

    Here, you are encapsulating copyAppFiles task inside applyAppFiles task which is semantically incorrect.

    You could simply register your copy task as below:

    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"
        }
    }
    

    And then call it as dependent to preBuild as:

    preBuild.dependsOn(copyAppFiles)
    

    I also think you do not need to check whether the app-config/src filetree is empty or not. If it's empty nothing would be copied. Unless, of course, you have a special use-case for this to be checked.