Build an Android development environment under Ubuntu

    N many tutorials on the Internet are all on the Windows platform, and Google’s official website recommends using Ubuntu to develop Android, and many friends also use the Linux system to develop. Let me introduce the construction of the Android development environment under Linux.

1. Install and configure JDK under Linux

   A lot has changed in the nearly three years since Oracle acquired Sun. As early as August, Oracle terminated the "Operating System Distributor License for Java" license, which means that third parties will no longer be able to distribute their software packages under this license. So Ubuntu Linux has started disabling the Oracle JDK browser plugin on all machines and will soon remove the package from the archive.

 The company pointed out that disabling Oracle's plug-ins will help improve security, because these plug-ins have been proven to contain many vulnerabilities. Insecure software. Most PC users think it's safe, but users of UNIX-based systems usually don't. After Oracle's JDK is deprecated, OpenJDK will take its place and be installed by default in Ubuntu and other Linux. Although many Linux distributions now have their own OpenJDK, the development process is slightly different from Oracle-JDK (SUN-JDK). Usually, Java developers still use Oracle-JDK as the standard for development. The following introduces the JDK installation and configuration under Linux. The Linux distribution used here is Ubuntu Kylin 14.04.

1. Download JDK

The latest JDK version is: Java SE Development Kit 8u5

Download address: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

I just installed the latest version of JDK8, but the following exception was reported when compiling the Android 4.4 system:

============================================
Checking build tools versions...
************************************************************
You are attempting to build with the incorrect version
of java.
 
Your version is: java version "1.8.0_05".
The correct version is: Java SE 1.6.
 
Please follow the machine setup instructions at
    https://source.android.com/source/download.html
************************************************************
make:进入目录'/home/huangph/a20_kitkat_44/android'
build/core/main.mk:168: *** stop。 停止。
make:离开目录“/home/huangph/a20_kitkat_44/android”

官方推荐4.4编译所需要的是JDK6,我本以为最新的应该是最好的,不过看来并不一定,所以我又只好把它换成JDK6,刚开始旧的版本的JDK找了蛮久没找到在哪里下载,结果找了一下才知道下载旧版本JDK的地址,下面贴上旧版本JDK的下载地址:http://java.sun.com/products/archive/

2.解压安装

我们把JDK安装到这个路径:/usr/lib/jvm
如果没有这个目录(第一次当然没有),我们就新建一个目录

cd /usr/lib
sudo mkdir jvm

建立好了以后,我们来到刚才下载好的压缩包的目录,解压到我们刚才新建的文件夹里面去,并且修改好名字方便我们管理,我下载的是6u31,为了和我们的服务器的JDK版本保持一致。

如果是以.tar.gz为后缀的JDK的话,使用下面的命令解压:

sudo tar -zxvf jdk-8u5-linux-x64.tar.gz -C /usr/lib/jvm,我安装的是JDK6,是以.bin为后缀的,所以使用下面的命令:

sudo chmod a+x jdk-6u31-linux-x64.bin
./jdk-6u31-linux-x64.bin
sudo mv jdk1.6.0_31/ /usr/lib/jvm/jdk6

 3.配置环境变量

gedit ~/.bashrc

在打开的文件的末尾添加

export JAVA_HOME=/usr/lib/jvm/jdk6
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

保存退出,然后输入下面的命令来使之生效

source ~/.bashrc

 4.配置默认JDK

由于一些Linux的发行版中已经存在默认的JDK,如OpenJDK等。所以为了使得我们刚才安装好的JDK版本能成为默认的JDK版本,我们还要进行下面的配置。
执行下面的命令:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk6/bin/java 300
sudo update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/jdk6/bin/javac 300

 注意:如果以上两个命令出现找不到路径问题,只要重启一下计算机在重复上面两行代码就OK了。

执行下面的代码可以看到当前各种JDK版本和配置:

sudo update-alternatives --config java

执行上面命令后,我的系统下面有以下几个JDK的版本:
有 2 个候选项可用于替换 java (提供 /usr/bin/java)。

  选择       路径                                          优先级  状态
------------------------------------------------------------
* 0            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      自动模式
  1            /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java   1071      手动模式
  2            /usr/lib/jvm/jdk6/bin/java                       300       手动模式

然后选择2,这样就可以将默认的JDK版本切换到JDK6了。

 5.测试

打开一个终端,输入下面命令:

java -version

显示结果:

java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b04)
Java HotSpot(TM) 64-Bit Server VM (build 20.6-b01, mixed mode)

这表示java命令已经可以运行了。

二、安装Eclipse

下载地址:http://www.eclipse.org/downloads/packages/eclipse-standard-432/keplersr2

下载完成后,解压就好了。

