Android doesn’t support ant anymore. Take a look at: Building an Android project from scratch using Gradle to learn how to create a project with gradle.
Getting the environment set up
There are two packages needed for developing android applications. One is the Java Development Kit and the other is the Android SDK. You can install JDK with this command:
1
sudo apt-get install default-jdk
You can get Android SDK from this site: http://developer.android.com/sdk/index.html. The site will give you two options, to download ADT (Android Development Tookit), which is the SDK + Eclipse or just the SDK. Choose to download only the SDK.
After downloading the package you need to unzip it, open a terminal, navigate to the folder where you downloaded it, go the the tools folder and run the android script:
1
2
cd /android-sdk/tools
./android
Now you will find yourself in the Android SDK Manager. This tool allows us to download the libraries we need to start developing for Android. The official documentation recommends to begin by downloading all the Tools folder, the latest Android folder and the package Android Support Library under Extras.
For running on Fedora I also needed these libraries:
1
yum install ncurses-libs.i686 libstdc++.i686 glibc.i686
Creating an Android Virtual Device (AVD)
To be able to run our programs from our computers we need to create a virtual device that will work as an emulator to test our changes. To configure an emulator:
1
2
cd /android-sdk/tools
./android avd
Once the AVD is open you can click the New button and a window will open asking you for information about the device. Most fields are select boxes so you can create your device however you see fit. You can test your new emulator with this command:
1
./emulator -avd YourEmulatorName
Our first project
Before we can create our project we need to find out wich targets are available in our installation:
1
./android list targets
You will get back a list of android devices with numeric ids. We will need those ids when we create our project.
Now we can use the create project command. This is how it works:
1
2
3
4
5
6
./android create project \
--target <target_ID> \
--name <your_project_name> \
--path path/to/your/project \
--activity <your_activity_name> \
--package <your_package_namespace>
This is an example of how I used it:
1
./android create project --target 1 --name TestApp --path /home/myself/android/myapp --activity MyMainActivity --package com.example.testapp
The activity name is the name of your application’s main class. The package name should follow the same rules as Java packages.
Now we can go to /home/myself/android/myapp/src/com/example/testapp/ and open my main class MyMainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.example.testapp;
import android.app.Activity;
import android.os.Bundle;
public class MyMainActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Building the project
The first thing we need to do is to set the JAVA_HOME environment variable. To do this we need to execute these commands:
1
2
export JAVA_HOME=/usr
echo 'export JAVA_HOME=/usr' >> ~/.bashrc
To build the project we need ant 1.8 or later. Since my package manager doesn’t have that version I had to download it directly from http://ant.apache.org/ and compile it.
Now you can go to your project folder and build it:
1
2
cd /home/myself/android/myapp
ant debug
The ant debug command builds the application in debug mode which is good for testing but shouldn’t be used for an application that will go live. You will now see a .apk file on your bin directory.
Running your application on the emulator
Start the emulator:
1
2
cd /android-sdk/tools
./emulator -avd YourEmulatorName
Install your app in the running emulator:
1
2
cd /android-sdk/platform-tools
./adb -s emulator-5554 install /home/myself/android/myapp/bin/MyTestApp-debug.apk
Where 5554 is your emulator id number. You can find this number on the title bar of your emulator window. When the installation completes you should see your application on the application launcher. If you don’t see it try restarting the emulator.
The application is only a string in the screen saying “Hello world”. I will go over the code in another article.
Running your application on your android device
Testing the application in an actual android device is even easier. Connect your device to your computer via USB and run this command:
1
2
cd /android-sdk/platform-tools
./adb install /home/myself/android/myapp/bin/MyTestApp-debug.apk
Now you will see your application on your applications list.
Building an existing project without eclipse
Now that I have all the tools to start developing for android I felt like checking what is available on Github to see some samples of programs. I found that most people are using eclipse (or something similar) for developing, but luckily this doesn’t mean that we need to have eclipse to compile and run the app. I downloaded the code as with any other project and used this command to create my ant build file:
1
2
cd /android-sdk/tools
./android update project --name someprojectname --target 1 --path /home/myself/android/somegithubapp/
This command generates the build.xml file necessary to build with ant. Once we have this file we can build it and run it following the steps I already explained.
android
java
mobile
]