本教程是以sdk和开发文档为基础的,IDE的话推荐Idea Intellij。建议新手先用sdk和自己喜欢的文本编辑器如VIM或Emacs来做一段安卓开发,等熟练了,再转到IDE上。IDE可以提供方便的代码补全和调试功能。
另外推荐开发用的系统为Mac OSX或者Linux,应用级开发其实Mac更为合适,安卓系统级开发就推荐用Linux,比如Ubuntu。
            export ANDROID_SDK_ROOT="/Users/bear/Dev/android-sdk-mac_x86"
            PATH="$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$PATH"
            现在最新的sdk是单独分了一个sdk manager出来,从官网下载后,解压缩后找到tools目录,执行./android就会出来带界面的sdk manager,勾选对应的包来下载
项目代码在http://code.dapps.douban.com/bear/RockWithAndroid/tree/master/hello
    android create project -n Hello -t android-16 -p hello -k com.hello -a HelloActivity
    Created project directory: hello
    Created directory /Users/bear/Work/hello/src/com/hello //包名是com.hello
    Added file hello/src/com/hello/HelloActivity.java //入口页面
    Created directory /Users/bear/Work/hello/res //资源目录
    Created directory /Users/bear/Work/hello/bin
    Created directory /Users/bear/Work/hello/libs
    Created directory /Users/bear/Work/hello/res/values 
    Added file hello/res/values/strings.xml //文案,国际化
    Created directory /Users/bear/Work/hello/res/layout //布局文件夹
    Added file hello/res/layout/main.xml
    //不同dpi的图片资源文件
    Created directory /Users/bear/Work/hello/res/drawable-xhdpi
    Created directory /Users/bear/Work/hello/res/drawable-hdpi
    Created directory /Users/bear/Work/hello/res/drawable-mdpi
    Created directory /Users/bear/Work/hello/res/drawable-ldpi
    Added file hello/AndroidManifest.xml //manifest文件
    Added file hello/build.xml //makefile
    Added file hello/proguard-project.txt
                
    
    //如果已有项目代码,可以用下面的语句来生成makefile
    android update project -n Hello -t android-16 -p hello -k com.hello -a HelloActivity
    //生成的local.properties里面有sdk的路径,比如hello里我的是
    sdk.dir=/Users/bear/Dev/android-sdk-mac_x86
            
            cd hello && ant debug //编译打包
            //adb具体可以看Android Debug Bridge
            adb uninstall com.hello
            adb install -r bin/Hello-debug.apk //安装
            adb shell am start -n com.hello/com.hello.HelloActivity //启动
            
            如果多台设备(含模拟器)的话,可以用下面的脚本来批量做:
            
            PHONES=`adb devices |grep "device$"|awk '{print $1}'`
            for p in $PHONES
            do
                xxxx
            done
            
            一般的安卓应用通过看log就能完成调试工作,而真的需要调试器的话,还是使用IDE吧。
            //看所有log
            adb logcat
            //看某个TAG的所有level的log
            adb logcat *:S TAG:*
            
            //根据提示输入相应信息
            keytool -genkey -alias hello.keystore \
                -keyalg RSA -validity 200000 -keystore hello.keystore
            //配置ant.properties,加入下面两行
            key.store=hello.keystore
            key.alias=hello.keystore
            
            
            ant release
            //看到提醒,输入刚才的密码,就能打出正式签名的包了,可以发布到Google Play和其他market了。
            -release-prompt-for-password:
                [input] Please enter keystore password (store:hello.keystore):
            xxxxxxx
                [input] Please enter password for alias 'hello.keystore':
            xxxxxxx
            ...
            Release Package: /Users/bear/Work/hello/bin/Hello-release.apk
            
            apk其实就是一个zip包,你可以改名后直接解开。
如果想了解更多反编译的,有下面的资料可以参考:apktool,dex2jar,JD-GUI。
            
            zipalign -v -f 4 Hello-release.apk Hello-release-align.apk
            
            
            //linux下面截屏并保存到电脑上
            adb shell screencap -p | sed 's/\r$//' > screen.png
            //Mac OSX下面截屏并保存到电脑上
            adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > screen.png
            
            ant是本地编译,但是如果不用IDE的增量编译太慢;而maven的构建,需要依赖maven的包服务器,如果不是自己搭建了Sonatype Nexus的话也不是太方便。Google推荐的新的构建方式是使用Gradle。现在的缺点是有一些从apk lib转到aac的包还没有,需要自己做。有兴趣了解的同学可以看RockWithAndroid/hello/build.gradle。因为还比较新,某熊也了解有限。
总结下,本章我们建了一个Hello World,而且能够发布到Google Play上,但是真正的应用代码并没有写。下一章,我们会了解安卓开发框架的主要组成部分。