title: Compile and use ABI-dependent executable binaries in Android with Android Studio 2.2 and CMake
body: <p>I'm testing out the new Android Studio C/C++ building via CMake through stable gradle (<a href="http://tools.android.com/tech-docs/external-c-builds" rel="noreferrer">http://tools.android.com/tech-docs/external-c-builds</a>).</p>
  
  <p>In my app, an already rooted device needs to use an ABI-dependent binary that I compile inside Android Studio.</p>
  
  <p>When I try to compile a standard library with </p>
  
  <pre><code>add_library(mylib SHARED mylib.c)
  </code></pre>
  
  <p>it gets automatically compiled and copied inside the lib/[ABI] folder of the APK (e.g. /lib/armeabi/mylib.so) but if I compile an executable binary with:</p>
  
  <pre><code>add_executable(mybinary mybinary.cpp)
  </code></pre>
  
  <p>binaries are corectly generated inside the build folder: </p>
  
  <pre><code>app/build/intermediates/cmake/debug/lib/armeabi/mybinary
  app/build/intermediates/cmake/debug/lib/x86_64/mybinary 
  ...
  </code></pre>
  
  <p>but they do not seem to be copied anywhere inside the apk.</p>
  
  <p>Which is the correct way to handle this need? Is a gradle-task the way to go?</p>
  
  <p>build.gradle:</p>
  
  <pre><code>apply plugin: 'com.android.application'
  
  android {
      compileSdkVersion 24
      buildToolsVersion "24.0.1"
      defaultConfig {
          applicationId "com.my.app"
          minSdkVersion 10
          targetSdkVersion 24
          versionCode 1
          versionName "1.0"
          testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
          externalNativeBuild {
              cmake {
                  cppFlags ""
              }
          }
      }
      buildTypes {
          release {
              minifyEnabled false
              proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
          }
      }
      externalNativeBuild{
          cmake{
              path "CMakeLists.txt"
          }
      }
  
      defaultConfig {
          externalNativeBuild {
              cmake {
                  targets "
                  arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_PLATFORM=android-21"
                  cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
                  cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
                  abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a'
              }
          }
      }
  }
  
  dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
          exclude group: 'com.android.support', module: 'support-annotations'
      })
      testCompile 'junit:junit:4.12'
      compile 'com.android.support:appcompat-v7:24.1.1'
      compile 'com.android.support:design:24.1.1'
      compile 'com.android.support:recyclerview-v7:24.1.1'
      compile 'eu.chainfire:libsuperuser:1.0.0.201607041850'
  }
  </code></pre>
  
  <p>CMakeLists.txt</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.4.1)
  
  set(CMAKE_VERBOSE_MAKEFILE on)
  
  add_executable(mybinary ${CMAKE_CURRENT_SOURCE_DIR}/mybinary.cpp)
  target_link_libraries( mybinary libcustom)
  target_include_directories (mybinary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
  </code></pre>
  
  <p>mybinary.cpp</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[]) {
  
      string hello = "Hello from C++";
      cout &lt;&lt; "Message from native code: " &lt;&lt; hello &lt;&lt; "\n";
  
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>How the app should interact with mybinary:</p>
  
  <pre><code>import eu.chainfire.libsuperuser.Shell;
  ...
  Shell.SU.run("/path/to/mybinary");
  </code></pre>