Android Studio — Compile and run an NDK application with custom .mk file

Lately I’ve started to develop a project which uses native libraries of OpenCV for Android.
I’ve been literally struggling to get the samples to work, since the samples provided are assuming Android Studio is using the given .mk file as default, but that’s not true! In fact, its default behavior builds a .mk file on-the-fly, therefore we can’t use our own way to build our work.

First, I’ve had to align the OpenCVLibrary310 module build.gradle file and my app’s build.gradle file to compile with the same targetSdk, in this case I’ve had to put compileSdkVersion to 21 in both of them.

I’ve been searching for a good amount of hours now (among lots of stackoverflow posts and other resources!), and finally got a solution to make it work.

Basically it looks for .mk files into the src/main/jni folder and produces the required libs. It also check which SO is in use and adapt to different command line instructions.

As per this post, hopefully it’s working and you should find the .so files into your poject src/main/libs folder. Of course you should provide an file to describe which libraries to be produced:

You can of course use this same approach for compiling code which requires custom and files, I’ve just compiled a sample project for CrystaX NDK which was assuming that the build was relying on its own .mk files (using Android Studio 2.1 / Gradle 2.1.0 / android.useDeprecatedNdk=true in the instead was completely ignoring them!).

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.