Category Archives: Java

Install Android SDK and Tools with Eclipse in Ubuntu, Mac OS X and Windows

Here I’ll explain the process to install Android SDK and Tools. There are 4 things you have to install in order to develop Android applications:

  • Android SDK
  • Eclipse
  • Android Development Tools plugins for Eclipse (ADT)
  • Android Virtual Device (ADV)

Android SDK

You can download the latest version of the Android SDK for your OS at the official Android SDK web. Latest versions at the time of this writing are those:

Ubuntu android-sdk_r24.4.1-linux.tgz
Mac OS X android-sdk_r24.4.1-macosx.zip
Windows android-sdk_r24.4.1-windows.zip

Once downloaded uncompress it anywhere you like in your hard drive. It can be for example in your users home folder (/home/username/android-sdk-linux for Ubuntu, /Users/username/Applications/android-sdk-macosx for Mac OS X, C:\Documents and Settings\username\android-sdk-windows for Windows XP, C:\Users\username\android-sdk-windows for Windows Vista/7/8/10) or beside other applications (/usr/lib/android-sdk-linux for Ubuntu, /Applications/android-sdk-macosx for Mac OS X, C:\Program Files\android-sdk-windows for Windows).

Be sure you have configured the JAVA_HOME, ANDROID_HOME and PATH environment variables:

In Linux edit the ~/.bashrc file:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/javac::")
export ANDROID_HOME=~/android-sdk-linux
export PATH=$PATH:~/android-sdk-linux/tools

For Mac OS X you should edit the ~/.bash_profile file:

export JAVA_HOME=$(/usr/libexec/java_home)
export ANDROID_HOME=~/Applications/android-sdk-macosx
export PATH=$PATH:~/Applications/android-sdk-macosx/tools

Eclipse

For Eclipse installation just go to https://eclipse.org/downloads/, download it and install it.

Android Development Tools

Open Eclipse and go to “Help→Install New Software”, click “Add…” and enter “ADT Plugin” for “Name” and “https://dl-ssl.google.com/android/eclipse/” for “Location”.

eclipse-add-new-software

eclipse-add-new-software-adt-plugin

Select that repository in “Work with:”, choose all the packages and proceed the installation. Click OK if you see a message saying that unsigned content is being installed . Once installed you have to restart Eclipse.

After the restart, Eclipse will ask you to configure the Android SDK location. Click “Open Preferences”, select “Use existing SDKs” and fill “Existing Location” with the path you used to decompress Android SDK. If you don’t configure it now you can configure it anytime in the Eclipse preferences under the Android options.

eclipse-preferences-android

After configuring the Android SDK location path you will be asked to install SDK Platform Tools. Click “Open SDK Manager” (or select “Window->Android SDK Manager”), select all the packages inside “Tools” and the version of Android of your choice (I used 4.2) and click “Install X packages…”. Don’t worry if you see “Stopping ADB server failed (code -1).” in the log messages, it’s OK.

Android Virtual Device

Now that everything is installed you have to create an Android Virtual Device (AVD) so you can use the emulator. Open the Java perspective (Window->Open Perspective->Java or click the icon with the yellow plus sign at the top right of the IDE and select Java). You will see 2 new icons with the Android symbol in the tools bar. Click the one on the right that looks like a phone (the other one opens the Android SDK Manager), click “New…” and give your Android Virtual Device a name, a device type, a target, etc. If you can’t select a target just restart Eclipse so it can load the newly installed SDK Platform Tools.

In Ubuntu for 64 bit systems you can get this error when creating an AVD:

Error: Failed to create the SD card.
Error: Failed to create sdcard in the AVD folder.

