Can Android Studio use the system CMake? - android-studio

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"

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.

Android NDK - building native libraries without Android Studio

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.

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.

Android Studio - Cmake get current Flavour (build variant)

I am using Android Studio 2.2.2 with Cmake 3.6. I am trying to get access to the current build flavour that is selected in Android Studio via Cmake. Is that possible?
Something like I get current directory path with
${CMAKE_CURRENT_LIST_DIR}
can I get current build flavour somehow in Cmake ?

Rhodes setup and installation for Android in Windows

I am trying to install Rhodes for Android application development. I installed rhodes gem (version 3.2), Android SDK latest release and Android NDK (latest). I run "rhodes-setup" and gave the installation path of Java, Android SDK and NDK. When i try to run "rake run:abdroid" for a sample rhodes applocation, it shows as
"WARNING!!! Path to Android NDK contain spaces! It will not work because of the G
oogle toolchain restrictions. Move it to another location and reconfigure rhodes."
I moved to another location and tried a another version of NDK ( revision 6 and 6b). But still having error. Could please point out what i am doing wrong?
Thank u.
Make sure there are no spaces in the path, like the error message suggests.
Bad NDK Path
C:\Program Files (x86)\android-ndk-r4\
Good NDK Path
C:\source\sdks\android-ndk-r4\
Update the path to the NDK in the Rhodes build configuration files. Try running "rhodes-setup" again and pointing it to your new NDK path.
Just move your NDK folder to your root folder. I have it like this:
C:\android-ndk-r5b
Remember to configure rhodes setup after you have made this change and before running your program. You do this by running:
$ rhodes-setup
on the command prompt while in your app folder.

Resources