Using Gradle 5.4.1, I want to build a CMake project for an Android app. This can be done in app/build.gradle with:
externalNativeBuild {
cmake {
// Provides a relative path to your CMake build script.
version "3.13.0+"
path "../subproject/CMakeLists.txt"
}
}
This project generates a shared library and some Java files by using SWIG. I can specifiy the output directory for the Java sources by -DJAVA_OUTPUT_DIR=generated/java.
How can I force gradle to wait for the CMake build before compiling the main app source code (Kotlin)? The source code imports the generated JAVA files and currently the build process fails because of missing Java files. It seems that gradle run cmake and Kotlin compiler parallel.
Bonus question: how can I add the generated source output directory to the AndroidStudio project tree as generated source? There is a special entry in the tree for generated sources.
Version info:
$ ./gradlew --version ------------------------------------------------------------ Gradle 5.4.1 ------------------------------------------------------------ Build time: 2019-04-26 08:14:42 UTC Revision: 261d171646b36a6a28d5a19a69676cd098a4c19d Kotlin: 1.3.21 Groovy: 2.5.4 Ant: Apache Ant(TM) version 1.9.13 compiled on July 10 2018 JVM: 11.0.5 (Debian 11.0.5+10-post-Debian-1deb10u1) OS: Linux 4.19.0-6-amd64 amd64
You should add the cmake
task as a dependency for the compileKotlin
task.
Gradle task are partially ordered (some task should be launched before some other tasks). Dependencies are used to express this relationship.
To declare a dependency on externalNativeBuild
- append this to the build.gradle
project.afterEvaluate {
if (tasks.findByName("externalNativeBuildDebug")) {
compileDebugKotlin.dependsOn externalNativeBuildDebug
}
if (tasks.findByName("externalNativeBuildRelease")) {
compileReleaseKotlin.dependsOn externalNativeBuildRelease
}
}
What is afterEvaluate
? It's gradle's lifecycle stage. It's when all definitions are read and applied. Read more here.
What are externalNativeBuildDebug
and externalNativeBuildRelease
? These are references to the externalNativeBuild
task, depending on the particular build command