Running CMake from command line within an Android

2020-07-13 11:48发布

How might one run an NDK Cmake build independently from the rest of an Android project, ideally from the command line, external to Android Studio?

The equivalent of running ndk-build from the jni directory for slightly older Android NDK projects.

I need to investigate exactly what the calls to the compiler look like, and I can't seem to get this information when building the whole project from within Android Studio

My first attempt was just to run cmake from the project/app directory containing CMakeLists.txt, but this informs me that cmake is not installed - so how is Android Studio managing to build it then?

3条回答
Luminary・发光体
2楼-- · 2020-07-13 11:57

Your original problem is that you cannot see the command-line invocation when building with Android Studio.

You can get the command line arguments to the compiler by editing your app/build.gradle file.

defaultConfig {
    ...
    externalNativeBuild {
        cmake {
            ...
            arguments "-DCMAKE_VERBOSE_MAKEFILE=1", ...
        }
    }

}

In Adroid Studio's Gradle Console pane, you will then see the command line for the compiler and linker like so:

[1/176] /home/bram/android-sdk-linux/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang --target=armv7-none-linux-androideabi --gcc-toolchain=/home/bram/android-sdk-linux/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64 --sysroot=/home/bram/android-sdk-linux/ndk-bundle/sysroot -isystem /home/bram/android-sdk-linux/ndk-bundle/sysroot/usr/include/arm-linux-androideabi -D__ANDROID_API__=19 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16 -fno-integrated-as -marm -mfpu=neon -Wa,--noexecstack -Wformat -Werror=format-security -Os -DNDEBUG -fPIC -MD -MT /home/bram/src/GPGOAP/CMakeFiles/gpgoap.dir/astar.c.o -MF /home/bram/src/GPGOAP/CMakeFiles/gpgoap.dir/astar.c.o.d -o /home/bram/src/GPGOAP/CMakeFiles/gpgoap.dir/astar.c.o -c /home/bram/src/GPGOAP/astar.c

查看更多
贼婆χ
3楼-- · 2020-07-13 12:03

As detail to the accepted answer:

The complete set of parameters passed to CMake is written to:

<project-root>/<module-root>/.externalNativeBuild/cmake/<build-type>/<ABI>/cmake_build_command.txt`

See for details: https://developer.android.com/ndk/guides/cmake.html#build-command

查看更多
Root(大扎)
4楼-- · 2020-07-13 12:21

If your goal is to just run from the command line (as opposed to trying to do exactly what gradle is doing), just use cmake the way you normally would:

$ cmake -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake \
    -DANDROID_ABI=whatever $YOUR_SOURCE_DIR

Alternatively, you can just run ./gradlew from the command line.

查看更多
登录 后发表回答