Ubuntu下第一个Jni例子
Ubuntu下第一个Jni例子
终端切换到 <sdk>/tools/ 目录下执行 Android 命令就可以把AVD Manager 打开了。 可是,如果你没有添加 <sdk>/tools 到你的环境变量中时,输入 android 回车后终端只会提示你
android:找不到命令
而只要在android前加上 ./ 就可以解决问题:
./android
更简单的办法是将 <sdk>/tools 路径添加进 PATH 环境变量。可以添加进用户级环境变量,也可以添加到系统环境变量中。通过命令或编辑文件均可,在这里我只提供一个办法,打开终端,输入:
sudo gedit /etc/environment
回车,在PATH=”………………….”的双引号中追加上:
:<sdk>/tools:<sdk>/platform-tools
比如:
:/opt/android-sdk/tools:/opt/android-sdk/platform-tools
注意,:是分隔符。
重启一下或者 source /etc/environment(立即生效) 在终端输入 android 回车就会有反应了。
安装Android NDK
下载Android NDK : http://developer.android.com/sdk/ndk/index.html
echo 解压缩得到android-ndk-r6b目录,即可。 tar -jxvf android-ndk-r6b-linux-x86.tar.bz2
也将其路径加入到source路径中
经过了上述步骤,在命令行下敲:
ndk-bulid
弹出如下的错误,而不是说ndk-build not found,就说明ndk环境已经安装成功了。
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
/home/braincol/workspace/android/android-ndk-r5/build/core/build-local.mk:85: *** Android NDK: Aborting . Stop.
二、代码的编写
1.首先是写java代码
建立一个Android应用工程HelloJni,创建HelloJni.java文件:
HelloJni.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" ); } }
这段代码很简单,注释也很清晰,这里只提两点::
static{
System.loadLibrary("hello-jni" );
}
表明程序开始运行的时候会加载hello-jni, static区声明的代码会先于onCreate方法执行。如果你的程序中有多个类,而且如果HelloJni这个类不是你应用程序的入口,那么 hello-jni(完整的名字是libhello-jni.so)这个库会在第一次使用HelloJni这个类的时候加载。
public native String stringFromJNI();
public native String unimplementedStringFromJNI();
可以看到这两个方法的声明中有 native 关键字, 这个关键字表示这两个方法是本地方法,也就是说这两个方法是通过本地代码(C/C++)实现的,在java代码中仅仅是声明。
用eclipse编译该工程,生成相应的.class文件,这步必须在下一步之前完成,因为生成.h文件需要用到相应的.class文件。
|
评论暂时关闭