首先建立一般的Android项目
再新建一个类hellojni.java用来进行JNI调用:如下
package cn.mydreamy.zhao;
public class hellojni {
static {
System.loadLibrary("hello");
}
public native String print();
}
对Activity类进行编写,调用hellojni类的Native方法
package cn.mydreamy.zhao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class hello extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final hellojni h =new hellojni();
final TextView textview = (TextView) findViewById(R.id.hello);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
textview.setText(h.print());
}
});
}
}
此时自己编写了自动化配置编译出JNI所需要的头文件
脚本如下:存放路径为:~/ (home路径)
名称随便定义为:JH
内容为:
。。。。。。。。。。。。。。。。。。。。。。
#!/bin/sh
read -p "请输入工程src目录:" workhome
cd ${workhome}
path=cn/mydreamy/zhao
read -p "请输入java文件名:" file1
FILES=${path}/${file1}
javac ${FILES}.java
#mv ${FILES}.class ~/exchange
set classpath=`pwd`
javah cn.mydreamy.zhao.${file1}
file2=cn_mydreamy_zhao_${file1}.h
echo $file2
cd ..
sudo mkdir jni
sudo chown -R administrator.administrator jni
sudo mkdir libs
sudo chown -R administrator.administrator libs
cp /home/administrator/.exchange/android-ndk-r4/samples/hello-jni/jni/* jni
cp src/${file2} jni
mv src/${file2} ~/.exchange
gedit ~/.exchange/$file2
cd ~
。。。。。。。。。。。。。。。。。。。。。。。。。
其中用到了Android NDK 目录为:/home/administrator/.exchange/android-ndk-r4/ ,
具体位置根据实际位置来修改脚本。
eclipse 工作目录在 :/home/administrator/workspace
下面更新Android项目就会发现编译好的头文件已经加入工程,在这之前编译时会打开这个编译好的头文件内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class cn_mydreamy_zhao_hellojni */
#ifndef _Included_cn_mydreamy_zhao_hellojni
#define _Included_cn_mydreamy_zhao_hellojni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: cn_mydreamy_zhao_hellojni
* Method: print
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL
Java_cn_mydreamy_zhao_hellojni_print
(JNIEnv *, jobject)
;
#ifdef __cplusplus
}
#endif
#endif
。。。。。。。。。。。。。。。。
复制其中要实现的方法名:
JNICALL Java_cn_mydreamy_zhao_hellojni_print
(JNIEnv *, jobject)
此时打开工程目录中由自动编译拷贝进来的hell-jni.c文件,内容如下:
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string.h>
#include <jni.h>
/* This is a trivial JNI example where we use a native method
* to return a new VM String. See the corresponding Java source
* file located at:
*
* apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
*/
jstring
Java_cn_mydreamy_zhao_hellojni_print( JNIEnv* env,
jobject thiz )
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}
。。。。。。
把红色部分用头文件中定义的函数替换,这时java就和c有了联系了,这时我们需要编写c文件的方法实现了
此例子不需要实现什么只是输出Helloword 故直接替换函数名就可以了。
还需要编写Android.mk文件:LOCAL_MODULE := hello
hello为要产生的os文件名,下面配置Application.mk时,名字是相同的
到此为止 就差最后一步了,用NDK编译C文件生成so文件,这里我有编写了个脚本,进行自动话编译。脚本如下:
#!/bin/sh
read -p "请输入工程名:" workname
workhome=/home/administrator/workspace/${workname}
NDKROOT=/home/administrator/.exchange/android-ndk-r4
PROJECT=mydreamy
echo ${workhome}
sudo cp -r ${workhome} ${NDKROOT}/apps/${PROJECT}
cd $NDKROOT
sudo chown -R administrator.administrator apps/mydreamy
mv apps/mydreamy/${workname} apps/mydreamy/project
gedit ${NDKROOT}/apps/${PROJECT}/Application.mk
make APP=mydreamy
sudo mv apps/${PROJECT}/project/libs/armeabi ${workhome}/libs/armeabi
sudo rm -rf apps/${PROJECT}/project
echo "请运行Android程序!"
cd ~
。。。。。。。。。。。。。。。。。。。。。。。。。。
刷新eclipse中的工程,就会发现so文件已经添加进工程了,这时大功告成了,便可以启动Android运行查看效果了。
工程目录如图:
大家也可以自己编写一下试试,其实没必要这么,配置操作也就几步,由于本人比较懒所以才写成了脚本配置,只要也是练了练手。