Ubuntu 14.04 设置Android开发环境

准备Java环境

本文仅仅安装sdk,不安装什么IDE,由于我仅仅须要命令行模式开发就可以。

首先安装openjdk 1.6。然后安装ant。这个不赘述。

下载SDK

从这里下载SDK for Linux 64bit的版本号: http://developer.android.com/sdk/index.html?hl=sk#download

wget http://dl.google.com/android/android-sdk_r23.0.2-linux.tgz

下载后,解压到本地文件夹,比方/opt/文件夹下, 然后环境变量设置,加入三行到~/.bashrc文件

export ANDROID_HOME=/opt/android-sdk-linux
PATH=$PATH:/opt/android-sdk-linux/tools
PATH=$PATH:/opt/android-sdk-linux/platform-tools


执行Android SDK Manager

执行android命令。将会弹出SDK Manager对话框,从中选择须要的package进行安装


安装模拟设备

android avd
详细參考官方文档:http://developer.android.com/training/basics/firstapp/running-app.html

我创建了一个模拟设备。  最后执行该模拟设备。








创建工程

显示targets

$ android list targets
Available Android targets:
----------
id: 1 or "android-10"
Name: Android 2.3.3
Type: Platform
API level: 10
Revision: 2
Skins: WQVGA400, HVGA, WVGA854, QVGA, WQVGA432, WVGA800 (default)
Tag/ABIs : default/armeabi
----------
id: 2 or "android-19"
Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 3
     Skins: WQVGA400, HVGA, WXGA800-7in, WXGA800, WVGA854, QVGA, WQVGA432, WVGA800 (default), WSVGA, WXGA720
 Tag/ABIs : default/armeabi-v7a
----------
id: 3 or "Google Inc.:Google APIs:10"
     Name: Google APIs
     Type: Add-On
     Vendor: Google Inc.
     Revision: 2
     Description: Android + Google APIs
     Based on Android 2.3.3 (API level 10)
     Libraries:
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: WVGA854, WVGA800 (default), WQVGA400, QVGA, WQVGA432, HVGA
 Tag/ABIs : default/armeabi
----------
id: 4 or "Google Inc.:Google APIs x86:19"
     Name: Google APIs x86
     Type: Add-On
     Vendor: Google Inc.
     Revision: 4
     Description: Android + Google APIs x86
     Based on Android 4.4.2 (API level 19)
     Libraries:
      * com.google.android.media.effects (effects.jar)
          Collection of video effects
      * com.android.future.usb.accessory (usb.jar)
          API for USB Accessories
      * com.google.android.maps (maps.jar)
          API for Google Maps
     Skins: WVGA800 (default), QVGA, WSVGA, WXGA800-7in, WQVGA400, WVGA854, WXGA720, WXGA800, WQVGA432, HVGA
 Tag/ABIs : default/x86


创建工程

$ android create project --target android-10 --name MyFirstApp \
--path MyFirstApp --activity MainActivity --package com.example.myfirstapp
> Created project directory: MyFirstApp
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/src/com/example/myfirstapp
Added file MyFirstApp/src/com/example/myfirstapp/MainActivity.java
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/res
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/bin
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/libs
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/res/values
Added file MyFirstApp/res/values/strings.xml
Created directory /home/dean/work/gitlab_cloud/android/example/MyFirstApp/res/layout
Added file MyFirstApp/res/layout/main.xml
Added file MyFirstApp/AndroidManifest.xml
Added file MyFirstApp/build.xml
Added file MyFirstApp/proguard-project.txt
dean@dean-Aspire-V7-481G:~/work/gitlab_cloud/android/example$ ls
MyFirstApp  README
dean@dean-Aspire-V7-481G:~/work/gitlab_cloud/android/example$ cd MyFirstApp/
dean@dean-Aspire-V7-481G:~/work/gitlab_cloud/android/example/MyFirstApp$ ls
AndroidManifest.xml  ant.properties  bin  build.xml  libs  local.properties  proguard-project.txt  project.properties  res  src

编译

执行以下的命令编译

ant debug


假设报以下错误:

     [aapt] /opt/android-sdk-linux/build-tools/19.0.3/aapt: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

安装库:

sudo apt-get install lib32z1


部署程序到模拟器

$ adb install bin/MyFirstApp-debug.apk
97 KB/s (4858 bytes in 0.048s)
	pkg: /data/local/tmp/MyFirstApp-debug.apk
Success


执行程序

好了,如今能够在模拟器里面执行程序了。 执行程序MainActivity。显示以下的文字:

Hello World, MainActivity

该文字事实上是定义在./res/layout/main.xml 文件里:

<?xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello World, MainActivity" /> </LinearLayout>



定义应用程序名称

假设想要改动Android中的应用程序名,不是在pom.xml中编辑,而是在src/values/strings.xml中定义,比方:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">mvn_example1</string>
    <string name="edit_message">Enter a message</string>
    <string name="button_send">Send</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
</resources>





posted on 2017-08-13 12:18  blfbuaa  阅读(165)  评论(0编辑  收藏  举报