android开发调用c++共享库so文件

1、编写libaab.cpp

#include <stdio.h>
#include <stdlib.h>


#ifdef __cplusplus
extern "C" {
#endif


int go() {
return 555;
}

#ifdef __cplusplus
}
#endif

运行g++命令编译得到libaab.so

arm-linux-androideabi-g++.exe -I/usr/local/linux-androideabi/platforms/android-19/arch-arm/usr/include/linux -I/usr/local/linux-androideabi/platforms/android-19/arch-arm/usr/include -c -fPIC -march=armv7-a aab.cpp -o ./aab.o
arm-linux-androideabi-ld.exe -shared ./aab.o -o ./libaab.so

或者gcc

arm-linux-androideabi-gcc.exe -I/usr/local/linux-androideabi/platforms/android-19/arch-arm/usr/include/linux -I/usr/local/linux-androideabi/platforms/android-19/arch-arm/usr/include -shared -L./ -fPIC aab.cpp -o ./libaab.so

 

2、新建JNITest项目,新建类JNITest/src/com/example/jo/JOB.java,专门处理jni

package com.example.jo;

public class JOB {
public static native void abc();
public static native int ShowStr(int a, int b);
static {
// load library: libaaa.so
try {
System.loadLibrary("aab");
System.loadLibrary("aaa");
} catch (UnsatisfiedLinkError ule) {
System.err.println("WARNING: Could not load library!");
}
}
}

3、打开cmd

set path=d:\soft\java\jdk1.8.0_20\bin
cd D:\Soft\adt-bundle-windows-x86_64-20140321\workspace\JNITest\bin\classes
javah -jni com.example.jo.JOB

在JNITest\bin\classes\目录下会生成头文件com_example_jo_JOB.h,复制到

/JNITest/jni/com_example_jo_JOB.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_jo_JOB */

#ifndef _Included_com_example_jo_JOB
#define _Included_com_example_jo_JOB
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_jo_JOB
* Method: abc
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_example_jo_JOB_abc
(JNIEnv *, jclass);

/*
* Class: com_example_jo_JOB
* Method: ShowStr
* Signature: (II)I
*/
JNIEXPORT jint JNICALL Java_com_example_jo_JOB_ShowStr
(JNIEnv *, jclass, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

4、编写JNITest\jni\aaa.cpp

#include "com_example_jo_JOB.h"
#include <stdio.h>
#include <stdlib.h>


#ifdef __cplusplus
extern "C" {
#endif

extern int go();


void Java_com_example_jo_JOB_abc( JNIEnv* env, jclass thiz) {
printf("hello,world!");
}

JNIEXPORT jint JNICALL Java_com_example_jo_JOB_ShowStr
(JNIEnv * env, jclass thiz, jint a, jint b) {
return go();
//return a + b;
}

jint JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) {
return result;
}
return JNI_VERSION_1_6;
}

#ifdef __cplusplus
}
#endif

5、复制刚刚生成的libaab.so到/JNITest/jni/libaab.so

6、编写/JNITest/jni/Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE :=aab
LOCAL_SRC_FILES := libaab.so

include $(PREBUILT_SHARED_LIBRARY)  

 

include $(CLEAR_VARS)

LOCAL_MODULE := libaaa
LOCAL_SRC_FILES := aaa.cpp
LOCAL_SHARED_LIBRARIES := aab

include $(BUILD_SHARED_LIBRARY)

7、在cmd运行

set path=D:\Soft\adt-bundle-windows-x86_64-20140321\android-ndk-r9d

cd /JNITest/jni

ndk-build

运行结果如下:

D:\Soft\adt-bundle-windows-x86_64-20140321\workspace\JNITest\jni>ndk-build
D:/Soft/adt-bundle-windows-x86_64-20140321/workspace/JNITest//jni/Android.mk:22:
D:/Soft/adt-bundle-windows-x86_64-20140321/workspace/JNITest//jni
[armeabi] Compile++ thumb: aaa <= aaa.cpp
[armeabi] SharedLibrary : libaaa.so
[armeabi] Install : libaaa.so => libs/armeabi/libaaa.so
[armeabi] Install : libaab.so => libs/armeabi/libaab.so

会将libaaa.so和libaab.so都复制到libs/armeabi/目录下

这样我实现了两个so,可以在libaaa.so里调用libaab.so里的方法Go,需要在aaa.cpp里声明为extern int Go();

就可以实现从一个c++的so里调用另一个so里的方法。这样就掌握了基本的c++共享库的调用方法,以后只需要将c++的开源库编译为so文件,再用这种方法就可以调用了

 

你可能感兴趣的:(Android开发)