Search code examples
javaandroidandroid-ndkjava-native-interface

JNI build gradle fails on finding ndk directory


I set ndk.dir in my local.properties file. But, when grade builds, this line

task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
        def ndkDir = android.ndkDirectory

throws an error saying that android.ndkDirectory is not set. How can I prevent myself to hard code the absolute path of ndkDirectory ?


Solution

  • This property has changed its name several times since NDK support was introduced. I am not sure it has stabilized now with the "experimental" plugin.

    Therefore I would recommend a version independent approach, invented by Riccardo Ciovati in How do i read properties defined in local.properties in build.gradle:

    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def ndkDir = properties.getProperty('ndk.dir')