Using your own handmade makefiles with Gradle is a bit tricky but not too complicated:
Android.mk
and Application.mk
files we created in the Interfacing Java with C/C++ section in this chapter into the app/src/main/jni
directory.app/build.gradle
.OS
"Class" and remove the first ndk
section we created in the previous section:import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.packtpub.store"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
app/build.gradle
., insert a sourceSets.main
section with the following:jniLibs.srcDir
, which defines where Gradle will find the generated libraries.jni.srcDirs
, which is set to an empty array to disable native code compilation through Gradle.... sourceSets.main { jniLibs.srcDir 'src/main/libs' jni.srcDirs = [] }
ndkBuild
that will manually trigger the ndk-build
command, specifying the custom directory src/main
as the compilation directory.Declare a dependency between the ndkBuild
task and the Java compilation task to automatically trigger native code compilation:
... task ndkBuild(type: Exec) { if (Os.isFamily(Os.FAMILY_WINDOWS)) { commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath } else { commandLine 'ndk-build', '-C', file('src/main').absolutePath } } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:21.0.3' }
The Makefile generation and native source compilation performed by the Android Gradle plugin can easily be disabled. The trick is to simply indicate that no native source directory is available. We can then use the power of Gradle, which allows defining easily custom build tasks and dependencies between them, to execute the ndk-build
command. This trick allows using our own NDK makefiles, giving us more flexibility in the way we build native code.
3.138.110.119