企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
github地址:[https://github.com/fymod/blog-ndk-hello-world](https://github.com/fymod/blog-ndk-hello-world) 更详细文档可[移步](http://blog.csdn.net/bingjianit/article/details/75567410) ### 配置 进入Android Studio的设置页面,选择Appearance&Behavior--System Settings--Android SDK--点击右面的SDK Tools--勾选CMake、LLDB、NDK三项--点击Apply。 ### 新建项目 第一个页面勾选上Inclode C++ support。 最后一步C++ Standard选择Toolchain Default;同时Exception和RTTI的支持也可以勾选上(不勾选也不会影响helloworld的运行)。 点击完成后,因为勾选了C++,基本配置自己都会生成好。 ### 配置 在app模块下新建文件夹cpp,然后使用Android试图查看,会发现自己生成好了native-lib.cpp。 查看native-lib.cpp的文本,发现只是返回了一个固定的字符串,这里不做修改。 在cpp文件夹下,再新建一个CMakeList.txt文件,文本内容如下 ~~~ # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. cmake_minimum_required(VERSION 3.4.1) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/cpp/native-lib.cpp ) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ~~~ ### 其他变更 打开app的build.gradle,发现自己已经添加好了externalNativeBuild等配置。这里不用动。 打开MainActivity.java,自己生成的代码很好理解。 先使用static加载native-lib,然后用natie方法获取字符串,并赋值给TextView。 ### 生成so文件 直接运行,然后查看 app--build--intermediates--cmake--debug--obj--arm64-v8a 会生成好native-lib.so文件。可以提供到其他人来使用。 ### github 可运行项目:[https://github.com/fymod/blog-ndk-hello-world](https://github.com/fymod/blog-ndk-hello-world)