The native method shows the code and Java applications to connect on a knitting blog. As described above, the conversion between a JNI wrapper code and processing data types are tedious and time-consuming development tasks. Here SWIG (Simplified Wrapper and Interface Generator) development tools is to simplify the above-described process.
SWIG Android is not Java or special tools. It is a can produce many other programming languages code, tools widely used here only introduce Android-related concepts:
installation
Mac system developers can refer to my another blog http://blog.csdn.net/liangjiangli/article/details/72832154
Windows system developers also need to go to the official website to download the installation package: http://www.swig.org/ , after decompression, add the directory where the exe file to the environment variable.
By way of example using the procedure SWIG
Android platform is a multi-user platforms resume on the Linux operating system, Linux system, a user ID assigned to each user, due to the platform-independent programming language, Java can not access the user ID, so use POSIX OS API function of getuid query this user ID.
1, interface file
New Unix.i interface file in the file jni
Document reads as follows:
/* 模块名是 Unix. */
%module Unix
%{
/* 包含POSIX操作系统API. */
#include<unistd.h>
%}
/* 告诉 SWIG uid_t. */
typedef unsigned int uid_t;
/* 让SWIG 包装getuid函数. */
extern uid_t getuid(void);
2, call the command mode by SWIG
First, build a java package used to save files
Open Terminalcd to the main directory, and then execute the command: swig -java -package com.study.eric.swig -outdir java / com / study / eric / swig jni / Unix.i
After executing the command you will find produced three documents, a C file, two Java files
In order to facilitate the implementation of the command, the new External Tools
Program: /Users/daredos/Swig/bin/swig
Parameters: -java -package com.study.eric.swig -outdir $ModuleFileDir$/src/main/java/com/study/eric/swig $ModuleFileDir$/src/main/jni/Unix.i
Working directory: /Users/daredos/Swig/bin
3, SWIG will be integrated into the Android build file
New swig-generate.mk
It reads as follows:
#
# @author liangjiangli
#
# 检查变量 SWIG_PACKAGE是否已经定义
ifndef SWIG_PACKAGE
$(error SWIG_PACKAGE is not defined.)
endif
# 用斜线替换Java目录的圆点
SWIG_OUTDIR:=$(NDK_PROJECT_PATH)/src/$(subst .,/,$(SWIG_PACKAGE))
# SWIG的默认类型是C
ifndef SWIG_TYPE
SWIG_TYPE := C
endif
# 设置SWIG的模式
#ifeq ($(SWIG_TYPE), cxx)
# SWIG_TYPE_MODE := - c++
#else
# SWIG_TYPE_MODE :=
#endif
# 追加SWIG封装源文件
LOCAL_SRC_FILES += $(foreach SWIG_INTERFACE, $(SWIG_INTERFACES),\
$(basename $(SWIG_INTERFACE))_wrap.$(SWIG_TYPE))
# 添加.cxx作为C++扩展名
#LOCAL_CPP_EXTENSION += .cxx
# 生成SWIG封闭代码(indention should be tabs for this block)
$_wap.$(SWIG_TYPE) : %.i \
$(call host-mkdir, $(SWIG_OUTDIR)) \
swig -java \
$(SWIG_MODE) \
-package $(SWIG_PACKAGE) \
-outdir $(SWIG_OUTDIR) \
$<
SWIG variables defined in the Android.mk file
SWIG_PACKAGE := com.study.eric.swig
SWIG_INTERFACES := Unix.i
SWIG_TYPE := c
include $(LOCAL_PATH)/swig-generate.mk
Then perform ndk-build command to re-package production .so
Finally, the code calls the results are as follows:
In short, swig installation and use are very simple things. Use swig can greatly reduce the extent of suffering programmer to help you accomplish what you want faster function.