Android Studio CMake/Ninja Not Used for Building an NDK project - android

I have the following CMAKE & Ninja installed through Android Studio's SDK Tools:
~/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja --version
1.8.2
I run into "Error Configuring" while trying to build my project. Here is the build output:
Executable : /Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/cmake
arguments :
-H/Users/ssk/MyProject
-B/Users/ssk/MyProject/.externalNativeBuild/cmake/debug/armeabi-v7a
-DANDROID_ABI=armeabi-v7a
-DANDROID_PLATFORM=android-16
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/Users/ssk/MyProject/build/intermediates/cmake/debug/obj/armeabi-v7a
-DCMAKE_BUILD_TYPE=Debug
-DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_CXX_FLAGS=-std=c++11
-DCMAKE_SYSTEM_NAME=Android
-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a
-DCMAKE_SYSTEM_VERSION=16
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
-DCMAKE_ANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle
-DCMAKE_TOOLCHAIN_FILE=/Users/ssk/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake
-G Ninja
-DANDROID_STL=gnustl_statics
-DANDROID_CPP_FEATURES=rtti exception
-DANDROID_TOOLCHAIN=gcc
-DANDROID_NDK=/Users/ssk/android-ndk-r17c/
jvmArgs :
It's missing:
-DCMAKE_MAKE_PROGRAM=/Users/ssk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja
Error:
CMake was unable to find a build program corresponding to "Ninja". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool
Only if I switch to CMake version say 3.6.3155560 it works. Otherwise, I have to install ninja from brew or macports.
Here is the snippet from my build.gradle:
externalNativeBuild {
cmake {
// Linker flags and Visibility options keeps the size of the library small
cppFlags "-std=c++11"
arguments "-DANDROID_STL=gnustl_static",
"-DANDROID_CPP_FEATURES=rtti exceptions",
"-DANDROID_TOOLCHAIN=gcc"
}
}
How to fix it?

Install/Update CMake From Android Studio SDK Manager
Check your CMake from sdk root directory if ninja exists.
Below is not good.
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_ABI=armeabi-v7a",
"-DANDROID_PLATFORM=android-16",
"-DANDROID_STL=gnustl_static",
"-DANDROID_CPP_FEATURES=rtti exceptions",
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=libs"
}
Because, ANDROID_PLATFORM should be automatically decided by Android external native build system according to minSdkVersion, see below official document from how ANDROID_PLATFORM works:
Instead of changing this flag directly, you should set the minSdkVersion property in the defaultConfig or productFlavors blocks of your module-level build.gradle file. This makes sure your library is used only by apps installed on devices running an adequate version of Android. The CMake toolchain then chooses the best platform version for the ABI you're building using the following logic:
If there exists a platform version for the ABI equal to minSdkVersion, CMake uses that version.
Otherwise,
if there exists platform versions lower than minSdkVersion for the ABI, CMake uses the highest of those platform versions. This is a reasonable choice because a missing platform version typically means that there were no changes to the native platform APIs since the previous available version.
Otherwise, CMake uses the next available platform version higher than minSdkVersion.
And, -DANDROID_ABI=armeabi-v7a is not good as well. You should not define this parameter here. CMake will iterate all your ABIs according to your abiFilters automatically. If you just want to build armeabi-v7a, you can specify this using abiFilter, e.g.
externalNativeBuild {
cmake {
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
}
Also, rtti and exceptions are cppFlags, below should be the proper way to set these two flags.
cppFlags "-std=c++11 -frtti -fexceptions"
Ensure that your have properly configured ANDROID_NDK path, because according to your question, you have TWO version of NDK set, one is -DANDROID_NDK=/Users/ssk/android-ndk-r17c/, the other one is -DANDROID_NDK=/Users/ssk/Library/Android/sdk/ndk-bundle. Config NDK path from local.properties:
ndk.dir=/Users/ssk/Library/Android/sdk/ndk-bundle
sdk.dir=/Users/ssk/Library/Android/sdk
what is the fix for -GAndroid Gradle - Ninja?
Add below arguments to the cmake config:
externalNativeBuild {
cmake {
...
version "3.10.2"
arguments "-GAndroid Gradle - Ninja"
}
}

Related

Cmake 3.10.2 not found in SDK, PATH or by cmake.dir prop

Getting the following error when building my project in Android Studio:
[CXX1300] CMake '3.10.2' was not found in SDK, PATH, or by cmake.dir property.
My Gradle looks like the following:
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
}
}
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
}
I have installed CMake in the SDK manager.
I have tried to remove cppFlags and replace with version 3.10.2 and path to the Cmake file; this causes the gradle to not sync at all.
I've tried looking at other threads with a similar problem, but doesn't seem to be able to fix my own problem.
Currently running Android studio bumblebee.
Any tip or help would be appreciated.
While the latest version of cmake is 3.18.1, it looks like Bumblebee is still looking for ver 3.10.2 by some rules it defines, which ended up with a mismatched version error.
Makes sure Cmake 3.10.2 is checked and installed in SDK tools. By the way, check "Show Package Details" in [Android SDK] -> [SDK Tools] -> CMake, to list all available CMake versions.
Good luck!

Android : gradle building libraries for all ABIs