To solve it install the ia32-libs libraries (http://stackoverflow.com/questions/3878445/ubuntu-error-failed-to-create-the-sd-card). Beware that they are about 260MB:

sudo apt-get install ia32-libs

Your first Android Application

Now you are ready to create your first Android application. Create a new “Android Application Project” with all the default options, just fill a “Application Name” like “Hello World”. The default option uses a “BlankActivity” with a template that needs the the Adroid Support library. It will ask you to install it and is as simple as clicking the “Install/Upgrade” button showed in that dialog window.

And there you have a fresh new “Hello Word” Android application. If you make the mistake of clicking the “Run” button you will get an error like this:

Error in an XML file: aborting build

That’s because by default the opened file after creating this “Hello World” project is “activity_main.xml”. You can also notice that a new file called “activity_main.out.xml” has been created, and it even is marked with errors. Delete that newly created file and open the “MainActivity.java” file under “src” folder. Now you can click the “Run” button. Select “Android Application” in the “Run As” dialog window.

I had a problem in Mac OS X Lion after running the emulator several times. When I tried to run the application the emulator crashed every time. Searching on the Internet I found this page where it explains that if you run the emulator in a secondary monitor and close it while in that monitor, a value of the X position of the window is stored and next time that value is bigger that the first monitors resolution and chashes. Deleting the emulator-user.ini file from the /Users/username/.android/avd/device_name folder (executing open /Users/username/.android/avd/device_name from a terminal window) and running the emulator again solved the problem.

As a hint I always deselect “Project->Build Automatically” option because I prefer to build the code manually pressing Control+B or Command+B.

Sources: http://developer.android.com/sdk/installing/installing-adt.html
http://developer.android.com/sdk/installing/adding-packages.html

Type the full pathname of a JDK installation when running Oracle SQL Developer

When you execute the Oracle SQL Developer for the firts time it’ll asks you to give the full pathname of a JDK installation:

 Oracle SQL Developer
 Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.

Type the full pathname of a JDK installation (or Ctrl-C to quit), the path will be stored in /home/user/.sqldeveloper/4.0.0/product.conf

oracle sql developer

If you don’t know that pathname run this command in the terminal and you’ll see the available JDKs and the one currently being used:

$ sudo /usr/sbin/update-alternatives --display javac
javac - auto mode
  link currently points to /usr/lib/jvm/java-7-oracle/bin/javac
/usr/lib/jvm/java-7-openjdk-amd64/bin/javac - priority 1051
  slave javac.1.gz: /usr/lib/jvm/java-7-openjdk-amd64/man/man1/javac.1.gz
/usr/lib/jvm/java-7-oracle/bin/javac - priority 1052
  slave javac.1.gz: /usr/lib/jvm/java-7-oracle/man/man1/javac.1.gz
Current 'best' version is '/usr/lib/jvm/java-7-oracle/bin/javac'.

You don’t have to give Oracle SQL Developer that full path but only the path that contains the bin folder, in my case it’s /usr/lib/jvm/java-7-oracle.

MTP support for Nexus 4 in Ubuntu 12.04

Add the langdalepl/gvfs-mtp package repository and update:

sudo add-apt-repository ppa:langdalepl/gvfs-mtp
sudo apt-get update
sudo apt-get install gvfs libmtp-common

Once this finishes reboot your system and then plugin your Nexus 4 and it should work.

converged-device-440x267

Ref: http://forum.xda-developers.com/nexus-4/general/mtp-support-ubuntu-12-04precise-nexus-4-t2330238

Avoid Android File Transfer starting automatically in Mac OS X after Android device plugin to USB port

“Android File Transfer” application starts automatically in Mac OS X when you plug your Android device to the USB port. Even if you only want to charge it.

Adroid File Transfer

This are the steps you have to take to avoid this (annoying) behaviour:

  1. Open the “Activity Monitor” and kill the “Android File Transfer Agent” process.
  2. Open “System Preferences” > “Accounts”. Go to the “Login Items” tab of your user. Remove the “Android File Transfer Agent”.
  3. Find your “Android File Transfer.app” file (usually in /Applications):

    • Right click (or ctrl+click) on it and select “Show package contents”.
    • Go to Contents/Resources.
    • Rename “Android File Transfer Agent.app” to e.g. “Android File Transfer Agent_DISABLED.app“.
  4. Go to /Users/username/Library/Application Support/Google/Android File Transfer (you can open Library folder with the “Go” menu in Finder while pressing “option” key or from the terminal running “open ~/Library”) and rename “Android File Transfer Agent.app” to e.g. “Android File Transfer Agent_DISABLED.app“.

Now “Android File Transfer” won’t auto-start when you plug your phone on the USB port, but if you open it manually everything will work just fine.

Ref: http://productforums.google.com/forum/#!topic/mobile/3hPIZjP0rDo

Select different available java and javac versions in Ubuntu with update-alternatives

It’s possible to have more than one version of Java in the same machine.

openjdk

If you have more than one Java JRE or JDK installed you can switch between them with the update-alternatives command:

$ sudo /usr/sbin/update-alternatives --config java
There are 2 choices for the alternative java (providing /usr/bin/java).

  Selection    Path                                           Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-i386/jre/bin/java   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java to provide /usr/bin/java (java) in manual mode.

You can do just the same for javac:

$ sudo /usr/sbin/update-alternatives --config javac

There are 2 choices for the alternative javac (providing /usr/bin/javac).

  Selection    Path                                        Priority   Status
------------------------------------------------------------
* 0            /usr/lib/jvm/java-6-openjdk-i386/bin/javac   1061      auto mode
  1            /usr/lib/jvm/java-6-openjdk-i386/bin/javac   1061      manual mode
  2            /usr/lib/jvm/java-7-openjdk-i386/bin/javac   1051      manual mode

Press enter to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/lib/jvm/java-7-openjdk-i386/bin/javac to provide /usr/bin/javac (javac) in manual mode.

You can check wich version is currently in use running:

$ java -version
java version "1.7.0_25"
OpenJDK Runtime Environment (IcedTea 2.3.10) (7u25-2.3.10-1ubuntu0.12.04.2)
OpenJDK Server VM (build 23.7-b01, mixed mode)
$ javac -version
javac 1.7.0_25