How to add dependency to native compile task

How can one add a dependency to one of the native compile tasks? For example, taking the samples/cpp as a starting point, I’d like to add the “clean” task as a dependency on compileHelloSharedLibraryHelloCpp.

compileHelloSharedLibraryHelloCpp.dependsOn clean

complains with

Could not find property ‘compileHelloSharedLibraryHelloCpp’ on root project ‘cpp’.

and

tasks["compileHelloSharedLibraryHelloCpp"].dependsOn clean

gives

Task with name ‘compileHelloSharedLibraryHelloCpp’ not found in root project ‘cpp’.

Thanks!

The native language and new publishing plugins both use the new configuration model, which means the syntax to configure their model elements is a bit different. For the most part, you simply need put this type of configuration within a ‘model {…}’ block. Take a look at the latest release notes for more info.

model {

tasks.compileHelloSharedLibraryHelloCpp {

dependsOn clean

}

}

Also, is there a specific reason why you need to perform a clean prior to your compile task? Incremental build support should make this unnecessary.

Great, thanks for the info. Using the clean task was just an example. What I really need to do is perform code generation (swig) prior to compilation.

There’s a nicer way to do code generation with a native project: declare an additional source set with a ‘generatedBy’ property. The ‘native-binaries/idl’ sample gives an example:

’’‘
model {   
    components {
        main(NativeExecutableSpec) {
            sources {
                idlOutput(CSourceSet) {
                    generatedBy tasks.idl
                }
                c.lib sources.idlOutput
            }
        }
    }
}
’’'

The ‘idlOutput’ source set takes it’s configuration from ‘generator’ task, which must have a ‘sourceDir’ property and may have an optional ‘headerDir’ property.

Alternatively, you can wire in an arbitrary task for a source set as demonstrated in this integration test.