Android JNI (implement your own jni_onload function)

Source: Internet
Author: User

You can register local functions in JNI in two ways:
(1) use the default local function registration process.
(2) rewrite the jni_onload () function by yourself. (This article introduces) (this is used in Android)

Java code:

package com.jni;public class JavaHello {public static native String hello();static {// load library: libtest.sotry {System.loadLibrary("test");} catch (UnsatisfiedLinkError ule) {System.err.println("WARNING: Could not load library!");}}public static void main(String[] args) {String s = new JavaHello().hello();System.out.println(s);}}

Local C language code:

# Include <stdlib. h> # include <string. h> # include <stdio. h> # include <JNI. h> # include <assert. h> jniexport jstring jnicall native_hello (jnienv * ENV, jclass clazz) {printf ("hello in C native code. /n "); Return (* env)-> newstringutf (ENV," Hello World returned. ") ;}# define jnireg_class" com/JNI/javahello "// specify the class to be registered/*** table of methods associated with a single class. */static jninativemethod gmethods [] = {"Hello "," () Ljava/lang/string; ", (void *) native_hello}, // bind};/** register several native methods for one class. */static int evaluate (jnienv * ENV, const char * classname, jninativemethod * gmethods, int nummethods) {jclass clazz; clazz = (* env)-> findclass (ENV, classname); If (clazz = NULL) {return jni_false;} If (* env)-> registernatives (ENV, clazz, gmethods, nummethods) <0) {return jni_false;} re Turn jni_true;}/** register native methods for all classes we know about. */static int registernatives (jnienv * env) {If (! Registernativemethods (ENV, jnireg_class, gmethods,
Sizeof (gmethods)/sizeof (gmethods [0]) return jni_false; return jni_true;}/** set some test stuff up. ** returns the JNI version on success,-1 on failure. */jniexport jint jnicall jni_onload (JavaVM * Vm, void * Reserved) {jnienv * Env = NULL; jint result =-1; if (* VM)-> getenv (Vm, (void **) & ENV, jni_version_1_4 )! = Jni_ OK) {return-1;} assert (ENV! = NULL); If (! Registernatives (ENV) {// register return-1;}/* success -- Return valid version number */result = jni_version_1_4; return result ;}

Compilation and running process:

1. Set three environment variables:
Export java_home: =/usr/lib/JVM/Java-6-sun-1.6.0.15
Export java_src_path: =/home/kortide/jackey/JNI/jni_onload/COM/jfo
Export native_src_path: =/home/kortide/jackey/JNI/jni_onload/JNI

2. Compile javahello. Java:
Javac $ java_src_path/javahello. Java

3. Compile nativehello. C to generate a shared library.
Gcc-I $ java_home/include/Linux-c-o $ native_src_path/nativehello. o $ native_src_path/nativehello. c

Gcc-FPIC-I $ java_home/include/Linux-shared-o $ native_src_path/libtest. So $ native_src_path/nativehello. o

4. Run
Java COM/JNI/javahello

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.