Android NDK - building native libraries without Android Studio - android-ndk

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 .

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.

Related

which cmake will Android plugin use?

With Android Studio 3.3 I use native (C++) library, which I built with CMake. The Android plugin (v. 3.2.1) will choose the 'builtin' or 'external' cmake, depending on the configuration of externalNativeBuild, as documented at developer.android.com.
I want to add an extra custom task (install) that should use the same cmake version as the Android Plugin. But even with the 'builtin' cmake, it's not clear what the path is. I can find android.sdkDirectory, but even there I have today sdk\cmake\3.6.4111459 and sdk\cmake\3.10.2.4988404, and for some strange reason, some of my projects choose 3.6, while others use 3.10. This contradicts the official the release notes for Android Studio that "Gradle still uses version 3.6.0 by default", but well…
How can I decide which to use, without reimplementing the Android Plugin's obscure logic?
One workaround that may help, parse the first line of the generated file .externalNativeBuild/cmake/debug/armeabi-v7a/cmake_build_command.txt:
Executable : C:\local\Android\sdk\cmake\3.10.2.4988404\bin\cmake.exe
This still needs some adjustments, because the later versions of build tools will use .cxx instead of .externalNativeBuild; the build variant names may be different too.
Android Studio will pick up the latest CMake version under sdk\cmake, but you can configure your particular version from
externalNativeBuild {
cmake {
...
version "cmake-version"
}
}
And specify our custom CMake director as below inside local.properties
cmake.dir="path-to-cmake"
Or, you can choose NOT to upgrade your CMake from SDK Manager (just to delete the sdk\cmake\<version to delete> folder will be fine) so that you can stick to the CMake version your project is comfortable with.

Generating protobuf c++ code inside Android Studio

I am trying to build a bridge between Java and C++ using Protobuf for serialization and storage purpose, inside an Android Studio project. Things work well on the Java side. Using the JavaLite plugin, I am able to generate the classes for each file.
Where things start to break is trying to do the same on the native side. I've tried many things and I came to the conclusion that using cmake would be the best way to get there. However, I am running into some issues.
Cmake doesn't seem to know about protobuf. When I add these lines to CMakeList.txt:
include(FindProtobuf)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
I am getting:
-- Could NOT find Protobuf (missing: Protobuf_LIBRARIES Protobuf_INCLUDE_DIR)
which is strange since my version passes the smoke test:
cmake_minimum_required(VERSION 3.4.1)
Sanity check, from terminal:
$ protoc --version
libprotoc 3.5.1
What are the other option to generate C++ protobuf in Android Studio? Would NDK-build be another option?

Android Studio Error Communicating With System CMake

I just created a trivial Android project with C++ support using Android Studio 3.0.1 on OS X High Sierra and I'm trying to figure out how to get CMake support.
I wasn't able to install CMake through Android Studio because there's an error with the project and the Tools > Android option is not available through the menu.
Here's what the project looks like:
For comparison, here's another trivial project without C++ support showing the Tools > Android option.
I read something about configuring Android Studio to use the system cmake. Apparently, you can override the cmake.dir setting in the project's local.properties file:
https://developer.android.com/studio/projects/add-native-code.html
excerpt:
Include the path to the CMake installation in your project's
local.properties file: cmake.dir="path-to-cmake"
So, I set the cmake.dir to /usr/local (because I already installed cmake previously through homebrew and changed the properties file)
It now reads (replacing ~ with your home directory)
# ... some comments warning you not to modify
# the properties file ...
ndk.dir=~/Library/Android/sdk/ndk-bundle
sdk.dir=~/Library/Android/sdk
cmake.dir="/usr/local"
Android Studio now shows the error message (newlines and \s inserted for legibility and home directory replaced with ~):
Error:Error occurred while communicating with CMake server.
Check log
~/AndroidStudioProjects/Cpptest/
\ app/.externalNativeBuild/cmake/
\ debug/armeabi-v7a/cmake_server_log.txt
for additional information.
However, the contents of that file suggest that the cmake binary did something reasonable in response to a command invoked by Android Studio.
CMAKE SERVER:-
CMAKE SERVER: [== "CMake Server" ==[
CMAKE SERVER: {"supportedProtocolVersions":[{"isExperimental":true,"major":1,"minor":1}],"type":"hello"}
CMAKE SERVER: ]== "CMake Server" ==]
The system CMake is version 3.10.0 which is greater than 3.7 (I believe the minimum supported version).
How do I "convince" Android Studio to use the system CMake?
Is there a way to install a CMake distribution managed by the IDE if Tools > Android is not available?
If neither of those options seems straightforward, can I configure Android Studio to use a different build system for the C++ sources, like GNU Make or just plain gradle?
So, I still don't know why pointing Android Studio at the system CMake didn't work. I also tried installing a "full CMake" distribution through their website (https://cmake.org/download/) and pointing to /Applications/CMake.app/Contents/
However, it is possible to install CMake and lldb in a different way in Android Studio on OS X even if Tools > Android Does not appear.
And that is:
Preferences > Appearance & Behavior > System Settings > Android SDK > SDK Tools
Or, equivalently, Cmd , and then type sdk in the search field and then click the SDK Tools tab.
Here's a picture.
There was an issue in earlier versions of external CMake support where we didn't support future CMake server protocol versions. This has since been fixed. If you try a recent Android Studio 3.1 I think it should work.

Can Android Studio use the system CMake?

I'm using Android Studio + CMake to build a native library. AS requested installing CMake, even though I already have CMake installed and in my path. Can you tell AS to look for CMake at a certain location, instead of installing another CMake?
The new Android plugin for gradle 3.0 will support using system CMake, for CMake 3.7+.
From that webpage:
Download and install CMake 3.7 or higher from the official CMake website.
Include the path to the CMake installation in your project's local.properties file:
cmake.dir="path-to-cmake"

Android studio use gradle2.5 dependencies com.android.tools.build:gradle-experimental:0.2.1

Android studio use gradle2.5 dependencies com.android.tools.build:gradle-experimental:0.2.1
when i create AndroidLibrary , how to create build.gradle?
(I know apply plugin: 'com.android.model.application' when add a Phone Module )
if your app could use the latest gradle-experimental, possibly:
Build native shared libs with: apply plugin: 'com.android.model.application'. one example is in hello-libs
Build native static libs, indirectly supported. It is in Teapot example. the native-app-glue is built as a static lib and consumed by Teapot application. When you build teapot APK, a static lib of native-activity will be generated forcefully in build directory. Grep it, you will find it. That static lib could be distributed to others
hope this helps. If you could use android studio cmake support, it is simpler[my personal view], the similar examples are in master-cmake branch

Resources