Skip to content

Using Android Studio

Nathan Esquenazi edited this page Sep 28, 2014 · 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, poor XML editing/validation, etc.), you may wish to download a copy of Android Studio. It currently is beta and there are updates to the IDE every week, but the constant improvements are making it better (see recent tools page for more details). This program is based on the JetBrains family of IDE's, so if you've used IntelliJ, PyCharms, or RubyMine, you'll find the user experience very familiar. Fortunately, this IDE is made free for Android developers.

Migrating from Eclipse

Since version 0.4, Android Studio now has the ability to import your Eclipse projects directly. When you do the import, your Java and XML resource files are relocated to src/main/java and src/main/res respectively. Gradle looks explicitly in the src/main directory and can compile successfully but will fail to include your .class files if they are not located in these particular directories.

The plugin will also generate a build.gradle. An example of what the file format is shown in the Gradle cliffnotes.

Using Gradle in Android Studio

When you make changes to the build.gradle, the changes should now be reflected in the IDE. You may have target SDK versions conflicts (i.e. Facebook SDK targets for API versions above 17), so you may need to resolve your Gradle configurations to be consistent.

Android Studio should update the build.gradle file if you try to add dependencies. Any .jar files in the libs/ directory are already incorporated at compile time. You can verify this fact by opening the Gradle file for the specific module you're using:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

Also, you should be able to do the same with Maven dependencies. go to Project Structure -> Modules-> Dependencies. Click on + -> Library Dependency and add modules such as "com.loopj.android:android-async-http:1.4.3". Double-check the build.gradle file to see that this change was added.

Regardless, you should keep track of what gets changed in the build.gradle since that is ultimately the file Android Studio uses to handle dependency management.

Other Notes

  • One useful tip -- inside Edit->Run Configurations, make sure to clear LogCat per run. It's often easy to get confused of stack traces and this option clears the logging statements between each test run:

    image

  • If you're adding libraries that also define R.java resource files (such as a PullToRefresh library or Google Play Services), you cannot add them as .jar files. They must be included as dependencies. If you try to include them as .jar files, you may encounter R definitions not found during execution time.

  • Install the GenerateSerialVersionUID to allow you to use the same feature in Eclipse to auto-generate a unique ID for a Serializable class.

image

You can then click on Code->Generate to SerialVersionUID:

image

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