首次在AS配置JNI

Add C/C++ codes into AS project

Paste_Image.png
  1. create jni folder under main, jni and java folder are the same parent location
  2. copy C and C++ files into jni folder
  3. create Android.mk
Paste_Image.png
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE    := gifflen   //lib name
LOCAL_SRC_FILES := gifflen.cpp  //src file
LOCAL_LDLIBS += -llog       // linked library
include $(BUILD_SHARED_LIBRARY)  //generate dynamic library

config build.gradle

Paste_Image.png

open the build.gradle under app folder

Paste_Image.png

add ndk module, the moduleName is mandatory, the ldLibs must set as above, some docs mentioned config ldLibs as ldLibs += "log", my AS will report error.

create java class for you c code

  1. create java class
Paste_Image.png
  static{    
    System.loadLibrary("gifflen");
  }

means load the library

public native void Close();
public native int AddFrame(int[] pixels);

public native means declare the functions you want to use in java

  1. change the jni name

find you pakage name in you java class

Paste_Image.png

now open the file which include the functions you want to invoke

Paste_Image.png

you'll find the JNIEXPORT name is "java_"+"package name"+"java class name"+"function name"
the picture is just the function declare, don't forget to change the real function name.

  1. if you files include C and C++ files, don't forget add
#ifdef __cplusplus
#endif 

in you C files, or the AS will report error

Generate Libs

now, you can go to the jni folder, and generate libs

Paste_Image.png

find the libs in

Paste_Image.png

because I didn't config in build.gradle which platform I prefer, so ndk compiled all platforms libs

and next you could use jni gladly

how to add NDK in AS

Paste_Image.png

click the top button, and choose NDK and ok button. AS will download NDK automatically.
if done, you can find the ndk in "Android/sdk/ndk-bundle", and then export ndk-build PATH "export PATH=$PATH:/usr/local/bin:/Users/liut1/Library/Android/sdk/ndk-bundle"

Paste_Image.png

check out the ndk path in the local.properties of your AS project, if error, correct it, if miss, add it.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市黔龟,隨后出現(xiàn)的幾起案子妇智,更是在濱河造成了極大的恐慌,老刑警劉巖氏身,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件巍棱,死亡現(xiàn)場離奇詭異,居然都是意外死亡蛋欣,警方通過查閱死者的電腦和手機航徙,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來陷虎,“玉大人到踏,你說我怎么就攤上這事。” “怎么了澈蝙?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵访忿,是天一觀的道長。 經(jīng)常有香客問我讹躯,道長菩彬,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任潮梯,我火速辦了婚禮骗灶,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘秉馏。我一直安慰自己耙旦,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布萝究。 她就那樣靜靜地躺著免都,像睡著了一般。 火紅的嫁衣襯著肌膚如雪帆竹。 梳的紋絲不亂的頭發(fā)上绕娘,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天,我揣著相機與錄音栽连,去河邊找鬼险领。 笑死,一個胖子當(dāng)著我的面吹牛秒紧,可吹牛的內(nèi)容都是我干的绢陌。 我是一名探鬼主播,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼熔恢,長吁一口氣:“原來是場噩夢啊……” “哼脐湾!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起叙淌,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤秤掌,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后鹰霍,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體机杜,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年衅谷,在試婚紗的時候發(fā)現(xiàn)自己被綠了椒拗。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,912評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖蚀苛,靈堂內(nèi)的尸體忽然破棺而出在验,到底是詐尸還是另有隱情,我是刑警寧澤堵未,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布腋舌,位于F島的核電站,受9級特大地震影響渗蟹,放射性物質(zhì)發(fā)生泄漏块饺。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一雌芽、第九天 我趴在偏房一處隱蔽的房頂上張望授艰。 院中可真熱鬧,春花似錦世落、人聲如沸淮腾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽谷朝。三九已至,卻和暖如春武花,著一層夾襖步出監(jiān)牢的瞬間圆凰,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工体箕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留专钉,地道東北人。 一個月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓干旁,卻偏偏與公主長得像驶沼,于是被迫代替她去往敵國和親炮沐。 傳聞我的和親對象是個殘疾皇子争群,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,922評論 2 361

推薦閱讀更多精彩內(nèi)容