This article presents some considerations regarding and how to convert from using Eclipse to Android Studio for developing applications. The information in this article is current as of August, 2014, and refers to Android Studio v0.8x, primarily on Linux platforms.
As the Android team puts Eclipse and the ADT package on the back burner, SDG's engineers found ourselves looking to move more fully to Android Studio. Android Studio brings a number of improvements over Eclipse/ADT, the biggest one being performance. It also features better integration with connected Android devices, and better predictive code completion. Rounding out the list of most significant changes is a switch between build tools: Eclipse builds projects with Ant, while Android Studio uses Gradle.
Ordinarily, Android Studio is easy to get up and running; however, because of this last change, there are a few gotchas to be aware of when attempting to work with projects started with Eclipse.
Installing Android Studio is relatively straightforward. First, you need Oracle Java 6 or later, or OpenJDK 7, and you'll need a JAVA_HOME environment variable which points to your installation. Second, if you have an Android SDKs directory you'd like to keep, you'll need an ANDROID_HOME environment variable pointing to it. (If you don't care, you can leave this step off, and Android Studio will make a new one for you.) Finally, you may need to install a specific version of Gradle when working with imported projects. Android Studio 0.8 calls for Gradle 1.10. You can find download links here.
Once you've finished that preparation, you can install Android Studio. Google provides binary downloads here, and users of Ubuntu or derivatives can install the android-studio package from ppa:paolorotolo/android-studio.
<div style="white-space: pre; font-family: monospace;">
sudo add-apt-repository ppa:paolorotolo/android-studio
sudo apt-get update
sudo apt-get install android-studio
</div>
There are two mechanisms for moving an Eclipse project into Android Studio. Unfortunately, neither is ideal. First, you can import an Eclipse project directly into Android Studio, but this option copies the project to a new directory, using Android Studio's project structure. This presents issues when attempting to work in pre-existing source control. Second, you can use Eclipse to export Gradle build files suitable for Android Studio. These are poorly organized, using an Eclipse-and-Ant view of dependencies and libraries, as separate projects or prebuilt libraries, rather than an Android-Studio-and-Gradle one, of libraries as Maven dependencies.
The best way to migrate a project from Eclipse to Android Studio, if you want to keep open the option of using either, is therefore to create a Gradle build script by hand. This is less daunting than it sounds. Below is a template, which we'll go through piece by piece. Create a build.gradle file in your project's root directory, and copy in the following.
<div style="white-space: pre; font-family: monospace;">
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile files('libs/PrebuiltLibrary.jar')
// You must install or update the Support Repository through the SDK manager to use these dependencies.
compile 'com.android.support:support-v4:19.+'
// Dependencies from Maven
compile 'org.mapsforge:mapsforge-map-android:0.4.3'
}
android {
buildToolsVersion '20.0'
compileSdkVersion 19
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
signingConfigs {
release {
keyAlias 'releasekey'
keyPassword 'keypassword'
storeFile file('/path/to/keystore')
storePassword 'keystorepassword'
}
}
}
</div>
The first block, buildscript { ... }, is boilerplate. Gradle will automatically search out any version of the Android Gradle build tools greater than 0.12.
<div style="white-space: pre; font-family: monospace;">
apply plugin: 'android'
</div>
This determines the type of project. As given, it will produce an application project. Change 'android' to 'android-library' to import a library project. (You can use the File->Import Module option in Android Studio to add a local library project to an application project.)
<div style="white-space: pre; font-family: monospace;">
repositories {
mavenCentral()
}
dependencies {
compile files('libs/PrebuiltLibrary.jar')
// You must install or update the Support Repository through the SDK manager to use these dependencies.
compile 'com.android.support:support-v4:19.+'
// Dependencies from Maven
compile 'org.mapsforge:mapsforge-map-android:0.4.3'
}
</div>
This block specifies Maven dependencies. The repositories block and the mavenCentral() call instruct the build system to look at the central Maven repository at maven.org for dependencies. There are three separate uses of the 'compile' directive in the dependencies block which are of interest. The first pulls a prebuilt library into the project. The second and third share a format which describes a library by its Maven coordinates: <group-id>:<artifact-id>:<version>. The second pulls from the Google Support Repository, which, as the comment remarks, comes from an SDK Manager component. The third comes from Maven Central, through the repositories block.
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
</div>
Skipping self-explanatory sections, this block sets up Gradle to work with the Eclipse project structure, telling it where to find source code and resources. The final block, signingConfigs, provides information to export signed APKs for release. Having prepared your build.gradle file, you can import it into Android Studio. Using the File->Import Project menu, select the build.gradle file you just created. Finally, you may need to set the project to use your Gradle installation. Using the File->Settings dialog, pick 'Local Gradle Distribution' in the Gradle pane, then set it to your Gradle installation's path. There are some additional build.gradle tips and tricks you might find useful we hope to share in a future article.
Comments