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

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.

Wednesday, April 1, 2015

How to learn programming

There is a huge 'learn coding' wave nowadays. Coding is, as we know, common parlance for computer programming. Many people are prophesying that coding will soon be the new literacy. I don't totally agree with this with stand, but I do realize that programming a machine is a fundamentally new invention of mankind (like the invention of writing) which will lead us into different ways of thinking (like writing did).
What I don't really agree with is the numerous 'teach coding' programs around.

I'd like to give my views on programming and how to learn it, from my own experience of more than 10 years (college+industry). The first and most important thing to know is that programming cannot be taught. It can be learned, by doing it. Yes, you will get stuck every day while you are programming and then you will have to refer to some guides, or consult an expert, or listen to a class. But your guide or your teacher will only give you inspiration, direction, and clarity. And also what he/she thinks.
But programming is like human thought -- it is continuously evolving. When you are stuck at a problem, you might come up with a different way of programming which no one has done yet -- and this has been happening regularly for the last few decades.

As I see it, the way you advance in programming is:
1. get inspired and get some direction, by talking to someone, or listening to a good classroom lecture, or whatever works for you
2. get started and get stuck as soon as possible -- getting stuck is crucial to learning programming (it is crucial to any learning)
3. go back to the references, talks, scratch your head, and come back to step 2.
This is why I like programming so much -- it is such a very practical art which one cannot learn theoretically.