以下开发是在ubuntu12.04进行。
准备开发环境:
1.这里我直接用的android官方提供的IDE。
http://developer.android.com/sdk/index.html
2.android sdk.
环境变量设置:
export ANDROID_HOME=$HOME/download/adt-bundle-linux-x86-20130729/sdk
export ANDROID_NDK_HOME=$HOME/download/android-ndk-r9
export JAVA_HOME=$HOME/jdk1.7.0_25
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_NDK_HOME:$PATH
export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
3.ndk,http://developer.android.com/tools/sdk/ndk/index.html
如图:
创建一个android项目。
layout-->main.xml内容很简单,就一个文本框,展示结果使用:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/txtView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="124dp"/> </RelativeLayout>
现在建立一个类,这个类主要是调用底层c函数的类:
package org.chenjun.jni;
public class JniUtil {
public native static int getSum(int a,int b);
static{
System.loadLibrary("jniUtil");//libjniUtil.so
}
}
只有一个方法,getSum,传递2个数值进行求和操作。通过javah编译出头文件出来。文件内容:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class org_chenjun_jni_JniUtil */ #ifndef _Included_org_chenjun_jni_JniUtil #define _Included_org_chenjun_jni_JniUtil #ifdef __cplusplus extern "C" { #endif /* * Class: org_chenjun_jni_JniUtil * Method: getSum * Signature: (II)I */ JNIEXPORT jint JNICALL Java_org_chenjun_jni_JniUtil_getSum (JNIEnv *, jclass, jint, jint); #ifdef __cplusplus } #endif #endif
在android工程下建立jni目录。将该头文件放到jni目录中。jni目录中创建c文件jniUtil.c:
#include <string.h> #include <jni.h> #include "org_chenjun_jni_JniUtil.h" JNIEXPORT jint JNICALL Java_org_chenjun_jni_JniUtil_getSum (JNIEnv *env, jclass clazz, jint a, jint b){ return a + b; }
jni目录中创建android.mk文件,文件的含义在ndk的doc中有介绍:
# 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. # LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := jniUtil LOCAL_SRC_FILES := jniUtil.c include $(BUILD_SHARED_LIBRARY)
目录结构如下图:
使用终端进入到android项目的根目录下面,执行ndk-build命令就会生成我们要的动态链接库:
在MainActivity中我们调用动态链接库给我们的方法进行计算并展示在文本框中:
package org.chenjun.android.jni;
import org.chenjun.jni.JniUtil;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtView = (TextView) findViewById(R.id.txtView);
int c = JniUtil.getSum(2, 6);
txtView.setText("2+6=" + c);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
运行后的结果如图:
如果紧紧是调用别人提供的动态链接库,之需要把so文件放到libs/armeabi下面,android在调用的时候一定要对应包名和方法名即可。
附件附带整个工程