需求描述:
现有一个第三方so文件(比如:libXXX.so),有详细的接口文档,可以在android下直接调用,但现需要用C再封装一层,自己写C调用第三方的libXXX.so,再编译成自已的so文件,android中再调用自己的so文件。
问题:
1.C下如何调用libXXX.so?
2.Android.mk文件如何编写?请贴详细代码,多谢!AndroidJNIso封装
现有一个第三方so文件(比如:libXXX.so),有详细的接口文档,可以在android下直接调用,但现需要用C再封装一层,自己写C调用第三方的libXXX.so,再编译成自已的so文件,android中再调用自己的so文件。
问题:
1.C下如何调用libXXX.so?
2.Android.mk文件如何编写?请贴详细代码,多谢!AndroidJNIso封装
解决方案 »
- 经典代码合集+ANGLE游戏引擎源码和例子(需要学习的留邮箱)
- 求根据经纬度确定城市名称的google api
- 求指点
- 软件是否支持移到sd卡,是系统判断,还是apk自己申明?
- 使用WebView做软件界面,那么如何获取Web页面的值呢
- 推荐:Android快速开发软件--谷歌推Google App Inventor工具 力拼iPhone OS
- Button android:id 语法问题
- android如何实现上传图片以及从相册里选择多个图片上传
- java.net.InetAddress.getByName 程序直接退出 ??
- 关于页面跳转的问题
- 自己用CXF+spring+hibernate写了个简单的webservice,用android虚拟机连接老是报错
- android 关于VideoView播放HTTP协议的MP4视频时所产生的问题
2.LOCAL_SHARED_LIBRARIES 后加引用的动态库仅供参考....
#
include $(CLEAR_VARS)LOCAL_MODULE := XXX
LOCAL_SRC_FILES := XXX.cLOCAL_CFLAGS := -Iyour_lib_name/include
LOCAL_LDFLAGS += -Lyour_lib_name/libLOCAL_LDLIBS := -lyour_lib_nameinclude $(BUILD_SHARED_LIBRARY)
void *handle;
int (*call_method)(void);handle = dlopen(so_path, RTLD_LAZY);//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
void *handle;
int (*call_method)(void);handle = dlopen(so_path, RTLD_LAZY);//函数指针
call_method = dlsym(handle, "method_name(调用方法的函数名)");
多谢!我试试。
但是Android.mk文件会
# 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 := gpio ---》自己定义要的so名称
LOCAL_SRC_FILES := gpio.c ---》你的C文件名称LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
应该有提供lib的头文件吧,直接在android.mk里面将头文件和lib 加入进来
然后在你自己的代码中#include头文件,直接调用相应的接口就可以了。如果没有头文件就得使用dlopen dlsym
不过.so文件你可以直接copy到/libs/armeabi/下面
然后代码中使用绝对路径dlopen ("/data/data/lib/libxxx.so )
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=hello-world
LOCAL_SRC_FILES :=hello-world.c
LOCAL_SHARED_LIBRARIES := hello-jni
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)相对应的c代码:
#include "string.h"
#include "jni.h"
#include "dlfcn.h"
#include <fcntl.h>void * filehandle = NULL;
jint (*getResult)(JNIEnv *, jobject, jint, jint) =NULL;jint JNICALL Java_com_example_hellojni_HelloAct_getInt(JNIEnv * env,
jobject thiz) {
jint result = 0;
filehandle = dlopen("libhello-jni.so", RTLD_LAZY);
if (filehandle) {
getResult = (jint (*)(JNIEnv *, jobject, jint, jint)) dlsym(filehandle,
"Java_com_example_hellojni_HelloJni_add");
if (getResult)
result = getResult(env, thiz, 3, 10);
dlclose(filehandle);
filehandle = NULL;
}
return result + 1;
}