c++cmakeandroid-gradle-pluginswigandroid-studio-2.2

Gradle task ordering issue in Android Studio


I am using Android Studio 2.2 Beta 3 and my test project uses CMake to build a hybrid C++ and Java app. The JNI interface is generated by SWIG. This means that I would want to generate the SWIG wrappers before the externalNativeBuild and JavaCompile gradle tasks; ie my CMakeLists.txt specifies the cpp wrapper file generated by SWIG and my java code imports the java wrapper files generated by SWIG.

To ensure SWIG is run and generates the necessary wrapper files before any tasks, I specifies the following in my app/build.gradle file

project.afterEvaluate {
    preBuild.dependsOn("runSwig")
}

When I run from the commandline using the command

./gradlew assembleDebug

I do not face any issues and as expected my task "runSwig" runs before any of the other tasks

:app:runSwig
:app:preBuild
:app:preDebugBuild
<blah blah more tasks>
:app:externalNativeBuildDebug

But issue is when the project is first opened in Android Studio, it looks like the external native build gets invoked before runSwig and I get the error

CMake Error at CMakeLists.txt:79 (add_library):
  Cannot find source file:

    ../../../wrap.cxx

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

FAILURE: Build failed with an exception.

The error does not go away till I either assemble using command line OR, remove the wrap.cxx file from my CMakeLists.txt and re-add it after swig gets run successfully at-least once.


Solution

  • Yeah, this is because Android Studio IDE need get the files to display in IDE before compile: expose different behavior between command line and IDE build. In here I hack to download the needed repo earlier than anything else, so Android Studio will not complain. But it is not pretty... and long android studio start up time...