还要以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. Android 常用代码集合
  2. Android 文件操作,删除,拷贝文件等
  3. Android系统源码编译全过程——下载Android源文件并编译
  4. [Android] 文件夹下文件的个数限制
  5. Android之Telephony各文件解释
  6. Android录音时获取分贝值的方法代码实例
  7. Android 自定义View及其在布局文件中的使用示例(二)

随机推荐

  1. 二.系统编译--Ubuntu14.04上搭建android
  2. Android中的Looper类&Handler &Message
  3. Android SQLite最简单demo实现(增删查改)
  4. [Android]开发摇一摇分歧表决器过程
  5. 问题虽小,但是遇到会蛋疼!android蛋疼问题
  6. Android USB Host的使用详解
  7. Android 高级面试
  8. 【eoeAndroid社区索引】Android二维码知
  9. Android开发便签7:如何让通讯录匹配N位号
  10. android设置控件样式(边框颜色,圆角)和图片