还要以NDK提供的two-libs为例子,走一遍多个静态库(.a文件)生成动态库(.so文件)的流程。

1、建立android工程,编写java对应JNI层的本地接口:

package com.example.twolibs;import android.app.Activity;import android.widget.TextView;import android.os.Bundle;public class TwoLibs extends Activity{    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        TextView  tv = new TextView(this);        int       x  = 1000;        int       y  = 42;        // here, we dynamically load the library at runtime        // before calling the native method.        //        int  z = add(x, y);        tv.setText( "The sum of " + x + " and " + y + " is " + z );        setContentView(tv);            }    public native int add(int  x, int  y);        static    {        System.loadLibrary("twolib-second");    }}


JNI文件夹下的准备预备文件如下,其中.o和.a文件都是用NDK编译生成,具体操作见下:

Android.mk  first.c  first.o   third.a  third.hfirst.a     first.h  second.c  third.c  third.o

2、编写jni层中间层代码second.c,在其中调用first.c中的first(int x,int y)函数及third.c中的test()函数,具体代码如下:

first.h头文件如下:

#ifndef FIRST_H#define FIRST_Hextern int first(int  x, int  y);#endif /* FIRST_H */

first.c中c代码:

#include "first.h"int  first(int  x, int  y){    return x + y;}


现在就用NDK的提供的C编译器来编译生成firs.a这个静态库,用于后续生成libtwolib-second.so动态库的源文件。步骤跟linux下编译生成静态库方法完全一样.

第一步:找到NDK下自带的gcc和ar,/android-ndk-r9e/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/

第二步:用NDK的gcc编译器来生成.a文件,如:

thinker@fans:~/android/android-ndk-r9/samples/two-libs/jni$ ../../../toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc -o first.o -c first.c 

第三步:用NDK的ar工具来生成.a文件,如:

thinker@fans:~/android/android-ndk-r9/samples/two-libs/jni$ ../../../toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-ar rcs first.a first.o

到此OK,生成了first.a静态库文件。类似步骤生成third.a库。

third.h如:

#ifndef THIRD_H#define THIRD_Hextern int third();#endif /* THIRD_H */

third.c中c代码:

#include "third.h"int third(){return 10;}

jni层中间层代码second.c如下:

#include "first.h"#include "third.h"#include #include #include  //修改日志tag中的值  #define LOG_TAG "log_from_second.c"//日志显示的等级  #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)  #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)  jintJava_com_example_twolibs_TwoLibs_add( JNIEnv*  env,                                      jobject  this,                                      jint     x,                                      jint     y ){    int i = third();    LOGI("the test result = %d",i);    return first(x, y);}

3、编写Android.mk文件:如下两种方式都可以,用于NDK编译工具生成的两个.a文件来生成最终的libtwolib-second.so 动态库。

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-secondLOCAL_SRC_FILES := second.cLOCAL_LDFLAGS := first.a third.aLOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -lloginclude $(BUILD_SHARED_LIBRARY)

方式二:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-firstLOCAL_SRC_FILES := first.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-thirdLOCAL_SRC_FILES := third.ainclude $(PREBUILT_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-secondLOCAL_SRC_FILES := second.cLOCAL_STATIC_LIBRARIES := libtwolib-first libtwolib-thirdLOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -lloginclude $(BUILD_SHARED_LIBRARY)

4、生成动态库

thinker@fans:~/android/android-ndk-r9/samples/two-libs/jni$ /home/thinker/android/android-ndk-r8e/ndk-build/home/thinker/android/android-ndk-r8e/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 3 in /home/thinker/android/android-ndk-r9/samples/two-libs/AndroidManifest.xml    Compile thumb  : twolib-second <= second.cSharedLibrary  : libtwolib-second.soInstall        : libtwolib-second.so => libs/armeabi/libtwolib-second.so

运行查看打印信息,一切OK。


参考网址:

http://blog.csdn.net/wjr2012/article/details/6887559


附:

可以直接编写Android.mk文件来生成两个.a文件,如下:

LOCAL_PATH:= $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-firstLOCAL_SRC_FILES := first.cinclude $(BUILD_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-thirdLOCAL_SRC_FILES := third.cinclude $(BUILD_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE    := libtwolib-secondLOCAL_SRC_FILES := second.cLOCAL_STATIC_LIBRARIES := libtwolib-first libtwolib-thirdLOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -lloginclude $(BUILD_SHARED_LIBRARY)

编译:

thinker@fans:~/android/android-ndk-r9/samples/two-libs/jni$ ../../../../android-ndk-r8e/ndk-build/home/thinker/android/android-ndk-r8e/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 3 in /home/thinker/android/android-ndk-r9/samples/two-libs/AndroidManifest.xml    Compile thumb  : twolib-second <= second.cCompile thumb  : twolib-first <= first.cStaticLibrary  : libtwolib-first.aCompile thumb  : twolib-third <= third.cStaticLibrary  : libtwolib-third.aSharedLibrary  : libtwolib-second.soInstall        : libtwolib-second.so => libs/armeabi/libtwolib-second.so

生成的.a文件在工程/obj/local/armeabi目录下:

thinker@fans:~/android/android-ndk-r9/samples/two-libs/obj/local/armeabi$ lslibtwolib-first.a  libtwolib-second.so  libtwolib-third.a  objs

更多相关文章

  1. mac android(android studio)环境搭建配置详解
  2. android 录音 mediaRecorder
  3. android 资源res下目录使用
  4. Android的数据存储方式
  5. Cocos2d-x 项目从VS移植到Android中的配置
  6. GreenDao 在 Android(安卓)Studio 中的配置使用
  7. Android(安卓)常用代码集合
  8. Android属性之excludeFromRecents
  9. NPM 和webpack 的基础使用

随机推荐

  1. Ubuntu12.04安装JDK6
  2. Android中查看网卡设备信息
  3. 使用AXMLPrinter2,smali,baksmali来实现A
  4. 键盘按钮效果
  5. Android(安卓)JNI开发基础
  6. Android中BaseAdapter原理
  7. android vlc 编译流程
  8. android 多点触摸实现图片缩放
  9. Android7.0中文文档(API)-- AlphabetIndexe
  10. android 单元测试 radio group