Android学习——NDK

NDK

NDK介绍

对大部分应用开发者来说可能都不怎么接触到NDK,但如果涉及到硬件操作的话就不得不使用NDK了。使用NDK还有另一个原因,就是C/C++的效率比较高,因此我们可以把一些耗时 的操作放在NDK中实现。
NDK是Native Development Kit的简称。它是一个工具集,继承类Android的交叉编译环境,并提供了一套比较方便的Makefile,可以帮助开发者快速开发C或者C++的动态库,并自动的将so和java程序打包成apk,在Android上运行

配置NDK开发环境

NDK下载地址:https://developer.android.google.cn/ndk/downloads/index.html
解压完之后配置Path

JNI概述

JNI是Java Native Interface的缩写,中文为JAVA本地调用。JNI标准成为java平台一部分,它允许java代码和其他语言写的代码进行交互
在这里插入图片描述

JNI程序实现步骤

  1. 编写带有native声明方法java类
  2. 使用javac命令编写所编写的java类
  3. 然后使用javah+java类名生成扩展名为h的头文件
  4. 使用C/C++实现本地方法
  5. 将C/C++编写的文件生成动态链接库
  6. 测试
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    将.h文件加入到jni目录中
    在这里插入图片描述
    在jni下创建hello.c文件
#include "com_example_ndk_MainActivity.h"
JNIEXPORT jint JNICALL Java_com_example_ndk_MainActivity_add
  (JNIEnv * env, jobject obj, jint num1, jint num2){
    return num1+num2;
  }

创建Android.mk文件

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= hello
LOCAL_SRC_FILES := hello.c
include $(BUILD_SHARED_LIBRARY)

编译,在命令行输入ndk-build
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="请输入第一个数字"
        android:ems="10"
        android:id="@+id/editText_num1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:hint="请输入第二个数字"
        android:ems="10"
        android:id="@+id/editText_num2"
        android:layout_below="@+id/editText_num1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/editText_num1"
        android:layout_alignEnd="@+id/editText_num1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="加法运算"
        android:onClick="addClick"
        android:id="@+id/button"
        android:layout_below="@+id/editText_num2"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="结果:"
        android:id="@+id/textView_result"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
package com.example.ndk;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText editText_num1,editText_num2;
    private TextView textView_result;
    //加载动态链接库
    static
    {
        System.loadLibrary("hello");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_num1=findViewById(R.id.editText_num1);
        editText_num2=findViewById(R.id.editText_num2);
        textView_result=findViewById(R.id.textView_result);
    }
    //声明一个本地方法,该方法由C、C++实现
    public native int add(int num1,int num2);
    public void addClick(View view){
        String num1=editText_num1.getText().toString();
        String num2=editText_num2.getText().toString();
        //调用本地方法
        int result= add(Integer.parseInt(num1),Integer.parseInt(num2));
        textView_result.setText("结果:"+result);
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/UUUUUltraman/article/details/89917824