使用Android Studio自带的NDK编译JNI

Posted zengjf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Android Studio自带的NDK编译JNI相关的知识,希望对你有一定的参考价值。

/**********************************************************************************
 *                      使用android Studio自带的NDK编译JNI
 * 说明:
 *     省得自己另外去下载NDK,编译JNI,麻烦。
 *              
 *                                          2017-4-30 深圳 龙华民治樟坑村 曾剑锋
 *********************************************************************************/

一、参考文档:
    1. NDK环境搭建-windows篇
        http://www.jianshu.com/p/708e6bf68ae9
    2. Mistake in documentation/error message?
        https://github.com/android-ndk/ndk/issues/264
    3. 编译一个模块时出现undefined reference to `__android_log_print 
        http://bbs.csdn.net/topics/360020076

二、error:
    1. 错误1:
        1. 错误现象:
            D:\zengjf\SourceCode\Android\android_app\Test>ndk-build
            C:/Users/zengjf/AppData/Local/Android/Sdk/ndk-bundle/build//../build/core/build-binary.mk:687: Android NDK: Module zengjf depends on undefined modules: android_runtime nativehelper cutils utils log
            C:/Users/zengjf/AppData/Local/Android/Sdk/ndk-bundle/build//../build/core/build-binary.mk:701: *** Android NDK: Aborting (set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies)    .  Stop.

            D:\zengjf\SourceCode\Android\android_app\Test>
        2. 解决方法
            1. C:\Users\zengjf\AppData\Local\Android\Sdk\ndk-bundle\build\core\build-binary.mk
            2. 修改内容:
                ...
                ifdef undefined_libs
                    $(call __ndk_warning,Module $(LOCAL_MODULE) depends on undefined modules: $(undefined_libs))

                    # https://github.com/android-ndk/ndk/issues/208
                    # ndk-build didnt used to fail the build for a missing dependency. This
                    # seems to have always been the behavior, so theres a good chance that
                    # there are builds out there that depend on this behavior (as of right now,
                    # anything using libc++ on ARM has this problem because of libunwind).
                    #
                    # By default we will abort in this situation because this is so completely
                    # broken. A user may define APP_ALLOW_MISSING_DEPS to "true" in their
                    # Application.mk or on the command line to revert to the old, broken
                    # behavior.
                    APP_ALLOW_MISSING_DEPS=true    // add this line 
                    ifneq ($(APP_ALLOW_MISSING_DEPS),true)
                        $(call __ndk_error,Aborting (set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies))
                    endif
                endif
                ...
    2. 错误2:
        1. 错误现象:
            undefined references to `__android_log_print follow
        2. 解决方法:
            Android.mk文件中加入LOCAL_LDLIBS += -llog即可

 

以上是关于使用Android Studio自带的NDK编译JNI的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | VS 自带的 Android 平台应用创建与配置 )

android studio怎么设置用32位ndk

android studio ndk-builld方式开发

如何用Android NDK编译FFmpeg

Android Studio编译开源项目(含NDK开发)常见报错

将 Android Studio 配置为使用 NDK(android-ndk-r10d,Windows 64 位)