午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

基于Android studio3.6的JNI教程之helloworld思路詳解

瀏覽:99日期:2022-09-26 15:13:05

jdk環(huán)境變量配置:

path中增加下面2個(gè)路徑,也就是android studio的路徑,android有自帶的jdk。

E:AndroidAndroid StudiojrebinE:AndroidAndroid Studiobin

新建工程:

一定要選擇Native c++類型,最后要選c++11支持。

基于Android studio3.6的JNI教程之helloworld思路詳解

SDK設(shè)置:

File->Settings

基于Android studio3.6的JNI教程之helloworld思路詳解

File->Project Structure

基于Android studio3.6的JNI教程之helloworld思路詳解

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

基于Android studio3.6的JNI教程之helloworld思路詳解

在MainActivity的同級(jí)目錄,新建一個(gè)hello.java,然后做一個(gè)簡(jiǎn)單的實(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 __cplusplusextern '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)行效果,

基于Android studio3.6的JNI教程之helloworld思路詳解

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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 中文字幕影音先锋 | 四虎4hu永久免费入口 | 日韩在线免费 | 国产a级淫片 | 三级福利视频 | 午夜寂寞影视 | 欧美黑粗硬 | 中文字幕99 | 亚洲人成在线观看 | www日韩精品| 中文字幕av第一页 | 正在播放jul一色桃子026 | 久久久久无码国产精品一区 | av黄色在线免费观看 | 日产精品久久久一区二区 | 蜜桃精品久久久久久久免费影院 | 最新国产视频 | 成人影片在线免费观看 | 日韩aaaaaa| 午夜在线观看影院 | 日韩成人中文字幕 | 99视频导航 | 中文字幕在线观看网站 | 欧美日本一区二区 | 中文字幕一区二区在线视频 | 99伊人 | 97久久久| 中文字幕高清在线免费播放 | 性高潮网站 | 最近2019中文字幕大全第二页 | 美女中文字幕 | 亚洲激情视频在线观看 | 成人国产视频在线观看 | 国产精品麻豆免费版 | 欧美不卡影院 | 日本色网址| 午夜国产小视频 | www.五月婷| 天天天天操 | 国产国语对白 | 亚洲欧美综合在线观看 |