以libpng为例。
如果使用动态库:
1、新建android工程,创建jni目录,其它步骤不多说了,参考官方的ndk入门就可以了。
将libpng所需源文件放在jni目录下,
并编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CFLAGS :=
LOCAL_MODULE := png
LOCAL_SRC_FILES :=\
png.c \
pngerror.c \
pngget.c \
pngmem.c \
pngpread.c \
pngread.c \
pngrio.c \
pngrtran.c \
pngrutil.c \
pngset.c \
pngtrans.c \
pngwio.c \
pngwrite.c \
pngwtran.c \
pngwutil.c
LOCAL_LDLIBS := -lz
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_STATIC_LIBRARY)
编写Application.mk
APP_OPTIM := release
APP_MODULES := libpng
然后编译,执行成功后会生成
libpng.so文件。
好了,动态链接库已经生成。将此文件复制至自己的工程的jni目录下。
Android.mk修改如下:
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := png
LOCAL_SRC_FILES := libpng.so
include ${PREBUILT_SHARED_LIBRARY}
include $(CLEAR_VARS)
LOCAL_MODULE := libgl2jni
LOCAL_CFLAGS := -Werror
LOCAL_SHARED_LIBRARIES := png
#LOCAL_STATIC_LIBRARIES := libpng
#LOCAL_C_INCLUDES += $(LOCAL_PATH)/png
LOCAL_SRC_FILES := gl_code.cpp
LOCAL_LDLIBS := -llog -lGLESv1_CM -lz
include $(BUILD_SHARED_LIBRARY)
在java代码中也别忘记加载动态库:
static {
System.loadLibrary(“stlport_shared”); // 加载stl的动态库
System.loadLibrary(“png”); // 加载gl2库
System.loadLibrary(“gl2jni”); // 加载gl2库
}
因为gl2jni使用了png库,所以要先加载png库,再加载gl2jni库。
为了方便,在google code已经建立好了工程,可位可以直接下载编译:
http://code.google.com/p/android-ndk-libpng/
——————————————————————————————————–
如果使用静态库:
将以上源代码放在项目的jni目录下
修改Android.mk为:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libgl2jni
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := gl_code.cpp
LOCAL_STATIC_LIBRARIES := png
#LOCAL_C_INCLUDES += $(LOCAL_PATH)/png
LOCAL_LDLIBS := -llog -lGLESv1_CM -lz
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/png/Android.mk
0 条评论。