title: Compile google/protobuf with ExternalProject_Add - Android
body: <p>I did create this bash script for compiling <code>protobuf</code> for Android but I don't want to have a separate build script just for this, I want it integrated in Android build in <code>CMakeLists.txt</code>.</p>
  <p>This is the script that I have for compiling <code>protobuf</code> for Android:</p>
  <pre class="lang-sh prettyprint-override"><code>export NDK=~/Android/Sdk/ndk/23.1.7779620
  export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
  export TARGET=x86_64-linux-android
  export API=26
  
  export AR=$TOOLCHAIN/bin/llvm-ar
  export CC=$TOOLCHAIN/bin/$TARGET$API-clang
  export AS=$CC
  export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
  export LD=$TOOLCHAIN/bin/ld
  export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
  export STRIP=$TOOLCHAIN/bin/llvm-strip
  
  export CFLAGS=&quot;-fpie -fpic -O3&quot;
  export CXXFLAGS=&quot;-fpie -fpic -O3&quot;
  export CPPFLAGS=&quot;-fpie -fpic -O3&quot;
  export LDFLAGS=&quot;-llog -fPIC&quot;
  
  ../protobuf-3.19.0/configure --host $TARGET --disable-shared --with-pic --with-gnu-ld
  make -j$(nproc)
  </code></pre>
  <p><code>CMakeLists.txt</code>:</p>
  <pre><code>set(ANDROID_CMAKE_ARGS
          -DANDROID_ABI=${ANDROID_ABI}
          -DANDROID_PLATFORM=${ANDROID_PLATFORM}
          -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
          -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
          -DANDROID_NDK=${ANDROID_NDK}
          -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}
          -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}
          -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
          -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
          -G${CMAKE_GENERATOR})
  
  ExternalProject_Add(
    protobuf-external
    PREFIX protobuf
    URL ${PROTOBUF_TAR_GZ}
    BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/protobuf
    CMAKE_ARGS
      &quot;-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}&quot;
      &quot;-Dprotobuf_BUILD_TESTS:BOOL=OFF&quot;
      &quot;-Dprotobuf_BUILD_EXAMPLES:BOOL=OFF&quot;
      &quot;-Dprotobuf_WITH_ZLIB:BOOL=OFF&quot;
      &quot;-DCMAKE_CXX_COMPILER:STRING=${CMAKE_CXX_COMPILER}&quot;
    SOURCE_SUBDIR cmake
    BUILD_ALWAYS 1
    STEP_TARGETS build
    INSTALL_COMMAND &quot;&quot;
  )
  
  EXternalProject_Get_Property(protobuf-external source_dir)
  
  include_directories(${source_dir}/src)
  link_directories(${CMAKE_CURRENT_BINARY_DIR}/protobuf)
  </code></pre>
  <p>In <code>.cxx/</code> only the folders are getting created but not getting compiled, what exactly am I doing wrong?</p>