Java Basics JNI using System.loadlibrary () __java in Android

Source: Internet
Author: User

tiny6410 part of the hardware (serial, LED, PWM buzzer, ADC, etc.) friendly made a hardware library friendlyarm-hardware.so

If the Android application wants to access the above hardware, it will use Java JNI technology


1. The full name of JNI is: Java Native Interface,java local interface, designed to achieve local interaction with C/s + +, not in Java or easy to use code snippets.

2, the writing of JNI steps

A, Java classes that write methods with native declarations

b, using the Javac command to compile a class implemented in a

C, Javah-jni Java class name generate header file with extension h

D. Implement local methods by using C + +

E, generate a dynamic link library from the local method in D

F, OK


1. Write Java program

Class helloworld{
public native void Displayhelloworld ();
static{
System.loadlibrary ("Hello");
}
public static void Main (string[] args) {
New HelloWorld (). Displayhelloworld ();
}
}

Javac Hello.java

Then Javah command

Javah-jni HelloWorld

Generate. h header file as

/* Do isn't EDIT this file-it is machine generated * *
#include <jni.h>
/* Header for class HelloWorld * *


#ifndef _included_helloworld
#define _included_helloworld
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:helloworld
* Method:displayhelloworld
* Signature: () V
*/
jniexport void Jnicall Java_helloworld_displayhelloworld
(JNIENV *, jobject);


#ifdef __cplusplus
}
#endif
#endif



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.