Ubuntu10.04中SDK、NDK环境的搭建

1.安装Ubuntu;
2.下载并安装JDK
本次安装的是jdk-6u23-linux-i586.bin,下载到要安装的目录下面,
添加运行权限:

chmod +x jdk-6u23-linux-i586.bin

运行安装程序:

./jdk-6u23-linux-i586.bin


加压完毕之后便可以设置jdk的环境变量了,
第一种方案:在$HOME的.bashrc中修改:

gedit ~/.bashrc

在其下方添加:

export JAVA_HOME=/home/ben/programFiles/java/jdk1.6.0_23
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

请注意JAVA_HOME就是安装JDK的路径。

运行:

source .bashrc

使变量生效

第二种方案:修改environment
打开之:

sudo gedit /etc/environment


在其中添加:

PATH="-保持原来的样子-:/home/ben/programFiles/java/jdk1.6.0_23/bin"
CLASSPATH=.:/home/ben/programFiles/java/jdk1.6.0_23/lib
JAVA_HOME=/home/ben/programFiles/java/jdk1.6.0_23



第三种方案:修改/etc/profile

sudo gedit /etc/profile


添加:

export JAVA_HOME=/home/ben/programFiles/java/jdk1.6.0_23
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

注销后重新进入 或者运行source ./.bashrc

注意:如果系统中自带open-jdk则应更改默认jdk命令,
添加命令所链接的快捷方式

sudo update-alternatives --install /usr/bin/java java /home/ben/programFiles/java/jdk1.6.0_23/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /home/ben/programFiles/java/jdk1.6.0_23/bin/javac 300
sudo update-alternatives --install /usr/bin/jar jar /home/ben/programFiles/java/jdk1.6.0_23/bin/jar 300
sudo update-alternatives --install /usr/bin/javah javah /home/ben/programFiles/java/jdk1.6.0_23/bin/javah 300
sudo update-alternatives --install /usr/bin/javap javap /home/ben/programFiles/java/jdk1.6.0_23/bin/javap 300

添加完毕后,修改之:

sudo update-alternatives --config java

有 2 个选项可用于替换项 java (提供 /usr/bin/java)。

选择 路径 优先级 状态
------------------------------------------------------------
* 0 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 自动模式
1 /home/ben/programFiles/java/jdk1.6.0_23/bin/java 300 手动模式
2 /usr/lib/jvm/java-6-openjdk/jre/bin/java 1061 手动模式

要维持当前值[*]请按回车键,或者键入选择的编号:1

修改完毕,运行

java -version

java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode)



至此,jdk安装完毕。

3 下载Eclipse,并解压之,对其添加运行权限。

tar xzvf eclipse-SDK-[version-code]-linux-gtk.tar.gz


4 下载sdk并加压到相应目录
添加环境变量:

gedit .bashrc

添加:

export PATH=$PATH:~/programFiles/android/android-sdk-linux/tools:~/programFiles/android/android-sdk-linux/platform-tools

运行

source ~/.bashrc


5 下载DDMS等
eclipe-help-install new software
键入网址:
http://dl-ssl.google.com/android/eclipse/
或者
https://dl-ssl.google.com/android/eclipse/
下载安装。

6 下载加压NDK

http://developer.android.com/tools/sdk/ndk/index.html#
下载相应NDK,并解压到相应文件夹:

tar jxvf /home/ben/ben/android-ndk-r8e-linux-x86.tar.bz2


修改环境变量:

gedit ~/.bashrc

添加:

NDK=$HOME/programFiles/android/android-ndk-r8e/
export NDK


运行 source ~/.bashrc

随后可以echo $NDK 看是否正确

7 测试
(1)进入$NDK/samples/hello-jni
编译:

$NDK/ndk-build

会发现生成libs和objs文件夹
证明成功。
现将hello-jni通过从现有代码中导入的方式导入eclipse工作区间
运行即可。

(2)创建MyJni工程
创建jni文件夹,创建myjni.c

#include <string.h>

#include <stdio.h>

#include <jni.h>



#include <android/log.h>

#define LOG_TAG "MYJNI"



#define LOGI(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)



static char s_string[] = "My god, I did it!";



jstring

Java_cn_edu_bupt_ben_myjni_MyJniActivity_stringFromJNI( JNIEnv* env,

jobject thiz )

{

LOGI("MyJNI is called!");

return (*env)->NewStringUTF(env, s_string);

}

注意:Java_cn_edu_bupt_ben_myjni_MyJniActivity_stringFromJNI的命名是有规则的
Java,报名,类名,函数名
在jni中创建Android.mk文件

LOCAL_PATH := $(call my-dir)



include $(CLEAR_VARS)



LOCAL_MODULE := myjni

LOCAL_SRC_FILES := myjni.c



LOCAL_LDLIBS += -llog



include $(BUILD_SHARED_LIBRARY)


在src中创建activity:

package cn.edu.bupt.ben.myjni;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyJniActivity 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("myjni");
}
}




运行之,齐活!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值