Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 310
Harkish Avatar asked Aug 31 '16 22:08

Harkish


People also ask

Does Gradle run tasks in order?

In Gradle terms this means that you can define tasks and dependencies between tasks. Gradle guarantees that these tasks are executed in the order of their dependencies, and that each task is executed only once.

Does order matter in Gradle dependencies?

The order inside the dependencies { } block is preserved. You are kind of right. Dependency ordering is preserved, but for example all compile dependencies will be before testCompile dependencies no matter of how you order them.

How do I sync Gradle with Android Studio?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

What is Gradle project Sync failed in Android Studio?

It could be that you are using gradle in offline mode. To uncheck it go to File > Settings > Gradle , uncheck the Offline Work checkbox, and click Apply Make sure you have internet connection and sync the project again.


2 Answers

I've run into this same problem with the order of the build in Android Studio 3 (and 2.3 I guess).

I don't know if this is a traditionally valid solution, but it -seems- to work in my Android/SWIG/NDK example (here: https://github.com/sureshjoshi/android-ndk-swig-example)

I added this into my Gradle file, so that CMake is called before the build fails out on missing files. CMake calls SWIG to auto-generate my Java files and place them appropriately.

project.afterEvaluate {
    javaPreCompileDebug.dependsOn externalNativeBuildDebug
}

I probably also need a

javaPreCompileRelease.dependsOn externalNativeBuildRelease

or something similar... Just haven't tested it yet.

Stemmed from this issue: https://github.com/sureshjoshi/android-ndk-swig-example/issues/8

like image 119
SJoshi Avatar answered Sep 22 '22 10:09

SJoshi


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

like image 36
Gerry Avatar answered Sep 22 '22 10:09

Gerry