Two ways to compile JNI libraries under "Go" Android (with examples)--good

Source: Internet
Author: User

Original URL: http://blog.sina.com.cn/s/blog_3e3fcadd01011384.html

Summarized as follows: Two methods are:
1) using the make system in Android source code
2) using the NDK (starting from NDK R5)

---------------------------------
Source Code requirements: If the top-level directory for Android engineering is my_android_project_name:
1) The sample contents of the My_android_project_name/jni/android.mk file are as follows (in the case of generating libjni_mouse.so C code):
Local_path:= $ (call My-dir)
Include $ (clear_vars)
Local_prelink_module: = False
Local_module: = Libjni_mouse
Local_src_files: = jni_mouse.c
Local_c_includes: = $ (Jni_h_include)
Local_ldlibs + =-l$ (sysroot)/usr/lib-llog
Local_shared_libraries: = Libcutils
Include $ (build_shared_library)

2) Part of the source file (for reference only):
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>

#include <jni.h>//located in $JDK/include
#include <cutils/log.h>//For Android make system: $Android _src/system/core/include/cutils/log.h
#include <android/log.h>//for NDK compiler

#define LOG_DBG (Tag,fmt,args ...) __android_log_print (android_log_debug,tag,fmt,# #args)
#define LOG_ERR (Tag,fmt,args ...) __android_log_print (android_log_error,tag,fmt,# #args)
#ifdef ___debug
#define DBG (FMT, args ...) __android_log_print (Android_log_debug, TAG, FMT, # #args)
#else
#define DBG (FMT, args ...)
#endif//___debug
#define OUT (FMT, args ...) __android_log_print (Android_log_error, TAG, FMT, # #args)

#define JNIREG_CLASS "Com/my_organization/my_pkg/jnimouse"

Static Jninativemethod jnimousemethods[] = {
{"Nativeopenmouse", "() I", (void*) Openmouse},
{"Nativeclosemouse", "() V", (void*) Closemouse},
{"Nativemovecursor", "(II) V", (void*) Movecursor},
...
};

static int Registernativemethods (jnienv* env, const char* ClassName, jninativemethod* methods,
int nummethods)
{
Jclass Clazz;

C + + way:clazz= Env->findclass (className);
Clazz = (*env)->findclass (env, className);
if (clazz = = NULL) {
Log_err (TAG, "Native registration error:unable to find class\n");
return jni_false;
}
C + + way:if (env->registernatives (Clazz, methods, Nummethods) < 0)
if ((*env)->registernatives (env, Clazz, methods, Nummethods) < 0) {
Log_err (TAG, "registernatives failed\n");
return jni_false;
}

return jni_true;
}

static int registernatives (jnienv* env)
{
if (!registernativemethods (env, Jnireg_class, Jnimousemethods,
sizeof (Jnimousemethods)/sizeof (Jnimousemethods[0]))
return jni_false;
return jni_true;
}

Jint jni_onload (javavm* vm, void* reserved)
{
jnienv* env = NULL;
Jint result =-1;

This was for C + +: if (vm->getenv ((void**) &env, jni_version_1_4)! = JNI_OK)
if (*VM)->getenv (VM, (void**) &env, jni_version_1_4)! = JNI_OK) {
Log_err (TAG, "error:getenv failed\n");
Goto bail;
}
ASSERT (env! = NULL);

if (!registernatives (env)) {
Log_err (TAG, "Error:jni_mouse native registration failed\n");
Goto bail;
}

result = Jni_version_1_4;

Bail:
return result;
}


---------------------------------
The compilation process is as follows:
1) using the make system in Android source code:
$ cd $ANDROID _SRC # (e.g./u_data/android/sourcecode/and22)
$  . build/envsetup.sh
# This will generate a lot of shell tools, actually shell functions.

$ Lunch My_lunch_number # If your project needs to use platform related content (shared library/java library, etc.)

# link (or copy) the top-level directory of the ANDROID project to $ANDROID _src/packages/apps, such as
$ ln-fs/abs/path/to/my_android_project_name $ANDROID _src/packages/apps

$ mmm Packages/apps/my_android_project_name
# If there are no errors, the following files will be generated (depending on the product name you use):
# $ANDROID _src/out/target/product/$PRODUCT _name/system/lib/libjni_mouse.so

# finally copy the generated library files to the Android project's libs/armeabi/directory:
$ mkdir-p $ANDROID _src/packages/apps/my_android_project_name/libs/armeabi
$ cp $ANDROID _src/out/target/product/$PRODUCT _name/system/lib/libjni_mouse.so \
$ANDROID _src/packages/apps/my_android_project_name/libs/armeabi

# This way, when the. apk file is compiled/generated normally in eclipse, the resulting JNI library is automatically included in the. apk file, which can be verified using the Unzip command:
# CP My.apk/tmp/tmp/my.zip; cd/tmp/tmp; Unzip My.zip; Ls-l Lib/armeabi/lib*.*

2) using the NDK (from NDK R5)
Starting with NDK R5, the NDK formally compiles JNI code as a standalone tool, but it often lacks some header files, such as <linux/uinput.h>.
0) Prepare the environment variables such as:
$ Export ndk=/u_data/android/sourcecode/android-ndk-r6b
$ export cc= $NDK/TOOLCHAINS/ARM-LINUX-ANDROIDEABI-4.4.3/PREBUILT/LINUX-X86/BIN/ARM-LINUX-ANDROIDEABI-GCC
$ export strip= $NDK/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-strip
$ export sysroot= $NDK/platforms/android-8/arch-arm # to see the version of the target Android you are using:)

1) for missing header files, you can borrow files with the same name under x86 on your PC, such as:
CD $NDK/platforms/android-8/arch-arm/usr/include/linux
Ln-fs. /.. /.. /.. /arch-x86/usr/include/linux/uinput.h.

2) Compile the executable file (if there is a main function), such as:
$ $CC--sysroot= $SYSROOT-o my_jni_mouse_exe jni_mouse.c my_main_func.c
#注: '--sysroot=dir ': Use Dir as the logical root directory for headers and libraries.

3) Compile the shared library (if there is no main function), such as:
$ $CC--sysroot= $SYSROOT-fpic-o jni_mouse.o-c jni_mouse.c # optional '-O ... '
$ $CC--sysroot= $SYSROOT-shared-o libjni_mouse.so JNI_MOUSE.O
# Strip again to reduce its size (directly overwriting the original library file):
$ $STRIP libjni_mouse.so

4) (optional) link to the shared library that you just created:
$ $CC--sysroot= $SYSROOT-l-ljni_mouse-o My_hello my_hello.c

<EOF>

Two ways to compile JNI libraries under "Go" Android (with examples)--good

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.