以下开发是在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

如图:


ubuntu android 调用 动态链接库_第1张图片

ubuntu android 调用 动态链接库_第2张图片
创建一个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 __cplusplusextern "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    := jniUtilLOCAL_SRC_FILES := jniUtil.cinclude $(BUILD_SHARED_LIBRARY)

目录结构如下图:


ubuntu android 调用 动态链接库_第3张图片

使用终端进入到android项目的根目录下面,执行ndk-build命令就会生成我们要的动态链接库:


ubuntu android 调用 动态链接库_第4张图片
在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;@Overrideprotected 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);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}

运行后的结果如图:


ubuntu android 调用 动态链接库_第5张图片

如果紧紧是调用别人提供的动态链接库,之需要把so文件放到libs/armeabi下面,android在调用的时候一定要对应包名和方法名即可。

附件附带整个工程

更多相关文章

  1. 转:Android文件操作总结
  2. 【Android的从零单排开发日记】之入门篇(十)——Android应用配置文
  3. 关于android单元测试结合SAX解析xml文件初步分析
  4. Android使用AsyncTask下载显示图片
  5. android app 缓存 ---- android 文件缓存使用流程解析
  6. 如何在Android上显示wmf的文件
  7. 阿里Android开发规范:资源文件命名与使用规范
  8. Facebook推出强大Android图片库Fresco 自动释放内存 图片缓存 内
  9. Android从网上获取图片的简单示例

随机推荐

  1. Python学习笔记18:Python多线程编程
  2. 运维利器:钉钉机器人脚本告警(Linux Python
  3. 套接字。接受错误24:对许多打开的文件
  4. python打印列表的下标和值的例子:
  5. windows下,python的各类包的安装(cython, n
  6. 利用Python进行数据分析-- 学习心得(汇总)
  7. os.path.dirname(__file__)使用、Python
  8. 由ajax [duplicate]生成时,formset的提交
  9. Python学习日志_2017/09/08
  10. python 基础第四篇