Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Android NDK project with Cmake

I would like to generate my android native application with the android NDK and Cmake, so, I've downloaded the android-cmake toolchain.

Cmake generate my project successfully, but when I try to go in the generate directory and try to run "make", I've got the following error:

-- Configuring done
-- Generating done
-- Build files have been written to: /Users/ldz/Desktop/myProject
[  1%] Building CXX object Project/src/Main/Core/CMakeFiles/Core.dir/Main/Main.cpp.o
arm-linux-androideabi-g++: error: unrecognized command line option '-stdlib=libc++'

I don't know what is wrong here, my project use C++11, here is my g++ --version result:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix

Thanks!

like image 921
LongDuZboub Avatar asked Oct 06 '13 10:10

LongDuZboub


Video Answer


2 Answers

To build an Android NDK project with Cmake and create APK, you should do :

  • Instead of using android-cmake, you should use the fork from taka-no-me.
  • Then use Apk.cmake from pixellight. Copy also [AndroidManifest.xml.in, LoadLibraries.java.in, strings.xml.in] from this repo.
  • Have a CMakeLists.txt like this :
    cmake_minimum_required(VERSION 2.8.3) project(testBuilder) include("Apk.cmake" REQUIRED) include_directories(${ANDROID_NDK}/sources/android/native_app_glue) set(TEST_SRC ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c src/Main.cpp ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -ffor-scope -fno-rtti -fno-exceptions -pipe -ffunction-sections -fdata-sections -ffast-math -Wnon-virtual-dtor -Wreorder -Wsign-promo -fvisibility=hidden -fvisibility-inlines-hidden -Wstrict-null-sentinel -Os -funroll-all-loops -fpeel-loops -ftree-vectorize") set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,--as-needed -Wl,--gc-sections -Wl,--no-undefined -Wl,--strip-all -Wl,-rpath-link=${ANDROID_NDK_SYSROOT}/usr/lib/ -L${ANDROID_NDK_SYSROOT}/usr/lib/") add_library(test SHARED ${TEST_SRC}) target_link_libraries(test log android) set_target_properties(test PROPERTIES COMPILE_DEFINITIONS "ANDROID") set(APP_SHARED_LIBRARIES ${LIBRARY_OUTPUT_PATH}/libtest.so) android_create_apk(test "${CMAKE_BINARY_DIR}/apk" "${APP_SHARED_LIBRARIES}" "" "Data")

This is Main.cpp

#include <android_native_app_glue.h>
#include <android/log.h>

#define APPNAME "TestApp"

void android_main(struct android_app* state)
{
    app_dummy(); // Make sure glue isn't stripped

    __android_log_print(ANDROID_LOG_INFO, APPNAME, "HolyShit you did it !");

    ANativeActivity_finish(state->activity);
}
like image 187
Vi. Avatar answered Sep 19 '22 16:09

Vi.


Based on Vi.:s answer I did a clone of android-cmake on github and added a modified Apk.cmake called android.apk.cmake. I use NativeActivity instead of pixellight:s LoadLibraries.java.

The clone is here: https://github.com/Discordia/android-cmake

I created the example in Vi.:s answer: https://github.com/Discordia/android-cmake-example

like image 25
Robert Sjödahl Avatar answered Sep 18 '22 16:09

Robert Sjödahl