Saturday, April 4, 2015

A command-line user's guide to building Android apps

I'd like to document my bare-bones Android app development work flow which is entirely command-line-based. The internet is full of app development guides using Eclipse or other IDE's, but resources for command-line users like myself are rare. I don't know why this is so, because the command-line is heavily used by programmers in most other domains I know.

Creating a new project

cd <android-sdk-dir>/tools
android create project --target <target-id> --name <name-of-project> \
--path <path-where-you-want-to-keep-your-project> \
--activity <first-activity-name> \
--package <app-package-name> 

For target-id do
android list targets
to see the available targets and select an id from them

Example:
android create project --target 1 --name MyFirstApp \
--path ~/Apps/MyFirstApp \
--activity MainActivity \
--package com.example.myfirstapp

Making changes

I use emacs to edit the java code generated, and the layout xml file main.xml. To test any non-Android-specific piece of functionality it is a good idea to try it out in a stand-alone .java file outside the project. I have done this for trying to do a http request and it has helped.

Building the app (debug version)

cd <your-project-directory>
PATH=<android-sdk-dir>tools:$PATH ant debug

Launching the app in your emulator or phone

Start up your emulator by opening the avd manager:
<android-sdk-dir>/tools/android avd

Otherwise, plug in your phone and turn on USB debugging.

In your project directory, do:
<android-sdk-dir>/platform-tools/adb install bin/<app-name>-debug.apk
<android-sdk-dir>/platform-tools/adb logcat

Your app is installed, run it and look at the logcat output running on your command-line to debug what went wrong. I am assuming you have put enough Log.d() messages in your app code.

After you're done, you can uninstall the app by doing
<android-sdk-dir>/platform-tools/adb uninstall <package-name>

where package name is that com.example.myfirstapp or something you gave while creating the project

Note: Google's documentation has all of this information - you just have to fish it out since their main focus seems to be Eclipse/Android Studio based development.

No comments:

Post a Comment