Compiling and Running your first Android application in Windows 7: Part 2: Import, modify, and compile
In Part 1, we obtained the complete source code for our first project. The source code should be at the following folder in the hard disk:
Part 2: Import and Compile using Eclipse
The source code distributed with the Simple Loan Calculator is an Android Project that still needs to be imported into an Eclipse workspace.
Although importing might indicate that a copy is made, we can choose to work with the existing folder and prevent having duplicate code.
With existing projects, it is a good idea to store workspaces in a folder that is completely separate from the project source code, for example:
Create a new workspace
- Start Eclipse and create a new workspace exclusively for the Simple Loan Calculator
Starting a new workspace means creating a new development environment. Watch out for any changes to configuration as you may have to do them all over again when starting a new workspace.
Every new workspace created begins with a welcome pane that contains information about the features and updates for the Eclipse version you are using.
- Close the welcome pane by clicking on the ‘x’ to the right of the tab. This will take you to a blank workspace that is set to show the Java perspective.
Tip: If you need to return to the Welcome screen, select Help ->Welcome from the main menu
Knowing your perspective at any time is key
Eclipse has several ‘modes’ that have different purposes. Each mode is called a perspective.
In this tutorial we need to be aware of two of them:
The Java Perspective: This is the default perspective. It contains the components that you need to navigate through source code, design and APIs in use.
The Debug Perspective: This is activated while the application is executing. The layout is quite different as it will describe what is happening to your logic while it executes.
At any point, whether designing or executing, you can switch perspectives by selecting Windows -> Open Perspective from the main menu
or use the upper right (perspective) toolbar to switch between them
Disable unneeded views.
Each perspective contains sections called views. For the purpose of this tutorial, we will not be needing the following views: ‘Connect Mylyn’, ‘Task List’, and ‘outline’ views.
- Simply click on the X to the right of each view title to close.
Important: Turn off automatic building of code
This is an Eclipse feature that is not well suited in cases where you need to understand what is going on, so in your initial projects it is important to remember turn it off using ‘Project-> Build Automatically’
Import the Simple Loan Calculator Code.
Our workspace is empty at the moment. Now it is time to import the Simple Loan Calculator code we downloaded in the previous step.
- Select File->Import from the main menu
- Browse to the Android->Existing Android code into Workspace and then click ‘Next’
This will open the Import Projects dialog box.
- Set the ‘Root Directory’ text box to
and then click ‘Browse’, followed by ‘OK’.
If there is a valid Android project to import, this will be displayed as something like:
in the ‘Projects’ list in the Import Activity dialog box.
This is a naming convention inherited from Java development practices. The initial sections such as ee.smkv in this case will usually mean the domain for the organization that has produced the project. The next sections, such as calc.loan are the application name and the last part, MainActivity, confirms that the project has the expected Android entry point.
- Select ‘Finish’ to initiate the import process. Make sure to leave the ‘Copy projects into working space’ checkbox unchecked to avoid duplicate code.
Starting the import process initiates several activities behind the scenes such as confirming dependencies. You need to keep watching the lower right part of the screen where you will notice a status
Ideally you need to wait until this is finished.
If the project finished importing correctly, you can now click on the imported MainActivity package and view a listing of source code (organized by class level) and resources
Clicking each class in turn provides a detailed look into their implementation
In the example above, the ee.smkv.loan.calculators package contains both the definition of the AbstractCalculator class and implementations of different loan types. The AnnuityCalculator, for example, implements the calculate method when a variable of type Loan is provided.
After this brief overview of the package explorer, it is time for the first compilation of the project.
We need to set our minimum and our target Android API Level. We obtained the API level when installing it from the Android SDK Manager.
Important: For this demonstration, the API level in the device is 10 (Equivalent to Android 2.3.x).
- Double-click on ‘AndroidManifest.xml’ at the end of the project. This will activate the ‘Android Manifest’ view. This is a way to edit project settings such as the internal version name, permissions requested when running, etc.
- Then click on the U- Uses Sdk entry at the ‘Manifest Extras’ section and you will notice that the attributes for ‘Uses Sdk’ will be displayed.
The Min SDK version was set by the original developer of the application. The developer must have had API 3 installed at the SDK Manager. One of the reasons for using a Min SDK verion of 3 is to allow the application to be installed in the widest number of devices possible. We will discuss this in a later tutorial.
For now you may recall that we only have the API SDK of our device set up so it is likely that API 3 will not be available and this will cause a run-time error.
- Simply change the ‘Min SDK version’ and ‘Target SDK version’ to the one that matches your device version.
- Any change here is not yet saved and you will know this by looking at the asterisk at the left of the title of the view
Save the change in Sdk minimum and target versions by selecting File -> Save from Eclipse
- After the configuration in the manifest is done, select Project -> Build project
- Wait until the lower right status is done.
If everything is fine, no errors should have been detected.
In summary, as of this moment:
- A series of compiled objects have been created (binaries) and created in the bin folder
- A new package using the manifest provided has been created and is ready for deployment.
- R.java and BuildConfig.java have been generated in the \gen folder
So far, having our device plugged in has not been a requirement. In the third and final part of this first tutorial, we will perform a modification to the original source and will finally deploy to our device.
Sign to our RSS feed for updates