一个打通jni,java framework,application三层的练习
要求:
1.需要包含三层结构:JNI, java framework, java application
2.字符串的内容为“Hello from JNI !”,必须在JNI层定义
这个练习是在Android的源码工程下做的,我的Android源代码路径为/Android/android-1.6_r2
开工:
1. JNI层 :在 /Android/android-1.6_r2/frameworks/base/core/jni 路径下创建例子 android_mytest_hellojni.cpp 文件,这个文件就是在 JNI 层实现接口。文件内容如下:(可参考同一目录下的 android_debug_JNITest.cpp 文件编写)
Cpp代码- #defineLOG_TAG"HelloJNI"
- #include"jni.h"
- #include"nativehelper/JNIHelp.h"
- #include"utils/Log.h"
- #include"utils/misc.h"
- namespaceandroid{
- staticjstringandroid_mytest_hellojni_displayString(JNIEnv*env,jclassclazz)
- {
- returnenv->NewStringUTF("HellofromJNI!");
- }
- /*
- *JNIregistration.
- */
- staticJNINativeMethodgMethods[]={
- /*name,signature,funcPtr*/
- {"displayString","()Ljava/lang/String;",
- (void*)android_mytest_hellojni_displayString},
- };
- intregister_android_mytest_hellojni(JNIEnv*env)
- {//此处的目录结构就是在Javaframework层的文件目录,且必须一致
- returnjniRegisterNativeMethods(env,"android/mytest/hellojni",
- gMethods,NELEM(gMethods));
- }
- };
#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){ //此处的目录结构就是在Javaframework层的文件目录,且必须一致return jniRegisterNativeMethods(env, "android/mytest/hellojni", gMethods, NELEM(gMethods));}};
2. JNI 层:对编译的修改配置
2.1修改/Android/android-1.6_r2/frameworks/base/core/jni目录下的Android.mk 文件,在LOCAL_SRC_FILES:= \ 下面加上
android_mytest_hellojni.cpp \
2.2修改/Android/android-1.6_r2/frameworks/base/core/jni目录下的AndroidRuntime.cpp 文件在extern int 后面添加
extern int register_android_mytest_hellojni(JNIEnv* env);
然后在static const RegJNIRec gRegJNI[] = {下面添加
REG_JNI(register_android_mytest_hellojni),
这样,JNI层的修改就到此为止了。
3. Javaframework 层:在 /Android/android-1.6_r2/frameworks/base/core/java/android/ 新建文件目录 mytest ,在该目录下新建文件 hellojni.java 声明接口。内容如下:(可以参考 android-1.6_r2/frameworks/base/core/java/android/debug 目录下的 JNITest.java 文件编写)
Java代码- packageandroid.mytest;
- publicclasshellojni{
- publichellojni(){}
- //此处声明为public所以才可以被application调用
- publicstaticnativeStringdisplayString();
- }
package android.mytest;public class hellojni{public hellojni(){}//此处声明为public所以才可以被application调用public static native String displayString(); }
4 .下面我们要对我们做过更改的 libandroid_runtime.so 和 framework.jar 进行重新编译。
在源代码工程目录下输入 make libandroid_runtime 重新编译生成 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
然后再输入 make framework 重新编译生成 framework.jar
…
Install: out/target/product/generic/system/framework/framework.jar
5. 然后在 eclipse 里面或者在记事本等编辑器(这么说只是为了说明只需要源代码),新建 Android project ,例子的结构如下:
在 testApp.java 文件中的内容如下:
Java代码- packagecom.integration.test;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.widget.TextView;
- //需要import自己定义的包,包名可以根据文件目录结构得到
- importandroid.mytest.hellojni;
- publicclasstestAPPextendsActivity{
- /**Calledwhentheactivityisfirstcreated.*/
- privateTextViewtv;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- tv=(TextView)findViewById(R.id.tv);
- hellojnihello=newhellojni();//调用自己写的JNI
- tv.setText(hello.displayString()+"");
- }
- }
package com.integration.test;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;//需要import自己定义的包,包名可以根据文件目录结构得到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(); //调用自己写的JNI tv.setText(hello.displayString()+""); }}
然后将上述文件复制到 /Android/android1.6_r2/packages/apps ,然后看教程《在源码中编译自己的 Android project 》就可以在模拟器中看到该应用运行如下图
更多相关文章
- NPM 和webpack 的基础使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值
- Android系统编译
- Android如何使用so文件和Android(安卓)studio中导入so
- android UI横竖屏的处理
- android studio使用问题及说明
- android 获取uri的正确文件路径的办法
- Android(安卓)- 文件读写操作 总结