Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a "Hello, World" Application from the Command Line

Author: Ian Darwin
Published? true -- FormatLanguage: W

Problem:

You want to create a new Android project without using the Eclipse ADT plug-in.

Solution:

Use the Android Development Kit tool android with the create project argument and some additional arguments to configure your project.

Discussion:

In addition to being the name of the platform, Android is also the name of a command-line tool for creating, updating and managing projects. You can either navigate into the android-sdk-xxx directory, or you can set your PATH variable to include its tools subdirectory.

Then, to create a new project, give the command "android create project" with some arguments. Here is an example run under MS-DOS:

C:\Documents and Settings\Ian\My Documents>PATH=%PATH%;"C:\Documents and Settings\Ian\My Documents\android-sdk-windows\tools"
C:\Documents and Settings\Ian\My Documents>android create project --target 1 --package com.example.foo --name Foo --activity HelloWorld --path .\MyAndroid
Created project directory: C:\Documents and Settings\Ian\My Documents\MyAndroid
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\src\com\example\foo
Added file C:\Documents and Settings\Ian\My Documents\MyAndroid\src\com\example\foo\FooActivity.java
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\res
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\bin
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\libs
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\res\values
Added file C:\Documents and Settings\Ian\My Documents\MyAndroid\res\values\strings.xml
Created directory C:\Documents and Settings\Ian\My Documents\MyAndroid\res\layout
Added file C:\Documents and Settings\Ian\My Documents\MyAndroid\res\layout\main.xml
Added file C:\Documents and Settings\Ian\My Documents\MyAndroid\AndroidManifest.xml
Added file C:\Documents and Settings\Ian\My Documents\MyAndroid\build.xml

C:\Documents and Settings\Ian\My Documents>

The list of arguments for the create project code follows:

List of Create Project Arguments
Name Meaning Example
--activity Name of your "main class" and default name for the generated .apk file --target HelloActivity
--name Name of the project and the generated .apk file --name MyProject
--package Name of Java package for your classes --package com.example.hello
--path Path to create the project in (does not create a subdirectory under this) --path /home/ian/workspace/MyProject (see above for Windows example)
--target Level of the Android platform to target --target 1

If it cannot complete the requested operation the android command presents a voluminous "command usage" message listing all the operations it can do and the arguments for them. If successful, the android create project command creates the following files and directories

Artifacts Created by Create Project
Name Meaning
AndroidManifest.xml Config file that tells Android about your project
bin generated binaries (compiled class files)
build.properties Editable properties file
build.xml Standard Ant build control file
default.properties
gen Generated stuff
libs Libraries, of course
res important resource files (strings.xml, layouts, etc.)
src source code for your application
src/packagename/ActivytName.java source of "main" starting activity
test copies of most of the above

It is normal and recommended Android practice to create your user interface in XML using the layout file created under res/layout, but it is certainly possible to write all the code in Java. To keep this example self-contained, we'll do it the "wrong" way for now. Use your favorite text editor to replace the contents of the file HelloWorld.java with the following contents:

public class Hello extends Activity {

        /**
         * This method gets invoked when the activity is instantiated in
         * response to e.g., you clicked on the app's Icon in the Home Screen.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                // Create a TextView for the current Activity
                TextView view = new TextView(this);
                // Make it say something
                view.setText("Hello World");
                // Put this newly-created view into the Activity
                // sort of like JFrame.setContentPane()
                setContentView(view);
        }
}

Assuming you have the Apache Software Foundation Ant Build Tool installed, you can now (in a command-line window) change directory into the project directory (...MyDocuments\MyAndroid in the above example) and issue the command:

ant debug

This will create an archive file named e.g., MyAndroid.apk (apk standing for Android Package) in the bin directory.

Assuming you have either the Emulator running, or your device plugged in and recognized via USB, you can then do

adb -e install -r bin/MyAndroid.apk

The -e flag is for the emulator; use -d for a real device.

If you are handy with shell scripts or batch files, you'll want to create one called, say, download, to avoid typing the adb invocation on every build cycle.

You will probably find it most convenient to create an icon for your app on the home screen of the device or emulator; this icon will survive multiple "install -r" cycles so it's the easiest way to test running your application.

See Also:

Hello World - Eclipse Version. The blog "a little madness" has a more detailed formulation. The official Android reference site has a page on developing without Eclipse.

tmoriarty 2011-10-12 16:51:13.867 Hi , I am new to this site, I just spotted a small typo here: "It it cannot complete" , I'm sure should be "If it cannot complete" . regards Tim