Building Cmake for Android from Windows
Posted on Sat 12 January 2019 in Review
Calling this little batch file from inside a library folder which has a CMakeLists.txt
has done a decent job of building some useful C++ libraries.
set android_sdk=C:\Craftwork\Android\sdk
set cmake_dir=%android_sdk%\cmake\3.10.2.4988404
set ndk_dir=%android_sdk%\ndk-bundle
set platform=16
set cxx-flags="-fexceptions -frtti -std=c++17 -Wno-format-security"
set build_dir_prefix=libs-android-ndk
for %%x in (x86 arm64-v8a x86_64) do (
%cmake_dir%\bin\cmake.exe ^
-H. ^
-B%build_dir_prefix%\%%x ^
-DANDROID_ABI=%%x ^
-DANDROID_STL=c++_shared ^
-DANDROID_PLATFORM=android-%platform% ^
-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=bin ^
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=lib ^
-DCMAKE_BUILD_TYPE=Release ^
-DANDROID_NDK=%ndk_dir% ^
-DCMAKE_CXX_FLAGS=%cxx-flags% ^
-DCMAKE_SYSTEM_NAME=Android ^
-DCMAKE_ANDROID_ARCH_ABI=%%x ^
-DCMAKE_SYSTEM_VERSION=%platform% ^
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON ^
-DCMAKE_ANDROID_NDK=%ndk_dir% ^
-DCMAKE_TOOLCHAIN_FILE=%ndk_dir%\build\cmake\android.toolchain.cmake ^
-G Ninja ^
-DCMAKE_MAKE_PROGRAM=%cmake_dir%\bin\ninja.exe
%cmake_dir%\bin\cmake.exe --build %build_dir_prefix%\%%x
)
Tweak the 4 variables at the start to match your environment. The commandline is largly inspired from CMake | Android NDK. But note the line
-G Ninja
above as opposed to
-GAndroid Gradle - Ninja
Yeah, they changed the generator name at some point in the move from CMake 3.6 to 3.10 and never updated the docuentation. That was a fun one to figure out. If you are still on cmake 3.6 then use the second form.
Working libraries.
I haven't tried this with many libraries. For me this works with
Some build as static libraries, others as shared.