您的位置:首页 > 移动开发 > Android开发

【Android-jni】JNI学习第一天

2014-03-08 11:32 351 查看
1.ndk环境变量
E:\AndroidToolscunzade\android-ndk-r9;
测试是否成功
C:\Users\Administrator>ndk-build
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
E:\AndroidToolscunzade\android-ndk-r9\build/core/build-local.mk:130: *** Android
NDK: Aborting    .  Stop.
2.新建Android工程TestJNI
3.在MainActivity中声明两个native方法
[code]public native String helloFromC();public native String hello_From_C();
[/code]
4.新建jni文件夹
5.生成 com_example_testjni_MainActivity.h头文件
E:\AndroidToolscunzade\workspace\TestJNI\bin\classes>javah com.example.testjni.MainActivity
/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_example_testjni_MainActivity */#ifndef _Included_com_example_testjni_MainActivity#define _Included_com_example_testjni_MainActivity#ifdef __cplusplusextern "C" {#endif/** Class:     com_example_testjni_MainActivity* Method:    helloFromC* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_helloFromC(JNIEnv *, jobject);/** Class:     com_example_testjni_MainActivity* Method:    hello_From_C* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_hello_1From_1C(JNIEnv *, jobject);#ifdef __cplusplus}#endif#endif
[/code]
6.编写hello.c实现头文件的方法
#include <stdio.h>#include <jni.h>#include "com_example_testjni_MainActivity.h"JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_helloFromC(JNIEnv *env, jobject obj){return (*env)->NewStringUTF(env,"helloFromC");}/** Class:     com_example_testjni_MainActivity* Method:    hello_From_C* Signature: ()Ljava/lang/String;*/JNIEXPORT jstring JNICALL Java_com_example_testjni_MainActivity_hello_1From_1C(JNIEnv *env, jobject obj){return (*env)->NewStringUTF(env,"helloFromC__");}
[/code]
7.编辑Android.mk文件
[code]LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := helloLOCAL_SRC_FILES := hello.cinclude $(BUILD_SHARED_LIBRARY)
[/code]
8.生成so库
E:\AndroidToolscunzade\workspace\TestJNI>ndk-buildAndroid NDK: WARNING: APP_PLATFORM android-18 is larger than android:minSdkVersi on 8 in ./AndroidManifest.xml"Compile thumb : hello <= hello.cSharedLibrary  : libhello.soInstall        : libhello.so => libs/armeabi/libhello.so
9.调用so库
[code]package com.example.testjni;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {public native String helloFromC();public native String hello_From_C();static {System.loadLibrary("hello");}public void click(View view) {Toast.makeText(this, helloFromC(), 1).show();}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onMenuItemSelected(int featureId, MenuItem item) {// TODO Auto-generated method stubif (item.getItemId()==R.id.action_settings) {Toast.makeText(this, hello_From_C(), 1).show();}return super.onMenuItemSelected(featureId, item);}}
[/code]
10.运行
a.点击按钮b.点击setting
11.重新生成时clean
E:\AndroidToolscunzade\workspace\TestJNI>ndk-build cleanAndroid NDK: WARNING: APP_PLATFORM android-18 is larger than android:minSdkVersion 8 in ./AndroidManifest.xmlClean: hello [armeabi]Clean: stdc++ [armeabi]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: