这里给出以命令行方式创建Android应用的步骤, 所使用的android SDK为Build Android Platform一文中 用make sdk命令构建出来的SDK。
注意:android SDK的主页已经转移到http://developer.android.com
首先需要安装Apache Ant。
在Apache Ant的主页上http://ant.apache.org/ 下载Ant到~/android/downloads/apache-ant-1.7.1-bin.tar.gz
然后将其解压缩到/usr/local/apache-ant-1.7.1
在~/.bashrc中增添以下脚本:
export ANT_HOME=/usr/local/apache-ant-1.7.1 export PATH=${PATH}:${ANT_HOME}/bin
根据 http://code.google.com/android/reference/othertools.html 中所述, 在执行make sdk命令后(可参考Build SDK), 似乎可以用新build出来的SDK的tools下边activitycreator脚本来 创建新的应用。
cd ~/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/ ./activitycreator --out HelloWorld com.android.hello.HelloWorld
但是发现报出如下错误:
ERROR: Target platform templates directory does not exist.
原因在~/android/src/development/tools/activitycreator/src/com/android/activitycreator/Activity Creator.java 中默认模版路径为:
// FIXME: target platform must be provided by the user private final String mTargetPlatform = "android-1.1";
而实际上在SDK目录下的platforms中只有android-1.5目录:
/home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/platforms: 总计 12 drwxrwx--- 3 justin justin 4096 02-15 19:09 . drwxrwx--- 6 justin justin 4096 02-13 17:06 .. drwxrwx--- 7 justin justin 4096 02-13 17:06 android-1.5
因此需要修改mTargetPlatform变量并重新编译SDK ,或者做一个符号链结:
/home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/platforms: 总计 12 drwxrwx--- 3 justin justin 4096 02-15 19:09 . drwxrwx--- 6 justin justin 4096 02-13 17:06 .. lrwxrwxrwx 1 justin justin 12 02-15 19:09 android-1.1 -> android-1.5/ drwxrwx--- 7 justin justin 4096 02-13 17:06 android-1.5
在次运行脚本,在创建好目录结构后又发生错误:
Package: com.android.hello Output directory: HelloAndroid Tests directory: HelloAndroid/tests Activity name: HelloAndroid ActivityTest name: HelloAndroidTest Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/src/ com/android/hello Added file HelloAndroid/src/com/android/hello/HelloAndroid.java Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/bin Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/libs Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/res Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/test s/src/com/android/hello Added file HelloAndroid/tests/src/com/android/hello/HelloAndroidTest.java Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/test s/bin Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/test s/libs Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/test s/res Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/res/ values Added file HelloAndroid/res/values/strings.xml Created directory /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/res/ layout Added file HelloAndroid/res/layout/main.xml Added file HelloAndroid/AndroidManifest.xml Added file HelloAndroid/build.xml ERROR: Could not access HelloAndroid/default.properties: /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.k wgj48_linux-x86/tools/lib/default.properties.template (No such file or directory)
此时在~/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/lib目录下 新建空白属性文件default.properties.template即可。
对新建的目录进行编译,运行:
cd HelloWorld ant
发现无法构建失败:
Buildfile: build.xml BUILD FAILED /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/build.xml:46: taskdef class com.android.ant.AndroidInitTask cannot be found Total time: 2 seconds
原来没有找到SDK中的类路径,通过检察build.xml中的属性文件发现:
<?xml version="1.0" ?> <project name="HelloAndroid" default="help"> <!-- The local.properties file is created and updated by the 'android' tool. It contain the path to the SDK. It should *NOT* be checked in in Version Control Systems. --> <property file="local.properties"/> <!-- The build.properties file can be created by you and is never touched by the 'android' tool. This is the place to change some of the default property values used by the Ant rules. Here are some properties you may want to change/update: application-package the name of your application package as defined in the manifest. Used by the 'uninstall' rule. source-folder the name of the source folder. Default is 'src'. out-folder the name of the output folder. Default is 'bin'. Properties related to the SDK location or the project target should be updated using the 'android' tool with the 'update' action. This file is an integral part of the build system for your application and should be checked in in Version Control Systems. --> <property file="build.properties"/> <!-- The default.properties file is created and updated by the 'android' tool, as well as ADT. This file is an integral part of the build system for your application and should be checked in in Version Control Systems. --> <property file="default.properties"/> <!-- Custom Android task to deal with the project target, and import the proper rules. This requires ant 1.6.0 or above. --> <path id="android.antlibs"> <pathelement path="${sdk-location}/tools/lib/anttasks.jar" /> <pathelement path="${sdk-location}/tools/lib/sdklib.jar" /> <pathelement path="${sdk-location}/tools/lib/androidprefs.jar" /> </path> <taskdef name="androidinit" classname="com.android.ant.AndroidInitTask" classpathref="android.antlibs"/> <!-- Class the Android Init task that will import the proper rule file containing all the Ant targets --> <androidinit /> </project>
需要在刚才新建的default.properties.template文件中增加SDK的路径:
sdk-location=/home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86
重新运行activitycreator脚本,将模版拷贝至项目目录:
./activitycreator --out HelloWorld com.android.hello.HelloWorld
然后重新运行ant,构建失败,发现没有指定目标平台:
Buildfile: build.xml BUILD FAILED /home/justin/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/HelloAndroid/build.xml:50: Android Target is not set. Total time: 4 seconds
需要在default.properties.template中增加:
target=android-3
再次运行activitycreator更新项目目录,运行ant脚本,构建通过:
Buildfile: build.xml [androidinit] Project Target: Android 1.5 [androidinit] Platform Version: 1.5 [androidinit] API level: 3 help: [echo] Android Ant Build. Available targets: [echo] help: Displays this help. [echo] debug: Builds the application and sign it with a debug key. [echo] release: Builds the application. The generated apk file must be [echo] signed before it is published. [echo] install: Installs the debug package onto a running emulator or [echo] device. This can only be used if the application has [echo] not yet been installed. [echo] reinstall: Installs the debug package on a running emulator or [echo] device that already has the application. [echo] The signatures must match. [echo] uninstall: uninstall the application from a running emulator or [echo] device. BUILD SUCCESSFUL Total time: 1 second
可以发现,这里显示了一些在开发过程中用到构件目标。
通过阅读刚才生成的build.xml构件文件,发现SDK tools中的android脚本 可能方便生成项目目录结构。
以下是它的帮助信息:
Usage: android [global options] action [action options] Global options: -v --verbose Verbose mode (false) -h --help This help (false) Valid actions: - list: Lists existing targets or VMs. - vm: Creates a new VM. - project: Creates a new project using a template. - update: Updates a new project from existing source (must have an AndroidManifest.xml). Action "project": Creates a new project using a template. Options: -n --name Name of the new project -o --out Location path of new project -m --mode Project mode (activity|alias) -t --target Target id of the new project -a --activity Activity name -p --package Package name
可以看出要创建一个新的项目,可以用android脚本的project子命令:
首先察看一下可用的目标id:
cd ~/android/src/out/host/linux-x86/sdk/android-sdk_eng.justin_linux-x86/tools/ android list
显示为:
ailable Android targets: [1] Android 1.5 API level: 3 Skins: HVGA-P, HVGA-L, QVGA-L, QVGA-P, HVGA [2] Android 1.5 API level: 3 Skins: HVGA-P, HVGA-L, QVGA-L, QVGA-P, HVGA Available Android VMs:
于是,可以用以下命令来新建一个项目目录:
mkdir HelloWorld android project --out HelloWorld --name HelloWorld --target 1 --activity HelloWorld \ --package com.android.hello