三、下载安装Android SDK

1. 先下载好最新的 Android SDK Package。这里我给出官网的下载地址:

http://developer.android.com/sdk/index.html

下载好后当然是解压了,解压到您的工作目录,这个目录就是今后使用SDK的目录:

tar -zvxf android-sdk_r22.6.2-linux.tgz

解压找到 tools 目录下的 android 后如图:

这个就是 Android SDK Manager,你可以通过这个来配置、管理和下载最新的SDK。

First of all, we first add platforms and packages through the Android SDK Manager, open the Android SDK Manager and check the tools and packages you need, here the Android SDK Manager will check the recommended packages for you by default. As shown in the picture: I checked and installed all SDKs above 4.0, as shown in the picture:

2. Configure the Android SDK to develop the height ring mirror

Here we are going to configure the development and debugging environment so that we can use the SDK well in the console. If you just want to use Eclipse for Android development, you can also omit it here. But I still think the console is pretty good, although I feel dizzy looking at it piece by piece, haha.

First configure the environment variables, the same as configuring the JDK. Run the code to configure environment variables:

gedit ~/.bashrc

Add the following to the very end of the file:

# Android SDK
export ANDROID_SDK=/home/huangph/software/android-sdk-linux
export PATH=$ANDROID_SDK/platform-tools:$ANDROID_SDK/tools:$PATH

Of course, the content behind "ANDROID_SDK=" is of course the directory where your own SDK is located. Don't copy it, the above is on my computer. Remember to save after modification, and finally run it:

source ~/.bashrc

 In the later use, I found that it is normal to use the adb command in the console, but sometimes when we need root privileges, when we use sudo adb, it will prompt that the adb command cannot be found. Later, I found a solution, here is an explanation:

cd /usr/bin
rm -rf adb
sudo ln -s /home/huangph/software/android-sdk-linux/platform-tools/adb

This can solve the problem that adb can also be used under sudo. If the same situation occurs with fastboot, it can be solved in the same way!

Fourth, install the ADT (Android Development Tools) plug-in

1. Download ADT

Open Eclipse, select  Help  >  Install New Software... .

点击 Add,在 Name 输入 “ADT Plugin” 作为名字,在 Location 输入 “https://dl-ssl.google.com/android/eclipse/”(不要引号),如图:

添加好插件地址后,在 Work with 中选择刚才添加的插件地址,然后等待一会儿下面就会出现需要安装的插件。选择需要安装的插件后点击安装即可。如图:

接下来就是等待下载安装,安装后了以后重启Eclipse即安装完成。

2.配置 ADT 插件

这里可能重启Eclipse后就会弹出ADT的配置对话框,如果没有弹出的话下面会介绍。

弹出的对话框如图,只需要把前面安装好的 Android SDK 的目录填入 Location 中就可以了。

然后弹出一个问你是否愿意想Google反馈使用信息的对话框,Yes or No 随便,然后 Finish。

如果没有弹出ADT配置对话框,那么我们如下操作来配置。

打开 Eclipse ,选择 Window > Preferences… 来打开选项面板。

在左侧选择 Android ,在右侧面板中找到 SDK Location 点击 Browse… 来选择你前面安装的SDK目录,最后点击 Apply 即可。如图:

这样您的ADT就基本配置完成了。最后为了保证您的插件是最新的,可以选择 Help > Check for Updates 让Eclipse自动检测需要更新的组件来更新。

五、新建 AVD(android vitural device)

开发的时候当然需要一台设备来做测试,Android SDK 的工具中提供了 Android 虚拟设备的功能,能够在本地虚拟一台 Android 设备。在正式开发之前,我们需要配置新建一个 AVD ,当然你可以使用前面安装好的 Android SDK Manager 来新建,这里我们也可以直接在已经配置好了的Eclipse里面进行添加。

选择 Windows > AVD Manager 点击 New 来新建一台 AVD ,然后在里面配备相应的参数,如图:

点击 Create AVD 完成。

你可以在新建完成后在 AVD Manager 里面运行您刚才新建的虚拟设备,附上几幅图:

六、新建 Android 项目

打开 Eclipse , File > New > Other… 选择 Android Application Project 后,就会出现向导对话框,然后更具向导填好相关信息,最后就可以生成一个 Android 项目,如图:

填写应用名、项目名、包名等信息,还有选择构建的SDK版本。

设置应用的图标:

完成新建 Android 项目:

项目建立好后,默认给出的是一个示例,我们直接运行一下看能否运行,点击工具栏的绿色运行按钮或者键盘 Ctrl F11 。运行效果如下:

这样,我们的环境就配置完成了!

Guess you like

Origin blog.csdn.net/Vincent20111024/article/details/26165177