当前位置: 代码迷 >> Android >> android so 库怎么调用其他库里的函数或变量
  详细解决方案

android so 库怎么调用其他库里的函数或变量

热度:58   发布时间:2016-05-01 21:51:16.0
android so 库如何调用其他库里的函数或变量?
比如 有个库A.so 想使用另外一个库B.so里的函数,调用B.so中的get_value()函数获取该库中的变量值。
B.so里的这个变量值是上层通过JNI控制的,是变化的
可是我在A.so里把从B.so里获取的值打出来 一直是一个不变的值。

请帮忙看看。谢谢



------解决方案--------------------
以下是两个.c的调用。
TwoLibs.java

public class TwoLibs extends Activity{

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

TextView tv = new TextView(this);
int x = 1000;
int y = 42;

System.loadLibrary("twolib-second");

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);
}

jni文件夹下面的文件:
Android.mk


LOCAL_PATH:= $(call my-dir)

# first lib, which will be built statically
#
include $(CLEAR_VARS)

LOCAL_MODULE := libtwolib-first
LOCAL_SRC_FILES := first.c

include $(BUILD_STATIC_LIBRARY)

# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)

LOCAL_MODULE := libtwolib-second
LOCAL_SRC_FILES := second.c

LOCAL_STATIC_LIBRARIES := libtwolib-first

include $(BUILD_SHARED_LIBRARY)


first.c
#include "first.h"

int first(int x, int y)
{
return x + y;
}

first.h

#include "first.h"
#include <jni.h>

jint
Java_com_example_twolibs_TwoLibs_add( JNIEnv* env,
jobject this,
jint x,
jint y )
{
return first(x, y);
}

second.c

#include "first.h"
#include <jni.h>

jint
Java_com_example_twolibs_TwoLibs_add( JNIEnv* env,
jobject this,
jint x,
jint y )
{
return first(x, y);
}


最后你自己去把用NDK编译下工程。然后在执行就OK了。其实这些都可以再ndk\android-ndk-r7b\samples里面找到。
  相关解决方案