简体   繁体   English

如何使用build.gradle文件向Android NDK添加静态库?

[英]How can I add a static library to Android NDK using the build.gradle file?

I'm trying to learn to use the NDK in AndroidStudio, and I'd like to import the "android_native_app_glue" file used in the "native-activity" sample, so that I have a framework for basic functions like the display, touch, etc. In the sample, it loads the library with this line in the android.mk file: 我正在尝试学习在AndroidStudio中使用NDK,并且我想导入“native-activity”示例中使用的“android_native_app_glue”文件,这样我就有了一个基本功能的框架,如显示,触摸,在示例中,它使用android.mk文件中的这一行加载库:

LOCAL_STATIC_LIBRARIES := android_native_app_glue

and then imports it in "main.c" simply with: 然后使用以下命令在“main.c”中导入它:

#include <android_native_app_glue.h>

But in AndroidStudio, as far as I can tell from experimenting with it, it doesn't use the android.mk file at all and instead uses the build.gradle file to do all the same functions instead. 但是在AndroidStudio中,据我尝试使用它,它根本不使用android.mk文件而是使用build.gradle文件来代替所有相同的函数。 So, for example, to replace LOCAL_LDLIBS := ... in the android.mk, I used ldLibs = ... in the build.gradle. 因此,例如,要在android.mk中替换LOCAL_LDLIBS := ... ,我在build.gradle中使用了ldLibs = ... What gradle code replaces LOCAL_STATIC_LIBRARIES ? 什么gradle代码替换了LOCAL_STATIC_LIBRARIES

Is there also a resource somewhere that explains, in general, how to translate from android.mk to build.gradle? 在某些地方是否还有一个资源可以解释如何从android.mk转换为build.gradle?

if you really want to make it work without Makefiles, you can copy and paste ndk\\sources\\android\\native_app_glue\\android_native_app_glue.(c|h) into your jni folder and add android to your ldLibs . 如果你真的想让它在没有Makefiles的情况下工作,你可以将ndk\\sources\\android\\native_app_glue\\android_native_app_glue.(c|h)复制并粘贴到你的jni文件夹中并将android添加到你的ldLibs

Else, you can still rely on classic Makefiles, by deactivating the default call to ndk-build, and making gradle use your libs from the libs directory: 另外,您仍然可以依赖经典的Makefile,通过停用对ndk-build的默认调用,并使gradle使用libs目录中的libs

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = [] //disable automatic ndk-build call
}

In that case you'll have to call ndk-build yourself, but you can also make gradle call it for you: 在这种情况下,你必须自己调用ndk-build ,但你也可以让gradle为你调用它:

// 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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用build.gradle创建AAR文件 - How can I create aar file using build.gradle 如何使用带有Gradle的Android Studio在Android上使用NDK从C ++构建静态库? - How to build static library from C++ using NDK on Android using Android Studio with Gradle? 在build.gradle中使用&#39;model.android.ndk&#39;命令 - Using 'model.android.ndk' command in build.gradle 使用NDK库时是否需要在build.gradle文件中添加arm64-v8a - Is it necessary to add arm64-v8a in build.gradle file when using NDK libraries 在哪里可以找到build.gradle文件,以便在eclipse中添加android的支持库? - Where I can find build.gradle file in order to Add Support Libraries for android in eclipse? 如何使用android studio将TIKA添加到build.gradle中? - How to add TIKA into build.gradle using android studio? 我们如何将 Admob 和 Firebase crashlytics 添加到 Android Studio Bumble Bee Build.Gradle 文件中 - How can we add Admob and Firebase crashlytics to Android studio bumble bee Build.Gradle file 使用支持库失败构建的Android build.gradle - Android build.gradle using Support Library Failing Build 在.aar文件中添加gradle依赖项(build.gradle) Android - Add gradle dependencies(build.gradle) in .aar file Android 如何在Codename One Android项目中为build.gradle添加`apply plugin`以添加本机库? - How to add `apply plugin` to build.gradle in Codename One Android project in order to add native library?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM