2

In order to get indexing in the Android Studio editor I should add the following code in build.gradle:

ndk {
            moduleName "MyModule"
            CFlags.add("-I${file("src/main/jni/headers1")}".toString())
            CFlags.add("-I${file("src/main/jni/headers2")}".toString())
}

but then gradle ignores my Android.mk, If I will remove this code from build.gradle, then I won't get proper indexing in the editor since all the header files are in those 2 folders. Is anyone knows how to make gradle to compile by my Android.mk and and still get native editing and debugging?

Im using: Android Studio 2.1 stable gradle-experimental:0.7.0 my build.grade:

apply plugin: "com.android.model.application"

def ndkDir = System.getenv("ANDROID_NDK_HOME")
def propertiesFile = project.rootProject.file('local.properties')
if (propertiesFile.exists()) {
    Properties properties = new Properties()
    properties.load(propertiesFile.newDataInputStream())
    ndkDir = properties.getProperty('ndk.dir')
}
model {
    android.sources {
        main {
            jni {
                source {
                    srcDirs.removeAll()
                    srcDir  'src/main/none'
                }
            }
            jniLibs {
                source {
                    srcDir 'src/main/libs'
                }
            }
        }
    }
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

         ndk {
                moduleName "MyModule"
                CFlags.add("-I${file("src/main/jni/headers1")}".toString())
                CFlags.add("-I${file("src/main/jni/headers2")}".toString())
    }

        defaultConfig {
            applicationId "com.myapp.android.me"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 23
        }

        buildTypes {
            debug {
                ndk {
                    debuggable = true
                }
            }
        }
    }

}
dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])
    compile 'com.google.code.gson:gson:2.2.4'
}

task buildNative(type: Exec, description: 'Compile JNI source via NDK'){
    commandLine "${ndkDir}/ndk-build",'NDK_DEBUG=1','NDK_PROJECT_PATH ='+ getProjectDir() + '/src/main'
}
tasks.withType(AbstractCompile) {
    compileTask -> compileTask.dependsOn buildNative
}
VitalyD
  • 289
  • 1
  • 15
  • Check http://stackoverflow.com/a/32640823/192373 - if you have more specific questions, I'll answer when I get hold of a computer keyboard – Alex Cohn May 02 '16 at 16:58
  • Hi Alex, thank you for the comment, I'm trying to get native debug ability in AS and to use the native editing, but cannot get them both, I must use my Android.mk since I have assembly files to compile. If I write "moduleName MyModule" in build.gradle then native editing works but gradle ignores my Android.mk file, if I remove "moduleName MyModule"from build.gradle, gradle compile as written in my Android.mk and native debugging is working but not native editing since AS does not know where to look for the symbols (headers1 and headers2) – VitalyD May 02 '16 at 18:09
  • Have you tried the linked answer? It does exactly this, allowing native debugging and editing. You must disable the tasks if `task.name.startsWith('compile') && task.name.contains('MainC')`. I also used `task.name.endsWith("SharedLibrary")` as criterion to set `dependsOn buildNative` – Alex Cohn May 02 '16 at 19:20
  • 1
    Thank you very much Alex! your suggestion helped me! I added "task.name.startsWith('compile') && task.name.contains('MainC') task.name.endsWith("SharedLibrary")" to my build.grade and native editing, debugging start to work together and gradle compiles by my Android.mk file. again thank you! – VitalyD May 03 '16 at 07:17

1 Answers1

2

You can set up the experimental plugin to run your buildNative task instead of the built-in compile and link tasks:

tasks.all {
    task ->
        if (task.name.startsWith('compile') && task.name.contains('MainC')) {
            task.enabled = false
        }
        if (task.name.startsWith('link')) {
            task.enabled = false
        }
        if (task.name.endsWith('SharedLibrary') ) {
            task.dependsOn buildNative
        }
}
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307