chugadie
chugadie

Reputation: 2873

How to compile Android NDK Samples

Android has some NDK samples that do not compile for me.

Given:

I get the following C compilation errors:

ndk-samples/native-activity/app/src/main/cpp/main.cpp:50:5: error: use of undeclared identifier 'android_set_abort_message'
    android_set_abort_message("failed for format error message");

native-activity/app/src/main/cpp/main.cpp:127:38: error: 'AChoreographer_getInstance' is unavailable: introduced in Android 24
    AChoreographer_postFrameCallback(AChoreographer_getInstance(), Tick, this);

ndk/25.2.9519653/toolchains/llvm/prebuilt/linux-x86_64/bin/../sysroot/usr/include/android/choreographer.h:95:17: note: 'AChoreographer_getInstance' has been explicitly marked unavailable here
AChoreographer* AChoreographer_getInstance() __INTRODUCED_IN(24);

This seems like the ANDROID_API is not getting passed the the C compilation step. How can I do that?

I've tried adding these to the sample CMakeLists.txt and reruning the above cmake, then make commands.

set(CMAKE_SYSTEM_NAME Android)
set(CMAKE_SYSTEM_VERSION 24) # API level
set(CMAKE_ANDROID_ARCH_ABI arm64-v8a)
set(CMAKE_ANDROID_STL_TYPE gnustl_static)
set(ANDROID_PLATFORM 24) # API level
set(CMAKE_ANDROID_API 24) # API level

Upvotes: 0

Views: 60

Answers (1)

Dan Albert
Dan Albert

Reputation: 10509

Both android studio and command line instructions are in the readme: https://github.com/android/ndk-samples?tab=readme-ov-file#build-and-run

ndk 25.2.9519653

Can't do that. They require a newer NDK: https://github.com/android/ndk-samples/blob/main/build-logic/src/main/java/com/android/ndk/samples/buildlogic/Versions.kt#L9

cmake -DCMAKE_SYSTEM_NAME="Android" -DCMAKE_SYSTEM_VERSION="24" -DCMAKE_ANDROID_NDK="${NDK_HOME}" -DANDROID_NDK="${NDK_HOME}" -DANDROID_PLATFORM=android-24 ..

It's not possible to build these with CMake only. You need to run Gradle to build the non-native parts of the app.

Upvotes: 1

Related Questions