Compiling c/c++ code using AndroidStudio requires creating a new project and configuring build.gradle

Use Cmake

About the use of Cmake can refer to the Cmake entry combat ** basic use steps are as follows: **

  1. Create a project directory workdir
  2. Create workdir/test.cpp
  3. Create CMakelList workdir/ cmakelist.txt
  4. Create a new build directory workdir/build (since the build process produces many temporary files, it is recommended to build in the build file)
  5. CD to the workdir/build directory and run the command “cmake..” , this process has errors mostly due to CMakeList configuration problems
  6. In the workdir/build directory, run the command “make”

Android Cmake

To use Android Cmake, you need to configure a series of parameters, which are written as a script for easy compilation: build.sh

#! /bin/bash
rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake

ANDROID_NDK_HOME="/home/fengshuo/android-ndk-r16b"  # configure your own NDK path

if [[ "$@"= ~"-d"]].then
        echo "----------------------------cmake debug----------------------------"
cmake -DDEBUG=ON -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK_HOME/build/cmake/android.toolchain.cmake \
      -DANDROID_NDK=$ANDROID_NDK_HOME \
      -DANDROID_ABI=arm64-v8a \
      -DANDROID_TOOLCHAIN=clang \
      -DANDROID_PLATFORM=android-26 \
      -DANDROID_STL=c++_static \
	  ..
else
        echo "----------------------------cmake release----------------------------"
cmake -DDEBUG=NO -DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK_HOME}/build/cmake/android.toolchain.cmake \
      -DANDROID_NDK=${ANDROID_NDK_HOME} \
      -DANDROID_ABI=arm64-v8a \
      -DANDROID_TOOLCHAIN=clang \
      -DANDROID_PLATFORM=android-26 \
      -DANDROID_STL=c++_static \
	  ..
fi

make

rm -rf CMakeCache.txt
rm -rf CMakeFiles
rm -rf cmake_install.cmake
rm -rf Makefile
rm -rf CTestTestfile.cmake

Copy the code

** Basic use steps: **

  1. Create a project directory workdir
  2. Create workdir/test.cpp
  3. Create CMakelList workdir/ cmakelist.txt
  4. Create a new build directory workdir/build (since the build process produces many temporary files, it is recommended to build in the build file)
  5. Place the build.sh script in the workdir/build directory
  6. CD go to workdir/build and run the “bash build.sh” command.

This allows you to compile c/ C ++ libraries without having to configure build.gradle or AndroidStudio