全部博文(185)
分类:
2009-06-14 10:55:46
Contents []
|
这里给出以命令行方式创建Android应用的步骤, 所使用的 SDK为一文中用make sdk命令构建出来的SDK。
注意: SDK的主页已经转移到http://developer.android.com
首先需要安装Apache Ant。
在Apache Ant的主页上 下载Ant到~//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
根据 中所述,在执行make 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.
原因在~//src/development/tools/activitycreator/src/com//activitycreator/ActivityCreator.java 中默认模版路径为:
// FIXME: target platform must be provided by the user private final String mTargetPlatform = "android-1.1";
而实际上在SDK目录下的platforms中只有-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)
此时在~//src/out/host/linux-x86/sdk/-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中的属性文件发现:
需要在刚才新建的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中的脚本可能方便生成项目目录结构。
以下是它的帮助信息:
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
可以看出要创建一个新的项目,可以用脚本的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