使用 SWIG 自动生成 JNI C 代码

【注】

1、在 Makefile 文件中,命令必须以【tab】键开始。否则会出现 “ missing separator.” 错误提示。

2、编译正常,但出现 “Method 'NewStringUTF' could not be resolve” 错误,解决办法:http://stackoverflow.com/questions/15899813/eclipse-method-newstringutf-could-not-be-resolved

Go to the project's Properties -> C/C++ General -> Code Analysis. Click the "Use project settings" radio button (or "Configure Workspace Settings..." button). Disable (uncheck) the "Method cannot be resolved" checkbox. Click "Apply," "OK." Then for your project, refresh, clean, refresh, build.

【例】my-swig-generate.mk

 1 #
 2 # Android 构建系统的 SWIG 扩展
 3 #
 4 # @author 
 5 #
 6 
 7 # 检查变量 MY_SWIG_PACKAGE 是否已经定义
 8 ifndef MY_SWIG_PACKAGE
 9     $(error MY_SWIG_PACKAGE is not defined.) 
10 endif
11 
12 # 用斜线替换 Java 目录中的圆点
13 MY_SWIG_OUTDIR := $(NDK_PROJECT_PATH)/src/$(subst .,/,$(MY_SWIG_PACKAGE))
14 # SWIG 的默认类型是 C
15 ifndef MY_SWIG_TYPE
16   MY_SWIG_TYPE := c
17 endif
18 
19 # 设置 SWIG 的模式
20 ifeq (    $(MY_SWIG_TYPE),cxx)
21   MY_SWIG_MODE := -c++
22 else
23   MY_SWIG_MODE := 
24 endif
25 
26 # 追加 SWIG 封装源文件
27 LOCAL_SRC_FILES +=    $(foreach MY_SWIG_INTERFACE,\
28     $(MY_SWIG_INTERFACES),\
29     $(basename $(MY_SWIG_INTERFACE))_wrap.$(MY_SWIG_TYPE))
30   
31 # 添加 .cxx 作为 c++ 扩展名
32 LOCAL_CPP_EXTENSION += .cxx
33 
34 # 生成 SWIG 封闭代码(indention should be tabs for this block)
35 %_wrap.$(MY_SWIG_TYPE) : %.i
36     $(call host-mkdir,    $(MY_SWIG_OUTDIR))
37     swig -java \
38     $(MY_SWIG_MODE) \
39     -package $(MY_SWIG_PACKAGE) \
40     -outdir $(MY_SWIG_OUTDIR) \
41       $<

 

在 Android.mk 中增加

1 MY_SWIG_PACKAGE := com.apress.swig
2 MY_SWIG_INTERFACES := Unix.i
3 MY_SWIG_TYPE := c
4 
5 include $(LOCAL_PATH)/my-swig-generate.mk

 

posted @ 2015-07-03 18:26  壬子木  阅读(403)  评论(0)    收藏  举报