基于Android studio3.6的JNI教程之helloworld思路詳解
jdk環(huán)境變量配置:
path中增加下面2個(gè)路徑,也就是android studio的路徑,android有自帶的jdk。
E:\Android\Android Studio\jre\bin E:\Android\Android Studio\bin
新建工程:
一定要選擇Native c++類型,最后要選c++11支持。

SDK設(shè)置:
File->Settings

File->Project Structure

首先確定工程的目錄結(jié)構(gòu),然后嘗試運(yùn)行一下工程,使用模擬器,確保工程沒問題,

在MainActivity的同級(jí)目錄,新建一個(gè)hello.java,然后做一個(gè)簡單的實(shí)現(xiàn),
package com.example.myapplication;
public class hello {
public native int add(int i, int j);
}
使用android studio自帶的Terminal進(jìn)入該hello.java所在目錄,執(zhí)行,
javac hello.java
Terminal下回到app/src/main所在目錄,執(zhí)行,
javah -d jni -classpath ./java com.example.myapplication.hello
此時(shí),會(huì)在main目錄下面生成一個(gè)和cpp,java同級(jí)的目錄jni。
在該目錄結(jié)構(gòu)里面新建hello.cpp。
將com_example_myapplication_hello.h中的內(nèi)容復(fù)制進(jìn)hello.cpp中,并且進(jìn)行方法的實(shí)現(xiàn),
#include <jni.h>
/* Header for class com_example_myapplication_hello */
#ifndef _Included_com_example_myapplication_hello
#define _Included_com_example_myapplication_hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_myapplication_hello
* Method: add
* Signature: (II)I
*/
#include "com_example_myapplication_hello.h"
JNIEXPORT jint JNICALL Java_com_example_myapplication_hello_add
(JNIEnv *, jobject, jint i, jint j){return i+j;}
#ifdef __cplusplus
}
#endif
#endif
將com_example_myapplication_hello.h,hello.cpp這連個(gè)文件復(fù)制到cpp所在的目錄,
然后修改CMakeLists.txt,增加,
add_library( # Sets the name of the library. hello # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). hello.cpp )
修改target_link_libraries如下,
target_link_libraries( # Specifies the target library.
native-lib
hello
# Links the target library to the log library
# included in the NDK.
${log-lib} )
修改hello.java的調(diào)用方式,
package com.example.myapplication;
public class hello {
static {
System.loadLibrary("hello");
}
public native int add(int i, int j);
}
修改MainActivity.java中的onCreate函數(shù),
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = findViewById(R.id.sample_text);
//tv.setText(stringFromJNI());
tv.setText("hello 9+10= " + new hello().add(9, 10));
}
然后,rebuild project,沒有錯(cuò)誤后,然后run app。
最終程序整體目錄結(jié)構(gòu),以及運(yùn)行效果,

JNI的整體流程思路:
Java先定義一個(gè)類,類中定義一個(gè)需要c++來實(shí)現(xiàn)的方法.通過javah生成需要c++實(shí)現(xiàn)的.h的c++頭文件實(shí)現(xiàn).h的c++頭文件中定義的方法Cmake編譯運(yùn)行
總結(jié)
到此這篇關(guān)于基于Android studio3.6的JNI教程之helloworld思路詳解的文章就介紹到這了,更多相關(guān)android studio JNI helloworld內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法
這篇文章主要介紹了Android應(yīng)用中使用DOM方式解析XML格式數(shù)據(jù)的基本方法,值得注意的是DOM方式解析的效率并不高,在數(shù)據(jù)量大的時(shí)候并不推薦使用,需要的朋友可以參考下2016-04-04
Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作
這篇文章主要介紹了Android數(shù)據(jù)庫SD卡創(chuàng)建和圖片存取操作的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android碎片fragment實(shí)現(xiàn)靜態(tài)加載的實(shí)例代碼
這篇文章主要介紹了Android碎片fragment實(shí)現(xiàn)靜態(tài)加載的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11
Android?nonTransitiveRClass資源沖突問題淺析
這篇文章主要介紹了Android?nonTransitiveRClass資源沖突問題,別搞錯(cuò)了,nonTransitiveRClass不能解決資源沖突,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12
Android UI設(shè)計(jì)系列之自定義SwitchButton開關(guān)實(shí)現(xiàn)類似IOS中UISwitch的動(dòng)畫效果(2
這篇文章主要介紹了Android UI設(shè)計(jì)系列之自定義SwitchButton開關(guān)實(shí)現(xiàn)類似IOS中UISwitch的動(dòng)畫效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果實(shí)例
我想看到今日頭條的點(diǎn)贊效果,應(yīng)該都覺得很絢麗吧,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果的相關(guān)資料,文中通過示例代價(jià)介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02

