Gradle无法找到android_native_app_glue

时间:2014-10-22 03:10:20

标签: android android-ndk android-gradle

我有一个本机库,我正在尝试使用gradle构建。如果我从gradle文件调用ndk-build命令,我就可以构建本机库。但是,如果我尝试使用android gradle插件中内置的ndk构建功能,我无法构建。

我得到fatal error: android_native_app_glue.h: No such file or directory

gradle文件的相关部分是:

buildTypes.debug.jniDebugBuild true

defaultConfig {
    ndk {
      moduleName 'myModule'
      stl 'gnustl_static'
      cFlags '-UNDEBUG -Werror -Wunused-variable -Wunused-but-set-parameter -Wtype-limits -Wmissing-field-initializers -Wreturn-type -Wuninitialized'
      ldLibs 'log', 'GLESv2'
    }
    productFlavors {
      armv7{
        ndk {
          abiFilter 'armeabi-v7a'
        }
      }
    }
}

有没有办法告诉ndk构建在哪里找到android_native_app_glue.h文件?

另外,有没有办法将详细标记传递给ndk-build,相当于ndk-build V=1

4 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我通过将"-I{path_to_android-ndk}/sources/android/native_app_glue"添加到cFlags,see here

来解决此问题

答案 1 :(得分:0)

执行此操作的一种方法是将其添加到Android.mk中的LOCAL_C_INCLUDES,如下所示:

LOCAL_C_INCLUDES += /path/to/ndk/sources/android/native_app_glue

ls

上执行/path/to/ndk/sources/android/native_app_glue/native_app_glue.h,确保路径正确无误

答案 2 :(得分:0)

截至:

  • gradle这个实验性:0.6.0-α-2
  • gradle-2.9-all.zip

一种方法(或者集成任何可以编译并链接为静态库的代码)是在项目中创建一个新模块,并将该模块的gradle文件更改为:

apply plugin: 'com.android.model.library'

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

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"
        defaultConfig.with {
            minSdkVersion.apiLevel = 21
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "0.0.1"
        }
        compileOptions.with {
            sourceCompatibility=JavaVersion.VERSION_1_7
            targetCompatibility=JavaVersion.VERSION_1_7
        }
    }
    android.ndk {
        moduleName = "native-activity"
        cppFlags.add("-Werror")
        ldLibs.addAll(["log", "android"])
        stl        = "gnustl_static"
        ldFlags.add("-c")
    }
    android.sources {
        main {
            jni {
                source {
                    srcDir "${ndkDir}/sources/android/native_app_glue"
                }
            }
        }
    }
}

然后在你的模块的gradle.mk中你应该添加:

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

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

model {
    android {
        [...]
    }
    android.ndk {
        [...]

        cppFlags.add("-I${file("${ndkDir}/sources/android/native_app_glue")}".toString())
    }
    android.sources {
        main {
            jni {
                dependencies {
                    project ":native-activity" linkage "static"
                }
            }
        }
    }
}

dependencies {
    compile project(':native-activity')
}

答案 3 :(得分:0)

在您的 cMakeList.txt 文件中添加这些行

//line1
include_directories(${ANDROID_NDK}/sources/android/native_app_glue)

//line2
add_library(native_app_glue STATIC
        ${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)

还要在 native_app_glue 中添加 target_link_libraries

只有在点击RUNshift + F10

后才生效