Automatically generate JNI code using Swig

Source: Internet
Author: User

This record applies to the appropriate modifications under Androidstudio,eclipse, but General Eclipse has a way of working under the UI.

Swig generating a JNI proxy class requires a. Class I file as an interface description:

Example:

%module Unix%{#include<unistd.h>%int  uid_t; extern uid_t getuid (void);

The description file defines the

1. The name of this module, UNIX,

2. Insert the pre-processing instruction #include<unistd.h>

3. To prevent Swig from seeing uid_t as the object's type declaration

4. Function prototypes

To be able to connect to the library using swig generated proxies and wrapper Jni/java code at Ndk-build, customize a makefile file as follows (put in the JNI directory):

1 ifndef swig_package2 $ (Error swig_package is not define.)3 endif4 5Swig_outdir: = $ (Ndk_project_path)/java/$ (subst.,/, $ (swig_package))6 ifndef Swig_type7Swig_type: =C8 endif9 Ten ifeq ($ (swig_type), cxx) OneSwig_mode: =-C + + A Else -Swig_mode: = - endif the  -Local_src_files + = $ (foreach swig_interface,$ (swig_interfaces), $ (basename$ (swig_interface)) _wrap.$ (Swig_type)) -  -Local_cpp_extension + =. Cxx +  -%_wrap.$ (swig_type):%. I +$ (Call host-mkdir, $ (swig_outdir)) ASwig-java $ (Swig_mode)-package $ (swig_package)-outdir $ (swig_outdir) $<

In fact, this MK is implemented by the definition in the compilation process to implement this command:

Swig-java-package Com.examples.swig-outdir Src/com/examples/swig jni/unix.i

To be able to implement the values of these parameters in the compilation, you need to define them in android.mk and call the above MK file before compiling it into a shared library/other library:

1Local_path: = $ (Call my-dir)2 3 include $ (clear_vars)4 5Local_module: =com_examples_jnidemo_mainactivity6Local_src_files: =com_examples_jnidemo_mainactivity.c7Local_ldlibs: =-Llog8 9Swig_package: =Com.examples.swigTenSwig_interfaces: =unix.i OneSwig_type: =C A  -Include $ (local_path)/swig_generate.mk -  the  -Include $ (build_shared_library)

Thus, the result of the build compilation is:

This only requires transferring the files in the Libs to Jnilibs, and then merging Java into the Java directory of the project and Jnilibs siblings:

At this point we just need to call it just fine:

Operation Result:

Automatically generate JNI code using Swig

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.