Skip to content

Using Android Studio

Roger Hu edited this page Nov 27, 2013 · 61 revisions

If you find yourself highly dissatisfied with developing with Eclipse (i.e. having to restart whenever recompiles somehow don't work, too many window panes to find, etc.), you may wish to download a copy of Android Studio (http://developer.android.com/sdk/installing/studio.html). It currently is alpha and there are updates to the IDE every week, so be forewarned.

Migrating from Eclipse

Check out http://developer.android.com/sdk/installing/migrate.html. The plugin attempts to export your Eclipse project to Gradle. What it essentially does is relocates your Java and XML resource files to src/main/java and src/main/res respectively. Gradle looks explicitly in the src/main directory and can compile successfully but fail to include your .class files if they are not located in these particular directories (see section about using Gradle in Android Studio)

The plugin will also generate a build.gradle. An example of what the file format is shown at: https://github.com/thecodepath/android_guides/wiki/Getting-Started-with-Gradle

Dependency Configurations

Android Studio gives you the option of using several different dependency management configurations. Normally when you import existing projects, it tries looking for Maven or Gradle-based project files (pom.xml or build.gradle respectively). You cannot switch dependency management systems after you've selected one.

There is one more way which isn't well documented, which is to choose neither option and rely on manual configuration within Android Studio to define the dependencies yourself. Currently Gradle inside Android Studio is very slow (though there are parallel and daemon modes being added to speed things up), so for smaller projects you may actually find it easier to take advantage of Android Studio's Project Structure to define your dependencies.

Using Gradle in Android Studio

Currently, Android Studio and Gradle actually operate very much independently of each other. Even if your Android Studio compiles correctly, you may find NoClassDefErrors when it tries to execute the software in your emulator. Hopefully by the time Android Studio v1.0.0 there will be more UI to manage the dependencies in Gradle, but currently everything must be done by tweaking build.gradle files.

Manual dependency management

  1. If you want to use manual dependency management, reimport your project into Android Studio. Do not choose either Gradle or Maven to build.

    image

  2. Suppose you want to have your Google Play Services and Facebook SDK defined as dependencies. Go to Project Structure->Modules and under your ProjectName, add the Google Play and Facebook module as a dependency to your module (look for the + sign at the right side):

    image

    You should see under your main project that Google Play Services + Facebook added and the Export button clicked:

    image

  3. Make sure to mark the Google Play Services and Facebook SDK as a Library module. Otherwise, you may get NoClassDefError exceptions when running. Make sure that you have declared your dependencies and exported them correctly.

    image

  4. If you're adding libraries that also define R.java resource files, you cannot add them as .jar files. You need to use the Create Module from Existing Sources and mark the src directory as a "Source directory"

Finding these guides helpful?

We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.

Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.

Clone this wiki locally