# android程序调用JNI

OS: MAC OS 10.7.5
Android: 4.3
NDK: android-ndk-r7c
java: 1.6.0_51

# 使用JNI

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView android:id="@+id/showText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>


MainActivity.java:

package net.lnmcc.usejni;

import android.os.Bundle;
import android.app.Activity;
import android.widget.TextView;

public class MainActivity extends Activity {
// 加载JNI库
static {
}
// 定义JNI方法
public native String getStringFromNative();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 调用JNI方法
String strResult = getStringFromNative();
TextView tv = (TextView)findViewById(R.id.showText);
tv.setText(strResult);
}

@Override
return true;
}

}


$javah -classpath bin/classes/ \ > -d jni \ > com.lnmcc.usejni.MainActivity #前缀一定是你的包名，否则找不到class文件  如果没有错误，javah就会自动生成net_lnmcc_usejni_MainActivity.h /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class net_lnmcc_usejni_MainActivity */ #ifndef _Included_net_lnmcc_usejni_MainActivity #define _Included_net_lnmcc_usejni_MainActivity #ifdef __cplusplus extern "C" { #endif /* * Class: net_lnmcc_usejni_MainActivity * Method: getStringFromNative * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_net_lnmcc_usejni_MainActivity_getStringFromNative (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif  ## 实现JNI函数 在project/jni文件夹中新建C源代码文件，JNI函数的命名规则为： JAVA_包名_类名_函数名 useJNI.c: #include "net_lnmcc_usejni_MainActivity.h" JNIEXPORT jstring JNICALL Java_net_lnmcc_usejni_MainActivity_getStringFromNative (JNIEnv *env, jobject obj) { return (*env)->NewStringUTF(env, (char*)"Hello, World"); }  ## 创建Android.mk 在project/jni/文件夹中创建Android.mk文件: LOCAL_PATH:=$(call my-dir) #项目根目录

include $(CLEAR_VARS) ＃除了LOCAL_PATH,清除其它LOCAL_变量的值 LOCAL_MODULE:=useJNI LOCAL_SRC_FILES:=useJNI.c include$(BUILD_SHARED_LIBRARY) # 编译成动态库


## 编译JNI库

$cd project/jni$ ndk-build #这是NDK-r7c提供的工具，确保在环境变量中


Compile thumb  : useJNI <= useJNI.c
SharedLibrary  : libuseJNI.so
Install        : libuseJNI.so => libs/armeabi/libuseJNI.so


## 测试程序

### 12 replies ›

1. I wanted to develop a quick remark to be able to appreciate you for the splendid facts you are placing here. My extended internet investigation has at the end of the day been recognized with sensible strategies to exchange with my best friends. I ‘d mention that most of us visitors are extremely blessed to dwell in a wonderful network with so many marvellous professionals with interesting pointers. I feel extremely lucky to have used your web page and look forward to some more entertaining moments reading here. Thank you once more for everything.

2. I simply wanted to type a brief comment so as to express gratitude to you for all of the great items you are writing on this site. My particularly long internet look up has at the end of the day been paid with good facts and strategies to talk about with my contacts. I ‘d state that that we site visitors actually are unquestionably lucky to live in a fantastic community with many marvellous people with very helpful suggestions. I feel extremely lucky to have seen the website page and look forward to tons of more enjoyable moments reading here. Thanks a lot once more for a lot of things.

3. I have to express some thanks to you for rescuing me from this type of incident. Just after exploring through the search engines and finding solutions that were not productive, I thought my entire life was over. Existing minus the solutions to the difficulties you have fixed through your good website is a critical case, as well as ones which could have in a wrong way affected my career if I had not come across your blog post. Your actual natural talent and kindness in touching every aspect was vital. I don’t know what I would’ve done if I hadn’t come upon such a subject like this. It’s possible to at this point look forward to my future. Thank you so much for the reliable and results-oriented help. I won’t hesitate to propose your web site to anyone who would like counselling on this subject.

4. I’m just writing to let you understand what a perfect encounter my cousin’s daughter undergone browsing yuor web blog. She figured out many things, not to mention what it’s like to possess an excellent helping mood to make the rest without hassle understand various very confusing issues. You actually surpassed people’s expected results. Thank you for providing such good, trustworthy, informative and in addition fun tips on this topic to Jane.

5. I simply had to say thanks yet again. I am not sure what I would’ve sorted out in the absence of those basics contributed by you over my concern. It previously was a very traumatic crisis in my opinion, however , witnessing your specialized form you dealt with it made me to cry over fulfillment. Extremely grateful for your assistance and even sincerely hope you comprehend what a great job that you’re getting into training many people through the use of your web page. I am sure you’ve never got to know all of us.

6. I happen to be writing to let you be aware of of the cool encounter my wife’s daughter enjoyed studying your site. She picked up numerous things, not to mention what it’s like to have an ideal teaching style to make the others quite simply know just exactly a number of extremely tough matters. You truly exceeded visitors’ expectations. I appreciate you for showing such interesting, safe, informative and easy thoughts on the topic to Jane.

7. I wanted to send you that little word just to give thanks the moment again for those breathtaking advice you have shown on this page. It’s tremendously open-handed with you to deliver without restraint precisely what a few individuals could have offered for sale for an e book to earn some cash on their own, most importantly since you might have tried it if you desired. These advice also worked to provide a easy way to recognize that other individuals have a similar keenness just like mine to grasp a good deal more when considering this matter. Certainly there are many more fun periods in the future for many who looked over your site.

8. I wanted to create you that bit of word in order to thank you the moment again on your spectacular guidelines you have contributed in this case. It’s quite unbelievably generous with people like you to present unhampered what exactly most people would have supplied as an electronic book to help make some profit for their own end, most importantly considering the fact that you might well have tried it if you wanted. These principles in addition acted to be the fantastic way to comprehend someone else have similar fervor similar to my own to grasp whole lot more in regard to this problem. I’m sure there are a lot more pleasant opportunities up front for folks who browse through your site.

9. I would like to show appreciation to you just for bailing me out of this scenario. Just after scouting throughout the internet and seeing principles which were not helpful, I believed my entire life was well over. Being alive without the presence of approaches to the issues you have solved as a result of your good guideline is a critical case, and the kind that would have adversely damaged my career if I had not come across your website. That skills and kindness in controlling all the details was tremendous. I am not sure what I would have done if I had not come across such a stuff like this. I can at this time relish my future. Thanks a lot very much for your reliable and effective guide. I won’t hesitate to suggest the website to anyone who desires counselling about this matter.

10. I precisely had to thank you so much once again. I am not sure the things that I would’ve gone through in the absence of the type of tips documented by you concerning my subject. Certainly was an absolute troublesome scenario for me, but being able to see the skilled style you solved the issue took me to leap with delight. I’m just happier for the work and in addition sincerely hope you find out what a great job that you’re carrying out training people today using your blog post. More than likely you have never met all of us.

11. I precisely wanted to thank you very much once again. I’m not certain the things that I could possibly have worked on without the entire strategies revealed by you over such area. This was a frightful scenario for me personally, nevertheless seeing the well-written way you resolved the issue took me to jump over gladness. I will be happy for this assistance and have high hopes you find out what a powerful job your are getting into educating some other people thru your web site. I’m certain you’ve never met all of us.

12. I must point out my admiration for your generosity for those people who must have help with this important question. Your personal commitment to passing the message all around had become rather useful and has usually encouraged associates much like me to get to their dreams. Your new useful useful information signifies a lot a person like me and additionally to my peers. Warm regards; from everyone of us.