Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
C Intro.
Inlining Java Native Calls at Runtime (CASCON 2005 – 4 th Workshop on Compiler Driven Performance) Levon Stepanian, Angela Demke Brown Computer Systems.
This work is licensed under the Creative Commons Attribution 4.0 International License. To view a copy of this license, visit
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Java Native Interface Modified from CS587x Lecture Department of Computer Science Iowa State University.
Writing Native Code for Android Systems. Why ndk There exist large c++ code libraries – E.g., Audio and video compression, e.g., Ogg Vorbis, The LAME.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
Session 1 CS-240 Data Structures Binghamton University Dick Steflik.
Java Native Interface (JNI). JNI Linking Java and C code.
COMP 14: Intro. to Intro. to Programming May 23, 2000 Nick Vallidis.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Java Basics Kris Secor Mobile Application Development.
Prepared by Uzma Hashmi Instructor Information Uzma Hashmi Office: B# 7/ R# address: Group Addresses Post message:
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Java Native Interface CS587x Lecture Department of Computer Science Iowa State University.
Moving from C to Java. The Java Syntax We Know Java Types Some of the types we know and love are still there  Integer: byte, short, char, int, long.
What is Android NDK ● A toolset that lets you embed in you app native source code ● C, C++(recently supported December 2010) and assembly(?) ● It is supported.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Rutgers University Excellence Campaign 2/20/2004 Java Native Interface Tutorial Xiaolin Li Rutgers.
Java Introduction to JNI Prepared by Humaira Siddiqui.
By Noorez Kassam Welcome to JNI. Why use JNI ? 1. You already have significantly large and tricky code written in another language and you would rather.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Android Cross Build and SUSI_3.02 Environments Settings Modify the file android_env.mak to fit your Android version and source path $vim SUSI_3.02/
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
1 Introduction to Java. 2 What is Java? A programming language. A platform –A virtual machine (JVM) definition. –Runtime environments in diverse hardware.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Copyright Curt Hill Variables What are they? Why do we need them?
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
Slide Advanced Programming 2004, based on LY Stefanus's slides Native Methods.
Java 变量类型与 native 变量类型 java 类型 native 类型描述对应的 C 类型 booleanjbooleanunsigned 8 bitsunsigned char bytejbytesigned 8 bitschar jcharunsigned 16 bitsunsigned.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
C is a high level language (HLL)
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Object Oriented Programming Lecture 2: BallWorld.
Introduction to Java Programming by Laurie Murphy Revised 09/08/2016.
Java Programming Language Lecture27- An Introduction.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
C++ Lesson 1.
Java Native Interface (JNI)
Java for Android is specific
Java package classes Java package classes.
C Basics.
Unit-2 Objects and Classes
null, true, and false are also reserved.
Introduction to Java Programming
An overview of Java, Data types and variables
JavaScript Reserved Words
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Names of variables, functions, classes
Subtype Substitution Principle
Presentation transcript:

Android JNI and JAR Library JNI Library 1. JNI *.c sources file must include jni header file jni.h #include 2. In Make file CFLAGS must add -I $(NDK_INC)/ and android_evn.mak must have It in Makefile_android CFLAGS += -I $(NDK_INC)/ android_evn.mak ifeq ($(VER40), $(VER)) NDK_INC = $(ANDROID_SRC)/prebuilt/ndk/android-ndk-r6/platforms/android-9/arch- $(ARCH)/usr/include NDK_LIB = $(ANDROID_SRC)/prebuilt/ndk/android-ndk-r6/platforms/android-9/arch- $(ARCH)/usr/lib Endif ( Android 4.0 ndk/android-ndk-r6/platforms/android-9/arch-x86/usr/include/jni.h ) ( Android 2.2 ndk/android-ndk-r4/platforms/android-8/arch-x86/usr/include/jni.h )

3.How to implements C function method JNIEXPORT jboolean JNICALL Java_ com_Advantech_SUSI_SUSILIB_SusiDllInit(JNIEnv *, jobject) { return SusiDllInit(); // SusiDllInit SUSI C function and return boolean } All available types define as below., Java Language Type Native Type Description boolean jboolean unsigned 8 bits byte jbyte signed 8 bits char jchar unsigned 16 bits short jshort signed 16 bits int jint signed 32 bits long jlong signed 64 bits float jfloat 32 bits double jdouble 64 bits com_Advantech_SUSI is packages name and SUSILIB is jar class library Implements in java file as below., import com.Advantech.SUSI.SUSILIB; // For Android APP JNIEXPORT and JNICALL are macros defined at jni.h Ref The Java™ Native Interface Programmer’s Guide and Specification ( ISBN )

JNI use Get/Release ArrayElements functions to let native library code to obtain a direct pointer to the elements of primitive arrays ( In Java no pointer only array) If use Get ArrayElements and must use Get ArrayElements to release memory in Virtual Machine JNIEXPORT jboolean JNICALL Java_com_Advantech_SUISI_SUSILIB_SusiHWMGetFanSpeed (JNIEnv* env, jobject obj, jshort fanType, jshortArray retval, jshortArray typeSupport ) { jboolean jbool; jshortArray jretval; jshortArray jtypeSupport; if( retval != NULL ) jretval = (*env)->GetShortArrayElements(env, retval, NULL); // If use C++ change to env->GetShortArrayElements // Use Get ArrayElements to pointer to allow the native code to obtain a direct pointer to the elements of primitive //arrays. if( typeSupport != NULL ) jtypeSupport = (*env)->GetShortArrayElements(env, typeSupport, NULL); // To obtain a direct pointer to the elements of primitive arrays if( typeSupport != NULL ) { jbool = SusiHWMGetFanSpeed ( (WORD) fanType, (WORD *) 0, (WORD *) jtypeSupport ); // Can’t pass NULL instead use (void *) 0 or (WORD *) 0 } else { jbool = SusiHWMGetFanSpeed ( (WORD) fanType, (WORD *) jretval, (WORD *) 0 ); } if( retval != NULL ) (*env)->ReleaseShortArrayElements(env, retval, jretval, 0); if( typeSupport != NULL ) (*env)->ReleaseShortArrayElements(env, typeSupport, jtypeSupport, 0); return jbool; }

JAR Library 1. Ceate a new Java class with Eclipse for Android APP call SUSI Methods package com.Advantech.SUSI; import android.util.Log; public class SUSILIB { public static native boolean SusiHWMGetFanSpeed(short fanType, short[] retval, short[] typeSupport); static { try { Log.i("JAVA", "System.load(/system/lib/libSUSI-JNI.so)"); System.loadLibrary("SUSI-JNI"); // Load SUSI JNI library libSUSI-JNI.so from /system/lib } catch (UnsatisfiedLinkError use) { String msg = use.getMessage(); Log.e("JAVA", msg); Log.e("JAVA", "WARNING: Could not load libSUSI-JNI.so"); }

2. Use Eclipse to build it and export a jar file for SUSI Demo APP In Package Explorer first to select Export then select export destination

Select resource files to be created as JAR file and output JAR file, Press finish.