Wednesday, March 19, 2014

Settings up an Android Application Project Developed on One PC on Another – For Beginner

Recently I wanted to add couple of new features to an Android application which was developed some time ago. I’m new to the Android development and here I’m listing down all the issues that I had to face and how I resolved them, in my attempt to set up the project in my development environment.

First I wanted to open up half way developed Android project, which was developed using Android Developer Tools (ADT). There was no obvious option like ‘open project’ in my ADT menu. Instead I had to use one of the options below which will open up Import Projects window.

With either approach, we should not copy our project into the workspace. Copy it elsewhere and let the Eclipse copy it into the workspace by selecting ‘Copy projects into workspace’ checkbox in the Import Projects window.

1. File -> New -> Project... -> Android Project from Existing Code
2. File -> Import -> Android -> Existing Android Code Into Workspace


Then my project was loaded in ADT Package Explorer. But I could see couple of errors in the Console saying:
Unable to resolve target 'android-17' Android Target Problem

Reason turned out to be that the SDK version using by me is different from the original version used in the project. I could found the SDK version set to 17 in application manifest file (AndroidManifest.xml), which resides in the project folder inside the workspace folder.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shighway"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />


So I tried to fix the issue by manually updating the SDK version to 18, which my environment was using. (We can find the Android version and related API number in Developer Dashboards.)

Then the Target resolving problem was gone and I got a new error:
File is Out of sync

Then I figured out that there are actually two AndroidManifest.xml files. Second one was in the ‘bin’ folder. Updating that file and setting SDK version to 18 in that AndroidManifest.xml cleared out all the errors.

Note: Changes to AndroidManifest.xml file took effect only after I close and open the ADT.

Build an Android project in ADT
In order to be sure, I was wondering whether there is an option in ADT to rebuild the project. Then I learnt that there is no menu option to do that. But if we are developing in Eclipse, the ADT plugin incrementally builds the project as we make changes to the source code. Also it outputs an .apk file automatically to the bin folder of the project.

Now I’m sure my project is building and it is targeting the 4.3 Jelly Bean version. But how about the support for pervious Android versions? There is a setting for that in the AndroidManifest.xml file. Setting is android:minSdkVersion, which specifies the minimum API Level required  for the application to run. I specified 8 which means 2.2 Froyo.

Then I used Run -> Run from ADT menu to run my application in an emulator. Yes, it was running, but emulator took while to load in the first run and it was not that fast in later runs as well.

Next task was to run the app in my Android device, which is running on 4.3 Jelly Bean. So I plugged my device and wanted to enable “USB debugging” in my phone from the Developer options. But developer options were not there in the Settings as Developer options is hidden by default on 4.3 and I had to enable it by:

Settings -> More -> About device -> tap "Build number" 7 times

Now USB debugging is enabled and when I run my application it was still running on the emulator.

I had to go to the Run -> Run Configurations... -> Target from ADT menu to set to prompt always to pick a device when running.


After that when I invoked Run form the menu I got Android Device Chooser window to select the device.


Even though the window pops out, my device was not listed in there. I had to update device driver software. In Windows 7, I did the following:
  • Go to Computer Management (Right click Computer -> Manage).
  • Under Device Manager, expand Portable Devices.
  • Select the phone and update driver software (Phone needs to be connected).
  • I let it search in the Internet and updated the driver.

With the updated driver, when I run my application I could see a device was listed, but it didn’t let me select it. Then I noticed that phone State is offline and Target is unknown for the listed device.


Therefore I disabled and re-enabled USB debugging on the phone and it fixed the issue. Later I learned that rebooting the phone is required in some cases.

Also there was a popup coming in the phone asking ‘Allow USB Debugging?’ with the device id. Accepting that listed the phone in Android Device Manager and it was available for selection.


And it deployed the application on my phone.

No comments: