JNI/NDK编程(一)——无参函数之Hello world !

本文源码:https://github.com/jt1024/JNIBasicForAndroid

环境

1.Android Studio 3.0
2.gradle-4.1-all
3.android sdk 26

一、在Android Studio 新建安卓工程

二、基本配置

1.配置ndk,如果没有安装,需要先安装ndk。ndk.dir=“ndk实际路径”


JNI/NDK编程(一)——无参函数之Hello world !_第1张图片
ndk01.png

2.改写build.gradle文件


JNI/NDK编程(一)——无参函数之Hello world !_第2张图片
ndk02.png

3.创建cpp文件夹、hello_lib.cpp、CMakeLists.txt
JNI/NDK编程(一)——无参函数之Hello world !_第3张图片
ndk03.png

三、编写代码

1.编写C++文件 hello_lib.cpp
路径:NDKFirstDemo/app/src/main/cpp/hello_lib.cpp

注意下面代码中 Java_com_tao_ndkfirstdemo_Java2CJNI_Java2Ccom.tao.ndkfirstdemo.Java2CJNI 的对应关系

//
// Created by tao1024 on 2018/3/15.
//

#include 
#include 

extern "C"
JNIEXPORT jstring

JNICALL
Java_com_tao_ndkfirstdemo_Java2CJNI_Java2C(JNIEnv *env, jobject instance) {

    return env->NewStringUTF("Hello world !");
}

2.编写Jave文件 Java2CJNI
路径:NDKFirstDemo/app/src/main/java/com.tao.ndkfirstdem.Java2CJNI

package com.tao.ndkfirstdemo;

/**
 * 作者: 麦典威
 * 修改时间:2018/3/15 0:02
 * 版权声明:www.ekwing.com
 * 功能: ${TODO}
 */


public class Java2CJNI {

    static {
        System.loadLibrary("hello_lib");
    }

    public native String Java2C();
}

3.编写Activity文件 HelloActivity
路径:NDKFirstDemo/app/src/main/java/com.tao.ndkfirstdemo.HelloActivity

package com.tao.ndkfirstdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class HelloActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello);
        Button btnJni = findViewById(R.id.btn_jni);
        btnJni.setOnClickListener(new View.OnClickListener() Java2CJNI   @Override
            public void onClick(View v) {
 Java2CJNIo    String result = new Java2CJNI().Java2C();
                Toast.makeText(HelloActivity.this, result, Toast.LENGTH_LONG).show();
            }
        });
    }
}

4.编写 activity_hello.xml




    

5.编写CMakeLists.txt文件,以后如果编写其他jni类,只需替换里面的“hello_lib”

# 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.
             hello_lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/hello_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.
                       hello_lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

四、编译

点击build——>Rebuild Project


ndk04.png

编译结束之后,回编译出对应的 .so 文件,如图


JNI/NDK编程(一)——无参函数之Hello world !_第4张图片
ndk05.png

五、运行程序

JNI/NDK编程(一)——无参函数之Hello world !_第5张图片
ndk06.jpg

附:JNI/NDK编程基础系列导航

JNI/NDK编程(一)——无参函数之Hello world !

JNI/NDK编程(二)——带参函数之模拟登录

JNI/NDK编程(三)——C 调用 Java 成员变量

JNI/NDK编程(四)——C 调用 Java 类中的函数/方法

JNI/NDK编程(五)——通过打印日志debug

JNI/NDK编程(六)——综合实战:通过编译lame实现wav转mp3

你可能感兴趣的:(JNI/NDK编程(一)——无参函数之Hello world !)