[Change] Android JNI examples

Android's SDK does not include the JNI support, but also how to support and no documentation of JNI. But since the Android platform is open source, we can release the source code to Google to find some clues (such as frameworks / base / media / jni / directory), the realization of the upper Slangin huapiao JAVA program to call Native C through JNI program functions.

The following steps can be achieved in accordance with a very simple example of JNI programs:

1. First, write C modules, dynamic libraries. (On how to compile the C module in the Android For more details, please refer to " Android build environment (1) - Compile Native C of the helloworld module . ")

In the development directory to add a new directory hellolib, and add hellolib.c and Android.mk file. hellolib.c reads as follows:

# Include

# Define LOG_TAG "TestLib"

# Undef LOG

# Include

JNIEXPORT void JNICALL Java_com_test_TestHelloLib_printHello (JNIEnv * env, jobject jobj)

(

LOGD ("Hello LIB! \ N");

)

Note that the function names need to follow the JNI specification (and therefore can also use javah-jni tools to generate header files, to ensure the accuracy of the function name), Java_com_test_TestHelloLib_printHello behind the naming of the corresponding code in java, package name is com.test, class name is TestHelloLib, native function name is printHello.

In addition, LOGD and # define LOG_TAG "TestLib" other way is to print the log provided by the Android LOG mechanism, so as to see through the Android's logcat tool log.

Used to compile the C module Android.mk file as follows:

LOCAL_PATH: = $ (call my-dir)

include $ (CLEAR_VARS)

LOCAL_SRC_FILES: = \

hellolib.c

LOCAL_C_INCLUDES: = \

$ (JNI_H_INCLUDE)

LOCAL_SHARED_LIBRARIES: = \

libutils

LOCAL_PRELINK_MODULE: = false

LOCAL_MODULE: = libhello

include $ (BUILD_SHARED_LIBRARY)

The file corresponding to the meaning of some variables as follows:

LOCAL_SRC_FILES - compile the source file

LOCAL_C_INCLUDES - need to include the header file directory

LOCAL_SHARED_LIBRARIES - Link requires an external library

LOCAL_PRELINK_MODULE - the need to prelink processing (refer to prelink the details: " Dynamic Storage Optimization - Prelink (pre-connection) technology ", Android's Toolchain, prelink tool:" Android Toolchain and Bionic Libc ")

LOCAL_MODULE - compile target

BUILD_SHARED_LIBRARY - specified to be compiled into a dynamic library.

Then back to Android top directory, and execute make libhello to compile:

# Cd $ (YOUR_ANDROID) & & make libhello

target thumb C: libhello <= development / hellolib / hellolib.c

target SharedLib: libhello (out / target / product / generic / obj / SHARED_LIBRARIES / libhello_intermediates / LINKED / libhello.so)

target Non-prelinked: libhello (out / target / product / generic / symbols / system / lib / libhello.so)

target Strip: libhello (out / target / product / generic / obj / lib / libhello.so)

Install: out / target / product / generic / system / lib / libhello.so

Compile the results obtained in the out / target / product / generic / system / lib / directory of the dynamic shared libraries libhello.so

2. Write Java module to call through the JNI C Interface mode. Eclipse environment set up particular Android SDK documentation please refer to the detailed description, and Hello Android program creation process, here we only need to modify the TestHelloLib.java given file:

package com.test;

import android.app.Activity;

import android.os.Bundle;

public class TestHelloLib extends Activity (

/ ** Called when the activity is first created. * /

@ Override

public void onCreate (Bundle savedInstanceState) (

super. onCreate (savedInstanceState);

setContentView (R.layout. main);

printHello ();

)

static (

System. LoadLibrary ("hello");

)

private native void printHello ();

)

Note that the above bold part of the code: private native void printHello () is used to declare a native interfaces, static (System.loadLibrary ("hello");) used to load the above steps to generate libhello.so (Note loadLibrary method parameter is not "libhello.so", but after removing the prefix and suffix "hello"), onCreate () method is called printHello () interfaces.

This step can be generated through the Android developers are familiar with apk file: TestHelloLib.apk.

3. Integration test TestHelloLib.apk and libhello.so. First run the emulator and TestHelloLib.apk and libhello.so uploaded to the emulator in. Note to libhello.so uploaded to the emulator's / system / lib directory, since the directory is read-only, first before uploading the implementation of adb remount:

# Adb remount

# Adb push out / target / product / generic / system / lib / libhello.so / system / lib

# Adb install TestHelloLib.apk

Next, the menu can be seen in the simulator has been installed TestHelloLib program can be run.

As the JNI interface to printHello () does not make the interface changes, to verify the performance of logcat Android tools needed to view. Run "adb logcat" can be found in the following log snippet:

I / ActivityManager (48): Starting activity: Intent (action = android.intent.action.MAIN categories = (android.intent.category.LAUNCHER) flags = 0x10200000 comp = (com.test / com.test.TestHelloLib))

I / ActivityManager (48): Start proc com.test for activity com.test / .TestHelloLib: pid = 174 uid = 10024 gids = ()

D / dalvikvm (174): Trying to load lib / system / lib / libhello.so 0x43481c58

D / dalvikvm (174): Added shared lib / system / lib / libhello.so 0x43481c58

D / dalvikvm (174): No JNI_OnLoad found in / system / lib / libhello.so 0x43481c58

D / dalvikvm (174): + + + not scanning''/ system / lib / libwebcore.so''for''printHello''(wrong CL)

D / dalvikvm (174): + + + not scanning''/ system / lib / libmedia_jni.so''for''printHello''(wrong CL)

D / TestLib (174): Hello LIB!

I / ActivityManager (48): Displayed activity com.test / .TestHelloLib: 806 ms

This contains a call printHello () interface to the log information, including "D / TestLib (174): Hello LIB!" Is printHello () by printing the information. Thus the successful completion of Android JNI examples are illustrated.

分类:Mobile 时间:2010-04-27 人气:1255
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.038 (s). 14 q(s)