Developing android applications from command line

来源:互联网 发布:json字符串中url编码 编辑:程序博客网 时间:2024/06/05 11:03

Here at Agiliq, we also develop cross-platform HTML5 mobile applications. Using Eclipse to create an android project (one time task) and edit html and javascript files for an android app is an overkill. Wouldn't it be great, if you could use your favourite text editor to edit html and js files and then fall back to the terminal to deploy the android app? We are going to see exactly this in the post.

First, let's install the android SDK starter package(I assume that you have installed JDK and ant). Head over here and install the SDK for your platform. Since I use ubuntu, I download the Linux platform SDK to my home directory. Uncompress the download and set the path to load the executables:

$ tar xvf android-sdk_r16-linux.tgz$ export PATH=$PATH:~/android-sdk-linux/tools:~/reqs/android-sdk-linux/platform-tools$ android sdk
Android SDK manager

If you don't wish to keep exporting the PATH variable everytime, you can set it in your ~/.bashrc or ~/.bash_profile file.

The android sdk command opens the Android SDK manager. Download the requisite SDK packages for the android versions that you plan to target. I plan to develop the app for Gingerbread and Icecream Sandwich phones and hence download the Android 4.0.3 (API 15) and Android 2.3.3 (API 10) components.

Let's check which versions of the SDK are installed from the terminal:

$ android list targets

While developing the app, we need a way to test the app and hence let's create Android Virtual Devices (AVD) that will create virtual devices that we'll run later through the emulator:

$ android avd
Android AVD manager

Create as many AVDs as the android SDK versions that you downloaded in the previous step.

To view a list of AVDs we just created on the terminal, use the following command:

$ android list avd

Let's test if the AVDs we created run on the emulator. I want to run the AVD named Gingerbread on the emulator:

$ emulator @Gingerbread
Android Emulator

We've setup the basic infrastructure for being able to develop android apps.

Creating an android project

Let's create an android project (for creating a phonegap based android project, skip to the next section):

$ android create project -n TestAndroidProj -t 'android-15' -p ~/android_proj -k com.example -a TestProjActivity

The value (TestAndroidProj) to the -n switch is the name of the project, the value (android-15) to the -t switch is the android SDK version the app targets. The value to the -p switch mentions the path of the android project. The -k switch requires a valid (java) package name and the -a switch takes the name of the initial Activity.

After running the above command, an android project should have got successfully created. Check if everything's as expected:

$ cd android_proj/$ ls src/com/example/TestProjActivity.javasrc/com/example/TestProjActivity.java

(BONUS) Installing and creating a Phonegap app

If you don't plan to create an HTML5 app or don't plan to use Phonegap, you can safely skip this section. See you later...

Immediately after installing the android SDK, clone Cordova (formerly Phonegap) from this location (or clone it from a github tag for a stable version). Add the bin directory under the cloned directory to your PATH:

$ git clone git://git.apache.org/incubator-cordova-android.git$ export PATH=$PATH:~/incubator-cordova-android/bin

Let's create a Cordova project, which is a wrapper around android create projectbut additionally sets up phonegap dependencies (the jar and the js files) for you:

$ cd ~/incubator-cordova-android$ ./bin/create ~/phonegap_android_proj com.example TestProjActivity 3

The first argument is the path of the project, the second is the (java) package name, the third is the name of the initial activity that loads your index.html and the fourth argument is the android sdk id (taken from android list targets, in my case 'android-15' id is '3').

Let's check if the project has been successfully created:

$ cd ~/phonegap_android_proj/$ ls assets/www/cordova-1.5.0.js  index.html  main.js  master.css$ ls libs/cordova-1.5.0.jar$ ls res/xml/cordova.xml  plugins.xml

The following sections are common to both native android projects and phonegap based android projects.

Ant commands

Now that we are done with the project and write all the code for the app, we need to be able to test it in the emulator. So let's start the emulator (mentioned above) and then start the Android Debug Bridge (adb). It is recommended you start adb as a superuser because if you later connect your phone, it will have the requisite permissions to access it:

$ sudo ~/android-sdk-linux/platform-tools/adb start-server

