I've started developing with Android NDK in Android Studio but it's a big pain since it doesn't have a proper C++ support. So I try to create a cmake project to build my stuff. I've used android-cmake and it works well so far, except for the fact that <android/log.h>
, <android/native_window.h>
and probably all the <android/*>
inclusions cannot be found. IDE (CLion) also marks them as missing because of it. Am I missing something in CMakeLists.txt
?
Here is CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.4)
project(App)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(AndroidNdkGdb)
include(AndroidNdkModules)
android_ndk_gdb_enable()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/src/main/libs/armeabi-v7a)
set(LIBRARY_DEPS GLESv2 android log)
include_directories(${PROJECT_SOURCE_DIR}/src/main/lib/armeabi-v7a/freetype/include/freetype2)
file(GLOB_RECURSE SRCS *.cpp)
file(GLOB_RECURSE HDRS *.h)
add_library(nativeegl SHARED ${SRCS} ${HDRS})
target_link_libraries(nativeegl log android)
android_ndk_gdb_debuggable(nativeegl)
and cmake launch params:
-DCMAKE_TOOLCHAIN_FILE=android.toolchain.cmake \
-DANDROID_NDK=/opt/android-ndk -DCMAKE_BUILD_TYPE=Debug \
-DANDROID_ABI="armeabi-v7a" -DANDROID_STL=gnustl_static \
-DANDROID_NATIVE_API_LEVEL=android-21
And this is the original Android.mk that works fine when I build in studio:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := nativeegl
LOCAL_SRC_FILES := $(patsubst $(LOCAL_PATH)/%, %, $(wildcard $(LOCAL_PATH)/*.cpp))
LOCAL_C_INCLUDES :=
LOCAL_CFLAGS := -std=c++11 -O2 -Wtype-limits -Wmissing-field-initializers -Wreturn-type -Wuninitialized
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2
LOCAL_STATIC_LIBRARIES := android_native_app_glue ndk_helper freetype
include $(BUILD_SHARED_LIBRARY)
$(call import-add-path,$(LOCAL_PATH)/../lib/$(TARGET_ARCH_ABI))
$(call import-module,freetype)
And Application.mk:
APP_STL := gnustl_static
APP_PLATFORM := android-10
APP_ABI := armeabi-v7a
appreciate your help.
Note: If you are using Android Studio, go to Add C and C++ code to your project to learn the basics of adding native sources to your project, creating a CMake build script, adding your CMake project as a Gradle dependency, and using newer versions of CMake than those included in the SDK. The NDK supports CMake via a toolchain file.
After you configure a new CMake build script, you need to configure Gradle to include your CMake project as a build dependency, so that Gradle builds and packages your native library with your app's APK. Note: If your project uses ndk-build, you don’t need to create a CMake build script.
To create a plain text file that you can use as your CMake build script, proceed as follows: Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu. Right-click on the root directory of your-module and select New > File. Enter "CMakeLists.txt" as the filename and click OK.
Warning: CMake has its own built-in NDK support. Before CMake 3.21, this workflow is not supported by Android and is often broken with new NDK releases. Starting from CMake 3.21, the implementations are merged. CMake's built-in support has similar behavior to the NDK toolchain file, though variable names differ.
The actual problem was in an error in one android-cmake file and wrong cmake variable in CMakeLists.txt. How did it happen? Probably I've followed some tutorial for older version of android-cmake. Here is how CMakeLists.txt looks now:
cmake_minimum_required(VERSION 2.8.4)
project(App)
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
include(AndroidNdkGdb)
include(AndroidNdkModules)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/src/main/libs/armeabi-v7a)
set(LIBRARY_DEPS GLESv2 android log EGL)
include_directories(${PROJECT_SOURCE_DIR}/src/main/lib/armeabi-v7a/freetype/include/freetype2)
file(GLOB_RECURSE SRCS *.cpp)
file(GLOB_RECURSE HDRS *.h)
android_ndk_gdb_enable()
add_library(nativeegl SHARED ${SRCS} ${HDRS})
target_link_libraries(nativeegl log android)
android_ndk_gdb_debuggable(nativeegl)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With