Exporting An Android Project From Eclipse Into Android Studio 0.4.0

packing

The Eclipse IDE. Remember the good times? So few, so…far between. The time has come to get comfortable with Android Studio, this guide will show you how to pack your bags and make the transition.

I encountered a number of issues while exporting an existing Eclipse project (containing multiple sub-projects) into Android Studio. I will show you how to avoid these problems and successfully migrate an old Eclipse project to Android Studio using Gradle. My goal is to cover a number of common errors you may encounter when migrating a cranky old Eclipse app to Android Studio.

We will cover the following:

  • Fixing different classes of Gradle error you are likely to encounter
  • Resolving Android manifest merge errors
  • Fixing Gradle version errors
  • Importing multi-project Eclipse projects into Android Studio Modules
  • Exporting / Importing Library projects

This guide covers the process using Gradle 1.9, Eclipse Juno and Android Studio 0.4.0. The Android project had API level 9 – 15.

Common Gradle Export Errors

Eclipse, by default, seems to export bad Gradle files (out of date) and Android Studio does not like this. The fix is quite easy, I’ll cover it later on in this guide.

If you see either of these errors or errors like these: “Gradle version 1.6 is required. Current version is 1.9” or  “Resolve Error: You are using an old, unsupported version of Gradle. Please use version 1.9 or greater” then you are going to need to make a couple manual tweaks to a single Gradle file and Android Studio’s preferences.

Export Gradle Build Files From Eclipse

Starting from your existing Eclipse project…

File -> Export -> Gradle Build Files

Export

Gradle Build Files

Add all your build files:

Build Files

Click “next.”

Generate

 Importing Eclipse Builds Into Android Studio

import

Open Android Studio and select “Import Project.”

directory

Select your project directory and hit “ok.”

import

You will see a dialog like the above.

Dealing With Gradle Errors

Ok, so here is the situation, it is likely you will get some of the below errors while attempting to import a project exported by Eclipse:

Unsupported

Resolve Error

The first thing to do is make sure your copy of Android Studio is using the correct version of Gradle by default. Start by clicking “Android Studio” -> Preferences -> Gradle.

preferences

gradle settings

Now make sure your Gradle settings look like the above. If your Gradle is 1.6 or some other version, download the latest Gradle here: http://www.gradle.org/downloads. Unzip that folder into some directory such as /Development/Gradle-1.9/ and point Android Studio at that directory.

Updating Eclipse Gradle Export Files So that Android Studio Can Use Them

Ok, so now that we have upgraded our Gradle, we are still going to get errors until we modify the Gradle files exported by Eclipse. I promise this won’t hurt.

Simply open the file “build.gradle” which was created by Eclipse in the very root of your multi-project setup and you will see something like this:

gradle

Here is the problem: Eclipse exported 0.5.+ as the version number. The fix is to change this to 0.7.+ like so:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}

You are now telling Android Studio to use Gradle version 1.9. Now try to import your project into Android Studio again.

Missing Android API Gradle Errors

Missing Target

You may get an error about a missing Android target if you are trying to import projects for which your copy of Android Studio isn’t prepared for.

To fix this click the Android AVD manager in the Android Studio toolbar:

avd

Now find the missing Android API level and install it and try again.

Could not find element “Application” errors

Android Studio will require an “Application” tag in the Android Manifest file of any imported applications. This error may be caused by attempting to import (from Eclipse) an Android Library project and Gradle failing to inject the correct plugin tags.

See below for the solution.

Manifest Merging Failed

If you get “manifest merging failed” errors you may need to look at setting the min and target SDK settings in the build.gradle file specific to that sub-project to match the AndroidManifest.xml’s min and target SDK versions for that file:

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 16
    }
}

This error’s source may also be that your imported module has incompatible SDK level with another module in the workspace. If one of your modules depends on another module and their version codes are incompatible this may be the result.

Missing ‘Application’ Error

Android Studio seems to require that the Android Manifest of an included library application have an <application/> tag in it someplace. In the project I was imported I solve this issue simply by adding an empty <application/> tag to the manifest.

Importing Android Libraries

To import a library, the primary change is to alter the plugin to “android-library” instead of “android.

So instead of:

apply plugin: "android"

You will now have:

apply plugin: "android-library"