Android项目简单集成NDK开发步骤

2021-08-16  本文已影响0人  伪装的狼

Android项目集成NDK开发步骤

  1. App内build.gradle文件添加

    defaultConfig {
            applicationId "com.damao.ndkdevdemo"
            minSdkVersion 16
            targetSdkVersion 30
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            
            //添加下面这句
            externalNativeBuild {
                cmake {
                    cppFlags ""
                }
            }
        }
        
    在defaultConfig同级添加:
    externalNativeBuild {
            cmake {
                path "src/main/cpp/CMakeLists.txt"
                version "3.10.2"
            }
        }
        ndkVersion '21.1.6352462'
    
  1. 在java同级目录新建文件夹cpp,然后右键,选择MarkDirectory as,再选择Sources Root

    # 在cpp目录下新建CMakeLists.txt文件,粘贴下面的内容:
    # CMakeLists.txt文件主要是用来编写编译的内容
    # 注意:project("ndkdevdemo")中的内容要改为当前工程的名称
    
    # 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.10.2)
    
    # Declares and names the project.
    
    project("ndkdevdemo")
    
    # 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
                
                # 在这里添加新建的cpp文件名称,这样就加入了编译
                 # Provides a relative path to your source file(s).
                 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} )
    
  1. 在cpp目录下新建native-lib.cpp文件

    //粘贴以下内容,这样一个C++函数就编写好了
    //注意:com_damao_ndkdevdemo需要改为项目的包名,其中.用_表示
    
    #include <jni.h>
    #include <string>
    
    extern "C" JNIEXPORT jstring JNICALL
    Java_com_damao_ndkdevdemo_MainActivity_stringFromJNI(
            JNIEnv* env,
            jobject /* this */) {
        std::string hello = "Hello from C++";
        return env->NewStringUTF(hello.c_str());
    }
    
  1. 在MainActivvity添加以下代码,表示导入C++开发库

    companion object {
            // Used to load the 'native-lib' library on application startup.
            init {
                System.loadLibrary("native-lib")
            }
        }
    
  1. 定义native函数

        /**
         * A native method that is implemented by the 'native-lib' native library,
         * which is packaged with this application.
         */
        external fun stringFromJNI(): String
    
  1. 调用定义的native函数

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // Example of a call to a native method
            findViewById<TextView>(R.id.sample_text).text = stringFromJNI()
        }
    
  1. 如果集成之后,MainActivity中的Native函数编译通过了,也能跑起来,但是在AS中不识别,点击不会自动跳转到对应的C++文件,可以在build.gradle中的externalNativeBuild里面的cmake内的path路径,把cpp删除,同步一下,再添加回去,然后同步就可以了。
上一篇 下一篇

猜你喜欢

热点阅读