I am working on an android native project which uses C++ code. I am using CMake to build C++ libraries. I am specifying CMake arguments like this in build.gradle:
cmake {
arguments "--warn-uninitialized",
"-DANDROID_TOOLCHAIN=clang",
"-DANDROID_STL=c++_shared",
"-DANDROID_PLATFORM=android-21",
"-DAPP_ALLOW_MISSING_DEPS=true",
"-DSHARED_LIBRARY=1",
"-DOFFLINE_SYNC_SUPPORT_ENABLED=0",
"-DT5_TARGET_PLATFORM=Android",
"-DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a",
"-DANDROID_ABI=armeabi-v7a"
What I want gradle is to build libraries only for armeabi-v7a as specified in cmake arguments but gradle by default is building code for all ABIs.
Though abiFilters can be used in gradle but my gradle build is being invoked by command-line where user has to specify ABI in command line and libs only for that ABI should be built. Like ./gradlew v7 should only build armeabi-v7a libs.

Canot build Android NDK project after latest NDK update and gcc deprecation

I have updated Android NDK to latest version available and now my project doesn't compile anymore.
At the first attempt to compile the project I received a message about gcc deprecation and invite to replace It with clang.
So I have tried to edit
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=gcc', '-DANDROID_STL=gnustl_static'
}
}
In
externalNativeBuild {
cmake {
arguments '-DANDROID_TOOLCHAIN=clang', '-DANDROID_STL=gnustl_static'
}
}
But unfortunately now I get the error:
Error while executing process C:\Android\android-sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {-HD:\My Project\myApp\jni -BD:\My Project\myApp\.externalNativeBuild\cmake\release\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-21 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\My Project\myApp\gradleBuild\intermediates\cmake\release\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Release -DANDROID_NDK=C:\Android\android-sdk\ndk-bundle -DCMAKE_TOOLCHAIN_FILE=C:\Android\android-sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=C:\Android\android-sdk\cmake\3.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja -DANDROID_TOOLCHAIN=clang -DANDROID_STL=gnustl_static}
The project compilation before the NDK update worked fine.
Don't try to build NDK when the path to your sources contains spaces.
The latest NDK deprecated gnustl, too. Try c++_shared instead.
You don't need to specify ANDROID_TOOLCHAIN in arguments.
Make sure you use a version of gradle Android plugin that is compatible with the latest NDK, see https://developer.android.com/studio/releases/gradle-plugin.
This runtime is tightly coupled to GCC, which is no longer supported in the NDK. As such, it has not received updates for several releases. The version in the NDK supports most of C++11 (see Issue 82), and some portions of this library are incompatible with Clang.
gnustl
This library is deprecated and will be removed in NDK r18. Beginning with NDK r16, you should use libc++ instead.
You should use "-DANDROID_STL=c++_static" instead of -DANDROID_STL=gnustl_static

Errors updating from NDK version 16 to NDK version 17

This is for an Android project using JNI with the NDK. I'm building the project with Android Studio 3.0.1. I recently updated my NDK from version 16 to version 17 in the hopes of making more compiler optimizations available. After the update, I'm getting the following errors:
CMake Error at C:/Users/John/AppData/Local/Android/Sdk/ndk-bundle/build/cmake/android.toolchain.cmake:312 (message):
Invalid Android ABI: armeabi. (armeabi is no longer supported. Use
armeabi-v7a.)
Call Stack (most recent call first):
C:/Users/John/AppData/Local/Android/Sdk/cmake/3.6.4111459/share/cmake-3.6/Modules/CMakeDetermineSystem.cmake:98 (include)
CMakeLists.txt
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
I'm getting this error for each of the deprecated ABIs armeabi, mips, and mips64. I understand that I should remove these ABIs from the build, but I can't find instructions on how to do that. I'm not using an Application.mk and I don't see the ABIs being specified anywhere; how can I remove these unused ABIs from my Android Studio project, or better yet, how can I set my project to just use the current non-deprecated ABIs? Thank you.
It should be enough to upgrade your gradle plugin to 3.1.2 or higher, in root (project) build.gradle script. Using the latest plugin is recommended not only to be compliant with latest NDK:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
}
}
You also must change gradle/wrapper/gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip v.4.4
If you cannot afford such change, you may try to skip the deprecated ABIs for build and packaging:
android {
defaultConfig {
ndk {
abiFilters 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
}
}
packagingOptions {
doNotStrip '*/mips/*.so'
doNotStrip '*/mips64/*.so'
}
}

adding "-O0" to cppFlags fails to disable clang compile optimization in android studio

I am building my app in debug mode, and I notice some errors saying "parent failed to evaluate: no location, value may have been optimized out". Therefore, I try to add "-O0" in my module build.gradle like this:
externalNativeBuild {
cmake {
cppFlags "-O0 -frtti -fexceptions -std=c++11 -DANDROID_ARM_NEON=TRUE -mfloat-abi=softfp "
abiFilters "armeabi-v7a"
}
}
But still, the same error shows up after adding "-O0". May I ask how to disable compiler optimization properly? My android Studio version is 2.3.3, my sdk tool version is 26.0.2 and my ndk version is 15.1.4
If you want to disable optimization for release build, you can force Debug for C/C++ only:
android {
defaultConfig {
externalNativeBuild {
cmake {
arguments '-DCMAKE_BUILD_TYPE:STRING=Debug'
You can override the build flags by adding the following to your CMakeLists.txt:
# Debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")
# Release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")
To verify this has worked check build output in:
app/.externalNativeBuild/cmake/<buildconfig>/<architecture>/build.ninja
Look for a line starting with FLAGS. This doesn't actually replace the existing compiler flags it just appends your flags and these take precedence.
The default flags are inherited from $ANDROID_NDK/build/cmake/android.toolchain.cmake so you could edit that file directly, however, if you update your NDK these changes will be overwritten.

Categories

Resources