Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build CMake library without Android-Studio (by command-line or GUI)

It seems, Android-Studio sets specific CMake options, And I can not build with command-line (outside of Android-Studio) no matter what I tried!!

Is there any way to build an Android project's CMake library without even opening Android-Studio?

Note: I already found solution and will share answer to this shortly.

like image 395
Top-Master Avatar asked May 29 '26 11:05

Top-Master


1 Answers

Yes there were some options and/or variables that need to be set for CMake command-line build to work (without even opening Android-Studio).

1. Firstly, create the my-toolchain.cmake file (beside your project, maybe in config dir), with content below:

#
# Allows compiling outside of Android-Studio.
#

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

set(CMAKE_TOOLCHAIN_DIRECTORY "$ENV{ANDROID_HOME}/ndk-bundle/build/cmake")
if(NOT CMAKE_TOOLCHAIN_FILE)
  if(NOT ANDROID_ABI)
    # Default to 32 Bit ARMv7 CPU.
    set(ANDROID_ABI "armeabi-v7a")
  endif()
  if(NOT ANDROID_PLATFORM)
    set(ANDROID_PLATFORM "android-15")
  endif()
  set(CMAKE_TOOLCHAIN_FILE "${CMAKE_TOOLCHAIN_DIRECTORY}/android.toolchain.cmake")
endif()

if(WIN32 AND NOT CMAKE_MAKE_PROGRAM)
  set(CMAKE_MAKE_PROGRAM "$ENV{ANDROID_HOME}/ndk-bundle/prebuilt/windows/bin/make.exe" CACHE INTERNAL "" FORCE)
endif()

2. Secondly, Add the file created in last step into your project, for example:

cmake_minimum_required(VERSION 3.2)

# Detect toolchain.
include(config/my-toolchain.cmake)

project(MyProject C CXX)

# ... and so on ...

3. Finally, in console cd where your CMakeLists.txt file is, and build with commands like:

cmake -H. -B .build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DANDROID_PLATFORM=android-14 -D ANDROID_ABI=armeabi-v7a

cd .build

"%ANDROID_HOME%\ndk-bundle\prebuilt\windows\bin\make.exe" install INSTALL_ROOT="%CD%\.build"

Note that last command of Step-3 changes based on your operating-system, but above should work for Windows if you have ANDROID_HOME environment-variable set to Android-SDK root directory (which of course, needs NDK extracted in ndk-bundle sub-dir).

All done!
Just repeat Step-3 for each CPU architecture, i.e. change ANDROID_ABI to one of possible options:
armeabi-v7a, arm64-v8a, x86, x86_64, mips, mips64

Although, mips and mips64 are deprecated by now (and Android NDK r16b was last version supporting them).

like image 184
Top-Master Avatar answered May 31 '26 06:05

Top-Master



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!