20

I am defining some variables within the Android.mk file (I am passing some flags for the compiler), but every time I build my project, the Android.mk is overwritten. I am assuming that Gradle is responsible and that I should be looking there? How do I use my own Android.mk file?

Background Info:

Ubuntu 64bit, Android Studio 1.0.1, JDK7. I have wrapped my NDK version with O-LLVM NDK, and as such am editing the Android.mk file located at app/build/intermediates/ndk/debug (it's the only Android.mk file within my project dir), different to the location that the doc for O-LLVM gives examples of.

Also, there is no Application.mk file, so again I am assuming that Gradle is responsible for the calls to the compiler?


Updated information

build.gradle - (app)

//The following code until the "----" line is the new build.gradle config file
// that disables automatic Android.mk file generation

import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'com.android.application'

android {

    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.md.helloworld"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        ndk {
            moduleName "MyLib"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jni
        jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk file
    }

    // Call regular ndk-build (.cmd) script from the app directory
    task ndkBuild(type: Exec) {
        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 following code is the original Android.mk file

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.md.helloworld"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        //The only modified line
        ndk {
            moduleName "MyLib"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:21.0.3'
}
*/

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := helloWorld
LOCAL_SRC_FILES := main.c

LOCAL_LDLIBS := -static

include $(BUILD_EXECUTABLE)

Application.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

APP_ABI := armeabi

NDK_TOOLCHAIN_VERSION := clang3.4-obfuscator

include $(BUILD_EXECUTABLE)

Please note: I am not passing any cflags just yet, I am trying to get a Vanilla build working first

Cœur
  • 37,241
  • 25
  • 195
  • 267
KyleM
  • 508
  • 2
  • 4
  • 11

1 Answers1

27

yes, by default the gradle android plugin regenerates and uses its own Android.mk file to compile your sources.

You can deactivate this and use your own Android.mk file instead, by setting this inside your build.gradle configuration file:

import org.apache.tools.ant.taskdefs.condition.Os
...     
android {
    ...

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs' //set libs as .so's location instead of jniLibs
        jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
    }

    // call regular ndk-build(.cmd) script from app directory
    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
    }
}

Note that if you only need to pass your own cflags to the auto-generated Makefile, you can set these inside the cFlags "" property to set inside android { ndk {}}

ph0b
  • 14,353
  • 4
  • 43
  • 41
  • Thanks ph0b. The issue I am having now is that the call to ndk-build within the build.gradle configuration file does not compile, so I removed that line and removed the dependency of the Java compiler, and run ndk-build from the command line. This builds the ndk fine, however I no longer have the .so files generated. Any pointers or tips? – KyleM Jan 09 '15 at 04:30
  • 1
    I don't see why it wouldn't compile, maybe you've forgotten the `import org.apache.tools.ant.taskdefs.condition.Os` ? the .so are generated by ndk-build, by default inside libs/(armeabi|armeabi-v7a|x86|mips|...). check that 'src/main/libs' path is correct and that your .so files are there. – ph0b Jan 09 '15 at 11:59
  • 1
    Hi ph0b, I have fixedd the compilation error, ndk-build wasn't available as a command, so that is sorted. The issue I am having now is that the .so files are not being generated anywhere. I have searched for them within the entire project directory, but no luck since building the project with the new gradle file. The project builds fine, but when trying to run, there is an UnsatisfiedLinkError as it cannot find the libMyLib.so file. I have updated my question with my gradle.build file and both the Android.mk file and Application.mk file. Many thanks ph0b – KyleM Jan 12 '15 at 04:41
  • 1
    You have to replace `BUILD_EXECUTABLE` by `BUILD_SHARED_LIBRARY`. Application.mk should only have the `APP_ABI` and `NDK_TOOLCHAIN_VERSION` declarations in your case. You should also set APP_ABI to `all` in order to generate your lib for all the supported cpu architectures. – ph0b Jan 12 '15 at 09:07
  • Thanks ph0b, all working now. For anyone else trying this I also had to set the LOCAL_LDLIBS to -shared and had to set LOCAL_MODULE to MyLib to match my Java file declaration. If anyone needs me to upload the final files to allow obfuscation of native code with the new Android Studio I am happy to do so. I am just debugging building for an architecture that isn't arm so that we can run it on an x86 emulator! – KyleM Jan 15 '15 at 21:47
  • @KyleM, I am new to Gradle and would appreciate you uploading your final files to help us debug! Thank you – beyondtheteal May 26 '15 at 18:02
  • @ph0b with ndk-build command line I am able to generate .so files in my libs folder but I am not able get header files(I am using gradle experimental plugin) For more details please look here http://stackoverflow.com/questions/36567079/ffmpeg-with-gradle-experimental-android-plugin – Pushpendra Apr 13 '16 at 04:17
  • Is it possible to represent the following lines from build.gradle in Android.mk testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" androidTestCompile 'com.android.support.test:runner:0.4.1' androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' I'm stuck big time in this step. Can anybody please help me with this? – Ambi Aug 08 '16 at 13:10