Check if the adb server process is running before you proceed further:

$ ps aux|grep adbroot     12589  0.0  0.0  20184   948 pts/3    Sl   16:43   0:00 adb fork-server server

Deploying the app to the emulator

In your project root, run the ant commands to deploy the app (signed with the debug key) to the emulator:

$ cd ~/android_proj$ ant clean debug install

The app should show up on your emulator and you can test it out. If you plan to use the phone, the same set of commands will work. You might want to close the emulator then or adb will complain that both the emulator and the device are connected. To debug and view log messages use the Dalvik Debug Monitor Server (ddms):

$ ddms

Once you are satisfied with the app, you will want to sign it and publish it to the market (Google Play).

Signing the app to deploy to marketplace

First, let's generate a private key that will be used to sign the application:

$ keytool -genkey -v -keystore app_signing.keystore -alias release \  -keyalg RSA -keysize 2048 -validity 10000

The keytool that is part of the JDK is used to create the private key. The -keystore argument's value is the name of the output file where the keys are stored. The -alias is a human readable name for the key (as multiple keys may be stored) in the keystore which can be used to refer the key later on. The encryption algorithm is set to RSA with a keysize of 2048 bits and a validity of 10000 days. Keep the generated keystore file very safe as this identifies you on the Google Play store.

After you have successfully generated your private key, let's compile the app in the release mode:

$ ant clean release

You will notice under the bin directory of your project, a file of the format<project_name>-release-unsigned.apk (in our case, 'TestAndroidProj-release-unsigned.apk'). After we are done with this, we have to sign the app with the private key we created previously:

$ jarsigner -keystore app_signing.keystore -digestalg SHA1 -sigalg MD5withRSA bin/TestAndroidProj-release-unsigned.apk release

The jarsigner utility uses the keystore (created previously) while specifying the digest algorithm, signature algorithm, the release android application and the name of the alias to be used from the keystore.

Let's verify if everything went fine:

$ jarsigner -verify bin/TestAndroidProj-release-unsigned.apk

You should get a 'jar verified' message and might want to repeat the signing process if you didn't.

Finally, let us align the generated application file (apk) before submitting to Google Play store.:

$ zipalign -v 4 bin/TestAndroidProj-release-unsigned.apk bin/TestAndroidProj.apk

'4' specifies that the files in the apk should be aligned to the 4-byte boundary. The next argument is the input signed application file and the last argument is the output file that is used to upload to the Google Play store.

The only command that we'll use frequently from the terminal is ant clean debug install and by doing so we can avoid running Eclipse (which hogs memory like there's no tomorrow).

Bonus: If you are an Emacs user, you might want to use android-mode which has key bindings for most of the above mentioned commands and other goodies

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小孩不爱学英语怎么办 孩子抵触学英语怎么办 初三了英语不好怎么办 孩子入园焦虑怎么办 家长总是说孩子怎么办 幼师打了小朋友怎么办 幼儿园老师欺负孩子怎么办 被老师冷落怎么办豆瓣 不满老师对孩子怎么办 老师总找茬孩子 怎么办 老师总针对孩子怎么办 老师看你不舒服怎么办 高中孩子不爱学习怎么办 孩子太倔不听话怎么办 老师老说孩子怎么办 孩子13不懂礼貌怎么办 老师不重视你 怎么办 我娃好动不听话怎么办 初三娃不听话该怎么办 小孩爱动不听话怎么办 宝宝吃饭讨神怎么办 孩子速度太慢怎么办 一年级做作业慢怎么办 小学三年级数学差怎么办 拼音基础太差怎么办 小孩学拼音差怎么办? 孩子字词基础差怎么办 孩子的语文不好怎么办 数学一直学不好怎么办 小孩数学成绩差怎么办 理科生语文不好怎么办 小学阅读题不好怎么办 如果孩子考不好怎么办 6岁不认识数字怎么办 数学一点都不会怎么办 初一数学太差怎么办 三年级孩子数学差怎么办 三年级孩子数学很差怎么办 初中学习不好高中怎么办 四年级孩子数学不好怎么办 孩子学习不开窍怎么办