10.1 Android NDK开发 一

2018-08-14  本文已影响0人  努力学习的安同学

标注:本文为个人学习使用,仅做自己学习参考使用,请勿转载和转发
2018-08-14: 初稿,参考SvenWang_
2018-08-24: 二稿,更新旧项目添加JNI使用CMake编译的方法

0. 引言

1. 准备工作

首先需要下载关于ndk的相关工具


2. 创建新项目直接进行ndk开发

上面的操作比较简单,在配置之后,直接得到如下的界面


下面是关于CMakeLists.txt文件

# 有关使用CMake在Android Studio的更多信息,请阅读文档:https://d.android.com/studio/projects/add-native-code.html

# 设置CMake的最低版本构建本机所需库
cmake_minimum_required(VERSION 3.4.1)

# 创建并命名库,将其设置为静态的
# 或共享,并提供其源代码的相对路径。
# 你可以定义多个library库,并使用CMake来构建。
# Gradle会自动将包共享库关联到你的apk程序。

add_library( # 设置库的名称
             native-lib     #这个根据库名更改
             # 将库设置为共享库。
             SHARED
             # 为源文件提供一个相对路径。
             src/main/cpp/native-lib.cpp )      #路径下的库名根据上面的库名更改
# 搜索指定预先构建的库和存储路径变量。因为CMake包括系统库搜索路径中默认情况下,只需要指定想添加公共NDK库的名称,在CMake验证库之前存在完成构建
find_library( # 设置path变量的名称
              log-lib
              # 在CMake定位前指定的NDK库名称
              log )
# 指定库CMake应该链接到目标库中,可以链接多个库,比如定义库,构建脚本,预先构建的第三方库或者系统库
target_link_libraries( # 指定目标库
                       native-lib        #根据上面更改库名的更改
                       # 目标库到日志库的链接 包含在NDK
                       ${log-lib} )

3. 在已有的项目的基础上进行ndk开发

public class JavaNativeUtil {

    static {
        // 设置依赖库,native-lib为依赖库的名称,可以自行修改 
        System.loadLibrary("native-lib");
    }

    public static native String hello();

}
#include <jni.h>

extern "C" {
// 这个路径参照上面创建的JavaNativeUtil的路径进行编写
JNIEXPORT jstring JNICALL Java_net_ankie_italker_jnitestdemo_JavaNativeUtil_hello(JNIEnv *env, jobject obj) {
    // 返回字符串
    return env->NewStringUTF("hello ndk");
}
}
cmake_minimum_required(VERSION 3.4.1)


add_library( # Specifies the name of the library.
# 这里是你so的名字。可以自己定义,但是在LoadLibrary的时候要对应上
             native-lib  

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
              #这里是刚才 创建的c++ 代码的名字
             src/main/cpp/Hello.cpp )

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 )

target_link_libraries( # Specifies the target library.
# 这里是你so的名字。和上面的一样
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "net.ankie.italker.jnitestdemo"
        minSdkVersion 19
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {    // 这个就是刚才添加的新的关于CMakeList.txt的添加
        cmake {
            path 'CMakeLists.txt'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

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

        Log.e(TAG, "onCreate:  " +  JavaNativeUtil.hello());
    }
}

4. 生成so文件

5. 备注

上一篇 下一篇

猜你喜欢

热点阅读