一.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 //存放头文件
  2. app/main/cpp //存放cpp或者.c
    一.Andrid NDK环境搭建_第1张图片

三.新建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…)

一.Andrid NDK环境搭建_第2张图片

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

六.编写.h .c文件

Hello.h


#ifndef PRACTICLE_HELLO_H
#define PRACTICLE_HELLO_H

#include 
#include 

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();
}

运行效果:
一.Andrid NDK环境搭建_第3张图片
code:https://github.com/HumorSmith/NDKPraticle/tree/master/1_helloworld

你可能感兴趣的:(ndk)