Gradle task ordering issue in Android Studio
<p>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.</p>

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

<pre><code>project.afterEvaluate {
    preBuild.dependsOn("runSwig")
}
</code></pre>

<p>When I run from the commandline using the command </p>

<pre><code>./gradlew assembleDebug
</code></pre>

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

<pre><code>:app:runSwig
:app:preBuild
:app:preDebugBuild
&lt;blah blah more tasks&gt;
:app:externalNativeBuildDebug
</code></pre>

<p>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</p>

<pre><code>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.
</code></pre>

<p>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. </p>