A three-tier exercise for connecting jni, java framework, and application

Source: Internet
Author: User

From: http://blogold.chinaunix.net/u4/123238/showart_2513848.html thank you very much

A three-tier exercise for connecting JNI, Java framework, and Application

 
 
One layer-3 exercise for connecting JNI, Java framework, and application, Daniel wood 08/27/2010

Preface: This article was posted in my original blog very early. I opened this blog here because I couldn't stand the garbled problem of the original blog, now, I will officially move it to this blog. Do not say I copied it ^-^. Exercise: obtain a string from the JNI layer and display it on the screen. Of course, the requirement for the mobile phone screen is as follows:
1. It must contain three layers: JNI, Java framework, and Java application.
2. The content of the string is "hello from JNI !", Must be defined in the JNI layer this exercise is done in the android source code project, my android source code path for/Android/android-1.6_r2 started:

1. JNI layer: Create the example android_mytest_hellojni.cpp file in the/Android/android-1.6_r2/frameworks/base/CORE/JNI path. This file is the interface implemented in the JNI layer. The file content is as follows: (you can refer to the android_debug_jnitest.cpp file in the same directory)

#define LOG_TAG "HelloJNI"
#include "jni.h"
#include "nativehelper/JNIHelp.h"
#include "utils/Log.h"
#include "utils/misc.h"

namespace android {
static jstring android_mytest_hellojni_displayString(JNIEnv *env, jclass clazz)
{
 return env->NewStringUTF("Hello from JNI!");
}

/*
* JNI registration.
*/
Static JNINativeMethod gMethods [] = {
/* Name, signature, funcPtr */
{"DisplayString", "() Ljava/lang/String ;",
(Void *) android_mytest_hellojni_displayString },

};
Int register_android_mytest_hellojni (JNIEnv * env)
{// The directory structure is the file directory at the Javaframework layer and must be consistent

Return jniRegisterNativeMethods (env, "android/mytest/hellojni ",
GMethods, NELEM (gMethods ));
}
};

2.JNI Layer: Modify the compilation Configuration

2.1 modify the Android. mk file under the/Android/android-1.6_r2/frameworks/base/CORE/JNI directory, add
Android_mytest_hellojni.cpp/
2.2 Add the androidruntime. cpp file under the/Android/android-1.6_r2/frameworks/base/CORE/JNI directory after the extern int
Extern int register_android_mytest_hellojni (jnienv * env );
Add the following content under static const regjnirec gregjni [] = {
Reg_jni (register_android_mytest_hellojni ),
In this way, the modification of the JNI layer ends here.

 

 

 

3. javaframework layer: In/Android/android-1.6_r2/frameworks/base/CORE/Java/Android/new file directory mytest, Create File hellojni. Java declaration interface under this directory. The content is as follows :( you can refer to the jnitest. Java file under the android-1.6_r2/frameworks/base/CORE/Java/Android/DEBUG directory)

Package android. mytest;
Public class hellojni {
Public hellojni (){}
// This is declared as public, so it can be called by the application.

Public static native String displayString ();
}

4. Next we will re-compile the libandroid_runtime.so and framework. jar that we have changed.

Enter make libandroid_runtime in the source code project directory to re-compile and generate libandroid_runtime.so

...

Target thumb C ++: libandroid_runtime <= frameworks/base/CORE/JNI/android_mytest_hellojni.cpp

Target thumb C ++: libandroid_runtime <= frameworks/base/core/jni/AndroidRuntime. cpp

Target SharedLib: libandroid_runtime (out/target/product/generic/obj/SHARED_LIBRARIES/libandroid_runtime_intermediates/LINKED/libandroid_runtime.so)

Target Prelink: libandroid_runtime (out/target/product/generic/symbols/system/lib/libandroid_runtime.so)

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

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

Then input make framework to re-compile and generate framework. jar.

...

Install: out/target/product/generic/system/framework. jar

 

5. Create an Android project in eclipse or in Notepad Editor (just to illustrate that you only need the source code). The example structure is as follows:

The content in the testApp. java file is as follows:

Package com. integration. test;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. widget. TextView;
// Import a custom package. The package name can be obtained based on the file directory structure.

Import android. mytest. hellojni;
Public class testAPP extends Activity {
/** Called when the activity is first created .*/
Private TextView TV;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
TV = (textview) findviewbyid (R. Id. TV );
Hellojni Hello = new hellojni (); // call the self-written JNI

TV. settext (hello. displaystring () + "");
}
}

Then copy the above file to/Android/android1.6 _ r2/packages/apps, and then read the Tutorial "compile your own Android project in the source code" to view the application running in the simulator as follows:


 

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.