官方实例:android ubuntu ndk jni hello-jni
2011-08-24 14:12
399 查看
这里我贴一下其它网友有关为什么用jni的原因:
你的Java代码,需要得到一个文件的属性。但是你找遍了JDK帮助文档也找不到相关的API。
在本地还有一个别的系统,不过他不是Java语言实现的,这个时候你的老板要求你把两套系统整合到一起。
你的Java代码中需要用到某种算法,不过算法是用C实现并封装在动态链接库文件(DLL)当中的。
参考资料:http://developer.android.com/sdk/ndk/index.html 、http://developer.android.com/sdk/ndk/overview.html
下 载 android-ndk-r6-linux-x86.tar.bz2 然后解压,那个解压后会有个README.txt文件大概对doc包下的一些说明文档进行了粗略的介绍,那个文档中有下面的指导,所以需要认真的看一下。
本地操作:
pateo@pateo-B86N53X:~$ pwd
/home/pateo
pateo@pateo-B86N53X:~$ gedit .bashrc
在文件中输入的内容:
export NDK_HOME=/home/pateo/work/android-ndk-r6
export PATH=$PATH:$NDK_HOME
然后保存
pateo@pateo-B86N53X:~$ source .bashrc
pateo@pateo-B86N53X:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/pateo/work/android-ndk-r6
pateo@pateo-B86N53X:~$ cd work/android-ndk-r6/samples/hello-jni
pateo@pateo-B86N53X:~/work/android-ndk-r6/samples/hello-jni$ ndk-build
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Compile thumb : hello-jni <= hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
pateo@pateo-B86N53X:~/work/android-ndk-r6/samples/hello-jni$ $NDK_HOME/ndk-buildGdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
把官方自带的hello-jni的工程导入到Eclipse中运行
把上面的代码贴出来让大家看一下:
java文件
c文件
你的Java代码,需要得到一个文件的属性。但是你找遍了JDK帮助文档也找不到相关的API。
在本地还有一个别的系统,不过他不是Java语言实现的,这个时候你的老板要求你把两套系统整合到一起。
你的Java代码中需要用到某种算法,不过算法是用C实现并封装在动态链接库文件(DLL)当中的。
参考资料:http://developer.android.com/sdk/ndk/index.html 、http://developer.android.com/sdk/ndk/overview.html
下 载 android-ndk-r6-linux-x86.tar.bz2 然后解压,那个解压后会有个README.txt文件大概对doc包下的一些说明文档进行了粗略的介绍,那个文档中有下面的指导,所以需要认真的看一下。
本地操作:
pateo@pateo-B86N53X:~$ pwd
/home/pateo
pateo@pateo-B86N53X:~$ gedit .bashrc
在文件中输入的内容:
export NDK_HOME=/home/pateo/work/android-ndk-r6
export PATH=$PATH:$NDK_HOME
然后保存
pateo@pateo-B86N53X:~$ source .bashrc
pateo@pateo-B86N53X:~$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/pateo/work/android-ndk-r6
pateo@pateo-B86N53X:~$ cd work/android-ndk-r6/samples/hello-jni
pateo@pateo-B86N53X:~/work/android-ndk-r6/samples/hello-jni$ ndk-build
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Compile thumb : hello-jni <= hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
pateo@pateo-B86N53X:~/work/android-ndk-r6/samples/hello-jni$ $NDK_HOME/ndk-buildGdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
把官方自带的hello-jni的工程导入到Eclipse中运行
把上面的代码贴出来让大家看一下:
java文件
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.hellojni; import android.app.Activity; import android.widget.TextView; import android.os.Bundle; public class HelloJni extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* Create a TextView and set its content. * the text is retrieved by calling a native * function. */ TextView tv = new TextView(this); tv.setText( stringFromJNI() ); setContentView(tv); } /* A native method that is implemented by the * 'hello-jni' native library, which is packaged * with this application. */ public native String stringFromJNI(); /* This is another native method declaration that is *not* * implemented by 'hello-jni'. This is simply to show that * you can declare as many native methods in your Java code * as you want, their implementation is searched in the * currently loaded native libraries only the first time * you call them. * * Trying to call this function will result in a * java.lang.UnsatisfiedLinkError exception ! */ public native String unimplementedStringFromJNI(); /* this is used to load the 'hello-jni' library on application * startup. The library has already been unpacked into * /data/data/com.example.HelloJni/lib/libhello-jni.so at * installation time by the package manager. */ static { System.loadLibrary("hello-jni"); } }
c文件
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include <string.h> #include <jni.h> /* This is a trivial JNI example where we use a native method * to return a new VM String. See the corresponding Java source * file located at: * * apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java */ jstring Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz ) { return (*env)->NewStringUTF(env, "Hello from JNI !"); }
相关文章推荐
- Android:JNI 与 NDK到底是什么?(含实例教学)
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- android Jni NDK开发环境搭建及其简单实例的编写
- Android-NDK开发之基础--Android JNI实例代码(四)-- JNI中的异常处理实例代码
- Android-使用Android studio 进行NDK-JNI开发实例(一)
- android下NDK开发环境搭建及TestJNI入门实例完整过程
- Android-java调用本地方法返回字符串显示在界面上/NDK-JNI开发实例(二)
- Android-使用C++实现调用本地方法返回字符串显示在界面上/NDK-JNI开发实例(八)
- Android:JNI 与 NDK到底是什么?(含实例教学)
- 【android NDK及JNI】ubuntu12.10 64位 + android-ndk-r9 编译ffmpeg遇到的问题 .
- Android JNI和NDK学习(08)--JNI实例一 传递基本类型数据
- android Jni NDK开发环境搭建及其简单实例的编写
- Android JNI和NDK学习(08)--JNI实例一 传递基本类型数据
- Android:JNI 与 NDK到底是什么?(含实例教学)
- Ubuntu下搭建 Eclipse+NDK for Android JNI 的开发环境
- Android:JNI 与 NDK到底是什么?(含实例教学)
- Android—JNI调用简单实例解析/Eclipse无添加NDK的选项/JNI返回数组
- Android:JNI 与 NDK到底是什么?(含实例教学)
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- JNI for Android, Android Studio NDK 入门 实例 demo