简体   繁体   中英

Android Studio custom gradle task with dependencies - “Could not get unknown property”

I'm using a library that supplies gradle tasks to upload builds to HockeyApp ( https://github.com/x2on/gradle-hockeyapp-plugin ). I think what it does specifically isn't important, just that it creates tasks. I can run those tasks no problem. I wanted to create a task to run several of these custom tasks at once. The upload tasks are named like upload[BuildVariant]ToHockeyApp and it created one for each build variant. My custom task, defined in android in the project level gradle file, looks like this:

task uploadLegacyReleaseToHockeyApp(dependsOn:
            uploadVariant1ReleaseToHockeyApp,
            uploadVariant2ReleaseToHockeyApp,
            // etc
      ){}

When syncing the gradle file, I get this error:

Error:(89, 0) Could not get unknown property 'uploadVariant1ReleaseToHockeyApp' for object of type com.android.build.gradle.AppExtension.

I've triple checked there are no typos and the task in the error message actually exists. Am I doing something wrong, or is it not possible to write a dependency on a task that's dynamically generated like this? I don't really know how the plugin works, but it creates its own task group with automatically generated tasks in it.

UPDATE based on the answer from lukegv:

If I use the dependsOn method I get this error: Error:(90, 0) Could not get unknown property 'uploadVariant1ToHockeyApp' for task ':app:uploadLegacyReleaseToHockeyApp' of type org.gradle.api.DefaultTask.

With the constructor:

task uploadLegacyReleaseToHockeyApp(dependsOn:
            'uploadVariant1ToHockeyApp',
            'uploadVariant2ToHockeyApp',
            // etc

I got this: Could not find method uploadLegacyReleaseToHockeyApp() for arguments [{dependsOn=uploadVariant1ToHockeyApp}, uploadVariant2ToHockeyApp,...

Note the first item is in curly braces and the rest are not.

If I put all the task names in a single string, it successfully syncs the build files, but the string is interpreted as a single task name so running it fails.

Gradle distinguishes between the configuration phase and the execution phase . In the configuration phase , Gradle evaluates (by executing) the whole build script and afterwards it builds a task dependency tree. In the execution phase , Gradle executes the tasks specified via command line and their dependencies.

Since it is possible to add or define things like variants over the whole build script, generated tasks are often created at the end of the configuration phase , so that they are not available as properties in the Project scope during configuration.

However, you can define a task dependency by specifying a task name as String. This way, a dependency is registered and the subsequently generated task will be used after the configuration phase , when the dependency tree is built.

As a example:

task t1 {
    dependsOn t2        // this fails
    dependsOn 't2'      // this works, even if t2 does not exist right now
}

afterEvaluate {
    task t2 {}
}

To solve your specific problem, specify your task dependency as String containing the task name:

task uploadLegacyReleaseToHockeyApp {
    dependsOn 'uploadVariant1ReleaseToHockeyApp'
    dependsOn 'uploadVariant2ReleaseToHockeyApp'
}

I am using the dependsOn method instead of the dependsOn map key in the constructor, because I think its more elegant that way, however you can also just edit your code to use a list of Strings:

task uploadLegacyReleaseToHockeyApp(dependsOn: ['uploadVariant1ReleaseToHockeyApp', 'uploadVariant2ReleaseToHockeyApp']) {
    // configuration
}

I got it working, after some further searching I found with the dependsOn in the constructor, it needs to be an array not just a list of strings:

task taskName(dependsOn:
            ['otherTask1','otherTask2']){}

I don't know why the dependsOn method failed but I have something that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM