Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 989
ssk Avatar asked Jan 31 '19 22:01

ssk


People also ask

How do I know if Android NDK is installed?

You'll need to point to your NDK in your eclipse by adding the path of ndk-build to Window > preferences > android > NDK. Right click on your project folder. Choose android tools -> add native support (the bottom one) and click finish. Now it will ask for a name for your .

Does Android Studio have NDK?

Android Studio also supports ndk-build due to the large number of existing projects that use the build toolkit. However, if you are creating a new native library, you should use CMake. This guide gives you the information you need to get up and running with the NDK on Android Studio.

What is CMake used for in Android Studio?

The Android NDK supports using CMake to compile C and C++ code for your application.


1 Answers

Install/Update CMake From Android Studio SDK Manager

Install/Update CMake From Android Studio SDK Manager

Check your CMake from sdk root directory if ninja exists. enter image description here


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:

  1. If there exists a platform version for the ABI equal to minSdkVersion, CMake uses that version. Otherwise,
  2. 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.
  3. 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"
    } 
} 
like image 174
shizhen Avatar answered Sep 16 '22 21:09

shizhen