Android NDK - building native libraries without Android Studio
Asked Answered
S

1

6

I'm working on a c/c++ cross-platform project, constructed of 2 main libraries (with a few external dependencies: ssl, yajl, fribidi).
The android solution will include Java files and a JNI layer, all bundled in a AAR file (including assets and the native libs).

I managed to build the whole project, but in a very awkward way:
I created a 'hello world' Android app', with native support, from within Android Studio, and added all native dependencies to the CMAkeList.txt. I added my Java code + JNI and managed to create the AAR (only for ARM, for now).

Now I need to separate the build of the different libraries, to their separate projects, respectively: libA, libB and C.aar.

How is it done without the IDE (and via command-line)?
There's the stand-alone NDK, the make_standalone_toolchain.py script, android.toolchain.cmake and other options, but none are documented or up-to-date. Most documentation still talks about the outdated Android.mk methodology.
I'd presume including android.toolchain.cmake in my CMakeList.txt, which will set all needed environment...

I'm using the newest Android Studio 3.0.1 and NDK r16b (installed via SDK Manager)

Alex - thanks, exactly what I was looking for. Just had to add a few flags and a call to make:

> cmake -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=%ANDROID_NDK%\build\cmake\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=android-19 -DCMAKE_MAKE_PROGRAM=%ANDROID_NDK%\prebuilt\windows-x86_64\bin\make.exe -DCMAKE_BUILD_TYPE=Release -DANDROID_ABI="armeabi-v7a with NEON" ..
> cmake --build .
Suppositive answered 1/1, 2018 at 12:13 Comment(1)
Instead of make, it's cleaner to use cmake --build. directory is an optional parameter. This way you don't need to worry about the choice of generator. Maybe Ninja will work fatster.Hinshaw
H
1

Android Studio (the standard Android gradle plugin, that is) does not support native-only modules, but you can split your CMake script and work with libA and libB separately. You can run cmake from command line (but better use the version that is shipped with Android SDK).

sdk/cmake/3.6.4111459/bin/cmake -DCMAKE_TOOLCHAIN_FILE=sdk/ndk-bundle/build/cmake/android.toolchain.cmake ...

The easiest way to build the AAR file that includes a compiled Java wrapper and the two native libraries would be with Android Studio, but you can run the gradle task from command line. This is what we typically do on a build server.

Hinshaw answered 1/1, 2018 at 13:2 Comment(2)
Does it matter whether we use CMake bundled with SDK or a standard one from APT repository?Vesica
@Vesica as I wrote, to be on the safe side, you may prefer the bundled cmake. It was verified by Google to work correctly with their toolchain file.Hinshaw

© 2022 - 2024 — McMap. All rights reserved.