NDK开发项目工作(一)

  • 新建一个NDK项目,没有的自动下载

    《NDK开发项目工作(一)》 image.png

要勾选IncludeC++ support,之后的选项都是默认的, 不需要在勾选什么配置。
BuildProject的时候可能会出现Build失败的情况,可能是cmake没有下载。在sdk tools里面下载重新Build项目即可。

  • 新增的目录结构

    《NDK开发项目工作(一)》 新增的文件

.externalNativeBuild文件夹不用管,系统自动生成的
cpp文件夹,包含的c文件,可自行创建或者修改代码
CMakeList.txt文件是cmake的配置文件

  • 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} )

有关于CMakeList.txt文件需要注意的是:
add_library:创建一个library,添加C/C++文件;find_library:表示依赖NDK中的库,一般不需要手动去更改;target_link_libraries:表示将目标库与NDK中的库进行连接。如果有多个库时,可以添加多个add_library和target_link_libraries。

生成的文件,基本就介绍完了。再看看build.gradle新增的

  compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.administrator.myapplication"
        minSdkVersion 26
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //新增的NDK,默认是空串,如果要自定义,这边需要修改
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {//新增的NDK
        cmake {
            path "CMakeLists.txt"
        }
    }
  • MainActivity的调用
public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");//这个是cpp里面使用的lib名称,以及cmake里面配置的名称
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();//这个是native-lib里面的方法名称
}

这就是简单的构建,如果需要开发的话,需要个人学习c语言

    原文作者:她的梦z
    原文地址: https://www.jianshu.com/p/db12724cbd77
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