Use command line to develop Android applications in Linux

Source: Internet
Author: User
Tags android sdk manager

This article describes how to use the command line to develop Android applications on the Linux platform and how to create a phonegap Android project. If you are interested in this, continue to read this article.

First, let's install the android SDK (I suppose you have installed JDK and ant ). You can install the SDK on your development platform. Because I use Ubuntu, I download the SDK on Linux to my home directory. Decompress the downloaded file and set the path to load the executable file:

1 $ tarxvf android-sdk_r16-linux.tgz
2 $ exportPATH=$PATH:~/android-sdk-linux/tools:~/reqs/android-sdk-linux/platform-tools
3 $ android sdk

If you do not want to set path every time, you can set your~/. BashrcOr~/. Bash_profileFile.

Then, run the android SDK command to open the android SDK manager. Download the necessary SDK version package. I plan to develop the ginger pie and ice cream sandwich system mobile app, So download the android 4.0.3 (API 15) and Android 2.3.3 (API 10) components.

Let's check whether the SDK version package is installed:

1 $ android list targets

When developing an application, we need to create an android Virtual Machine (AVD) to test the application:

1 <span style="font-size:14px;">$ android avd</span>

You can download and create multiple avds in the previous step.

To view the AVD list, run the following command:

1 $ android list avd

Assume that we want to run"Gingerbread"Simulator, use the following command:

1 $ emulator @Gingerbread

Now we have set up the environment for developing Android applications.

Create an android Project

Let's create an android Project (to create a phonegap Android project, read the next section ):

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

-N: Project name (Testandroidproj);
-T: Android SDK version (Android-15 );
-P: Path of the android project;
-K: Java package name;
-A: The initial activity.

After running the preceding command, an android project is created.
Check the Directory and files:

1 $ cdandroid_proj/
2 $ lssrc/com/example/TestProjActivity.java
3 src/com/example/TestProjActivity.java

Install and create a phonegap Application

If you do not want to create an HTML5 application or do not want to use phonegap, you can skip this section.

After installing the android SDK, obtain Cordova (formerly phonegap) (or obtain a stable version from GitHub) and add the bin directory to yourPath:

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

Let's create a Cordova project. This is to shell the android project and we need to set the dependency of phonegap (jar and JS files ):

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

The first parameter is the path of the project;
The second is the package name (Java );
The third is the identifier activityname, which is used to load index.html;
The fourth parameter is the android sdk id (for example, 'android 1.5 'has an ID of '3 ').

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

1 $ cd~/phonegap_android_proj/
2 $ lsassets/www/
3 cordova-1.5.0.js  index.html  main.js  master.css
4 $ lslibs/
5 cordova-1.5.0.jar
6 $ lsres/xml/
7 cordova.xml  plugins.xml

Ant command

Suppose that we have completed coding for this project, and we need to test it on the simulator. Therefore, first start the simulator and then start the android debugging bridge (ADB ). It is recommended that you use a Super User to start the service, because if you connect to the service on your mobile phone later, it requires necessary permissions to access the service:

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

Check the running status of the ADB service process:

1 $ psaux|grep
adb
2 root     12589  0.0  0.0  20184   948 pts/3    Sl   16:43   0:00 adb fork-server server

Deploy the application to the simulator

Run the ant command in the root directory of the project to deploy the application to the simulator (use the debug key for signature ):

1 $ cd~/android_proj
2 $ ant clean debug install

When the application is displayed on the simulator, you can test it. If you want to test with a mobile phone, you can also use the same command above.

You can use the virtual debug monitoring server (ddms) to debug and view log information:

1 $ ddms

At this point, as long as you are satisfied with the developed application, you can sign it and publish it to the Google market.

Signature Application released to market

First, let's generate a private key for signature:

1 $ keytool -genkey -v-keystore app_signing.keystore -aliasrelease \
2 -keyalg RSA -keysize 2048 -validity 10000

JDK's keytool is used to create a private key.
-Keystore: name of the output private key file;
-Alias: Private Key alias. Multiple keys can be stored in the keystore for later use;
Set the RSA encryption algorithm. The key length is 2048 bits and the validity period is 10000 days.
Make sure that the generated keystore file is very secure, because this is the unique identifier of the Google market for you.

After the private key is successfully generated, we compile the application into the publishing mode:

1 $ ant clean release

You will find that there is an APK file in the bin directory of your project:<Project_name> -release-unsigned.apk('Testandroidproj-release-unsigned.apk 'in our example '). We have not signed the application for the previously created private key:

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

Use the jarsigner tool for signature. The keystore is previously created and the signature algorithm is specified. The Android Application uses the keystore alias.

Let's verify:

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

You should get a 'jar verified 'message.

Finally, let's submit the generated APK file to the Google market.

1 $ zipalign -v4 bin/TestAndroidProj-release-unsigned.apk bin/TestAndroidProj.apk

'4' indicates that the APK file uses 4 bytes. The following parameter is the signed application file. The last parameter is the file uploaded to the Google market.

The only frequently used command here is ant clean debug install, which can avoid insufficient memory during eclipse runtime.

Note:: If you are an Emacs user, you can use Android-mode, which includes the preceding commands and other good things.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.