1. 程式人生 > >一.Andrid NDK環境搭建

一.Andrid NDK環境搭建

本文使用將Cmake來整合和演示

一.給新建的普通工程加上cmake支援

在app/build.gradle

android{
  defaultConfig{
      ....
      externalNativeBuild {
            cmake {
                cppFlags "" //暫不瞭解具體用途
            }
        }
        ....
	}

   externalNativeBuild {
        cmake {
            path "CMakeLists.txt" //cmake檔案的路徑
} } }

新增完之後,你的app/build.gradle應當如下

android {
    compileSdkVersion 28
    buildToolsVersion "28.0.2"
    defaultConfig {
        applicationId "com.example.a1_opencv_helloworld"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild { cmake { cppFlags "" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } externalNativeBuild {
cmake { path "CMakeLists.txt" } } }

二.新建cpp檔案

  1. app/main/cpp/include //存放標頭檔案

三.新建CMakeLists.txt

  1. 路徑:app/CMakeLists.txt
  2. CMakeList.txt簡單說明
# 指定cmke版本
cmake_minimum_required(VERSION 3.4.1)
add_library(native-lib
        SHARED
        src/main/cpp/Hello.c) #新增.c原始檔
include_directories(src/main/cpp/include) #新增標頭檔案的位置
target_link_libraries(native-lib
        ${log-lib}) #選

四.在MainActivity中.載入so庫

public class MainActivity extends AppCompatActivity {

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

    static {
        System.loadLibrary("native-lib");
    }
}

五.native層新增helloworld方法

  1. 在java層新增native方法

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG,getHello());
    }

    static {
        //載入靜態庫,靜態庫的名字,再之前的CMakeList.txt指定
        System.loadLibrary("native-lib");
    }

    //新增native方法
    public native  String getHello();
}
  1. 生成標頭檔案有兩種方式
  • 直接在android studio 在方法上生成(滑鼠放在方法上,alt+enter,出現create_function…)

在這裡插入圖片描述

  • 使用javah -d (新版本jdk中 Java9-Java10已經不支援)

六.編寫.h .c檔案

Hello.h


#ifndef PRACTICLE_HELLO_H
#define PRACTICLE_HELLO_H

#include <android/log.h>
#include <jni.h>

JNIEXPORT jstring JNICALL
Java_com_example_a1_1opencv_1helloworld_MainActivity_getHello(JNIEnv *env, jobject instance);
#endif //PRACTICLE_HELLO_H

Hello.c

#include "Hello.h"

JNIEXPORT jstring JNICALL
Java_com_example_a1_1opencv_1helloworld_MainActivity_getHello(JNIEnv *env, jobject instance) {
   return (*env)->NewStringUTF(env, "native hello world");
}

七.MainActivity修改

public class MainActivity extends AppCompatActivity {
    public static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView contentTv = findViewById(R.id.tv_content);
        contentTv.setText(getHello());
    }

    static {
        //載入靜態庫
        System.loadLibrary("native-lib");
    }

    //新增native方法
    public native  String getHello();
}