一、JNI開發
為了使Java代碼能夠直接與Native 互動,Android提供了JNI技術,
關于概念性的東東直接百度了解一下好了,
二、在Android中開始JNI開發
首先Android Studio中新建一個JNI專案
在gradle檔案下添加:
sourceSets {
main {
jni {
srcDirs 'src\\main\\jni'
}
}
}
構建一個jni檔案夾,用于 存放自己的cpp檔案

然后建立一個CMakeLists.txt檔案,用來編譯生成lib
# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
cmake_minimum_required(VERSION 3.4.1)
# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add_library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
add_library( # Specifies the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/jni/{替換成自己的cpp檔案名}.cpp)
find_library( # Defines the name of the path variable that stores the
# location of the NDK library.
log-lib
# Specifies the name of the NDK library that
# CMake needs to locate.
log )
# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the log library to the target library.
${log-lib} )
然后在java檔案 下建立自己要呼叫的方法介面檔案

然后實作這個介面即可,
public class PeopleAction implements Action {
static {
System.loadLibrary("native-lib");
}
public native void run();
public native void say();
public native void eat();
}
這樣就可以啦,整挺好,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/378242.html
標籤:其他
