I tried to follow your simplified structure as close as possible.
Here is the app / build.gradle file :
apply plugin: 'com.android.library' android { compileSdkVersion 24 buildToolsVersion "25.0.1" defaultConfig { minSdkVersion 21 targetSdkVersion 24 externalNativeBuild { ndkBuild { targets "my_tool" abiFilters "armeabi-v7a" } } } externalNativeBuild { ndkBuild { path "../native/Android.mk" } } }
The native / Android.mk file is identical to yours:
LOCAL_PATH := $(call my-dir)/my_tool/src include $(CLEAR_VARS) LOCAL_MODULE := my_tool LOCAL_SRC_FILES := main.c include $(BUILD_EXECUTABLE)
I also have the native / main.c files and the minimal application /src/main/AndroidManifest.xml :
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.tool" />
I did not touch the root build.gradle script created by the Android Studio wizard:
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.3.0-alpha3' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
Now I can create a project, and here is what I get:
$> file ./app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/my_tool ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped
Android Studio shows my main.c in the cpp folder in the default view:
Update : in order to remove the executable file and pack it in the APK, you need to change the native / Android.mk :
LOCAL_PATH := $(call my-dir)/my_tool/src install: LIB_PATH := $(call my-dir)/libs include $(CLEAR_VARS) LOCAL_MODULE := my_tool LOCAL_SRC_FILES := main.c include $(BUILD_EXECUTABLE) install: $(LOCAL_INSTALLED) -mkdir $(LIB_PATH) -rm -r $(LIB_PATH) mv $< $(<:my_tool=lib-my_tool-.so) mv $(realpath $(dir $<)..) $(LIB_PATH) .PHONY: install
In addition, app / build.gradle needs some tweaking:
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "25.0.1" defaultConfig { minSdkVersion 21 targetSdkVersion 24 externalNativeBuild { ndkBuild { targets "my_tool" abiFilters "armeabi-v7a" arguments 'V=1', 'install' } } } externalNativeBuild { ndkBuild { path "../native/Android.mk" } } sourceSets { main { jniLibs.srcDirs = ['../native/libs'] } } }
It depends on the old hack , which depends on the undocumented behavior of the NDK and may be interrupted without warning when updating the NDK in the future.