Time for action – using your own Makefiles with Gradle

Using your own handmade makefiles with Gradle is a bit tricky but not too complicated:

  1. Copy the 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.
  2. Edit app/build.gradle.
  3. Add an import for the 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'
            }
        }
  4. Still in the android section of 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 = []
          }
  5. Finally, create a new Gradle task 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'
    }
  6. Compile and install the project on your device by clicking on installDebug in the Gradle tasks view of Android Studio.

What just happened?

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.110.119