• Android ndk开发入门集锦一


    1、搭建NDK环境

    1.1首先配置ndk环境变量如下图输出 如何配置ndk环境变量这里不再赘述 请自行百度

    1.2.如果没有ndk环境需要在AndroidStudio中下载如下图

     1.3通过这里下载NDK下载 选择自己对应电脑系统位数下载即可 比如32bit或者64bit 这里不再赘述如下图所示 下载好解压默认一般放在Android sdk根目录

    1.4 NDK版本选择
     

    2、添加c++支持库

    新建一个Project工程 选择c++ 然后选择对应的版本 这里需要注意是你的c++Standard支持库与ndk版本一致 否则会出现编译异常 点击finish就会开始构建项目

    3、配置app/build.gradle文件

    配置app build.gradle 主要配置ndk的 path ndk版本号 以及abiFilters 芯片支持类型

    abiFilters 'armeabi-v7a', 'arm64-v8a'

     3.1选择对应cmake版本

      3.2CmakeLists.txt文件

    1. # For more information about using CMake with Android Studio, read the
    2. # documentation: https://d.android.com/studio/projects/add-native-code.html
    3. # Sets the minimum version of CMake required to build the native library.
    4. cmake_minimum_required(VERSION 3.10.2) 对应cmake版本号
    5. # Declares and names the project.
    6. project("myapplication") 项目名称
    7. # Creates and names a library, sets it as either STATIC
    8. # or SHARED, and provides the relative paths to its source code.
    9. # You can define multiple libraries, and CMake builds them for you.
    10. # Gradle automatically packages shared libraries with your APK.
    11. add_library( # Sets the name of the library.
    12. face-lib
    13. # Sets the library as a shared library.
    14. SHARED
    15. # Provides a relative path to your source file(s).
    16. face-lib.cpp Facer.h Facer.cpp)
    17. #这里指添加cpp文件 h头文件你的加载库文件
    18. # Searches for a specified prebuilt library and stores the path as a
    19. # variable. Because CMake includes system libraries in the search path by
    20. # default, you only need to specify the name of the public NDK library
    21. # you want to add. CMake verifies that the library exists before
    22. # completing its build.
    23. find_library( # Sets the name of the path variable.
    24. log-lib
    25. # Specifies the name of the NDK library that
    26. # you want CMake to locate.
    27. log )
    28. //这里指找到cmake库名字
    29. # Specifies libraries CMake should link to your target library. You
    30. # can link multiple libraries, such as libraries you define in this
    31. # build script, prebuilt third-party libraries, or system libraries.
    32. target_link_libraries( # Specifies the target library.
    33. face-lib
    34. # Links the target library to the log library
    35. # included in the NDK.
    36. ${log-lib} )
    37. //关联ndk动态链接so库


    4、编写jni代码 cpp文件

    1. #include <jni.h> 导入jni头文件
    2. #include <string> 导入字符串库
    3. #include "Facer.h" 导入Facer.h头文件
    4. extern "C" JNIEXPORT jstring JNICALL
    5. //这里必须全路径否则就会找不到对应的类 包名+类名+方法名
    6. Java_com_example_myapplication_Facer_getFacer(JNIEnv *env, jclass clazz, jstring top,
    7. jstring bottom, jstring right,jstring brow,jstring eyes,
    8. jstring hand) {
    9. Facer facer(//使用 env->GetStringUTFChars将jstring转化为string
    10. env->GetStringUTFChars(top, 0),
    11. env->GetStringUTFChars(bottom, 0),
    12. env->GetStringUTFChars(right,0),
    13. env->GetStringUTFChars(brow, 0),
    14. env->GetStringUTFChars(eyes, 0),
    15. env->GetStringUTFChars(hand, 0)
    16. );
    17. return env->NewStringUTF(facer.getFace().c_str());//返回对应字符串
    18. }


    5、添加头文件

    1. //
    2. // Created by zm-pc on 2022/7/2.
    3. //
    4. #include <iostream>
    5. using namespace std;
    6. #ifndef MY_APPLICATION_FACE_H
    7. #define MY_APPLICATION_FACE_H
    8. class Facer {
    9. public:
    10. Facer(const string &top="-",const string &bottom="-",const string &right="%",
    11. const string &brow="~", const string &eyes=".",const string &hand="$");
    12. ~Facer();
    13. public:
    14. string top;
    15. string bottom;
    16. string right;
    17. string brow;
    18. string eyes;
    19. string hand;
    20. public:
    21. void printFace();
    22. string getFace();
    23. };
    24. #endif //MY_APPLICATION_FACE_H


    6、编译so库

    6.1Build->MakeProject(Window快捷键Ctrl+F9) 然后去build->intermediates->cmake->debug->obj对应不同芯片so

    6.2配置多平台交叉编译so


    7、加载so库 调用native方法

     这里只是简单演示 主要属性ndk开发流程以及配置


    8、结束总结

         8.1 NDK版本与c++要对应

         8.2 cmake要与android studio ndk也要对应

         8.3 需要配置环境变量 

         8.4 最好不要选最新cmake或者ndk环境

         8.5编译出现问题一定要细心检查 不要慌 仔细检查配置

         转载请注明出处 谢谢!Android ndk开发入门集锦一_KdanMin的博客-CSDN博客

  • 相关阅读:
    535. TinyURL 的加密与解密
    vim恢复.swp [BJDCTF2020]Cookie is so stable1
    聊聊FASTER和进程内混合缓存
    Java.lang.Character类中codePointBefore(char[ ] a, int index)方法具有什么功能呢?
    C/C++多进程高并发框架分享【内附可执行源码注释完整】
    高压放大器在静电喷涂技术中的应用
    十、MFC控件(二)
    Spring Boot 2.x系列【17】功能篇之JSON
    尚硅谷大数据数仓项目superset db upgrade三个报错解答
    智能合约漏洞案例,DEI 漏洞复现
  • 原文地址:https://blog.csdn.net/qq_15950325/article/details/125571175