Using the ArcGIS Android SDK with Android Studio – Part 1

Google’s Android Studio is a developer IDE based on IntelliJ IDEA platform. The platform is still in early access preview. If you are not comfortable using an unfinished product, you may want to continue to use the Eclipse Plugin bundled with the ArcGIS Android SDK.

Android Studio Basics

In Eclipse you have the concept of a workspace which can consist of multiple projects linked or unlinked. In Android Studio, projects are replaced by App Modules and Libary Modules. Modules are a discrete unit of functionality that can be run, tested, and debugged independently. Modules are somewhat similar to an Eclipse project with a few key differences:

Each module has it’s own Gradle build file

Some modules can be Library Modules which are conceptually the same as Libary Projects in Eclipse.

Including Jar lib dependencies
Local jar files go in the libs folder at the root of your module directory. In Android Studio you need to add the jars as a gradle dependency. This should be added to your gradle build file as follows:

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

The code above adds all the *.jar files from the modules libs directory as dependencies.

Including pre-built Native .so libs
Local pre-built native libs go in the jniLibs folder in your modules /src/main/ folder. There is no need to add a gradle dependency when using this folder. Native libs outside this folder would require a gradle dependency.

Manifest changes
Several common items and settings which in Eclipse are in the Android Manifest have either automatically added, e.g: allowBackup=true or have been moved to build.gradle such as version codes and minSdkVersion, targetSdkVersion, etc. Feature and permission requirements are still declared in the manifest.

Integrate ArcGIS Android SDK

In this section we will step through the process of creating a new Android Studio project and integrate the ArcGIS Android SDK in the app module.

Create a new Android Studio Project

Open up Android Studio and choose New Project under Quick Start

Choose and Application name and Module name. These can be the same in this example. We will use Hello World for both.

Continue to accept the defaults in the remaining windows by clicking Next and complete the wizard.

Your project should open up.

Update the Projects Gradle build

As we stated earlier, each module has it’s own gradle build file. This include the project as well. In this step we want to ensure that your project is using the latest Gradle plugin. Gradle plugins are on a different release cycle then Android Studio so you may have to manually update until Android Studio directly supports the latest version of Gradle plugin.

Double click on your projects build.gradle file to open it in the editor. This will be the one at the project root directory, not in your app module directory.

Ensure that under dependencies you have the following classpath

dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}

Click the Sync Project with Gradle Files button from the toolbar.

Gradle Plugin v0.10.0 is the latest version of the Gradle Plugin at the time of the writing so the version will change as more releases come out.

Update App Module Manifest

The Android.manifest file is now located in the /src/main/ directory of your app module.

Now we need to create a MapView object and access the map from the layout

Instatiate a MapView variable by adding the following:

MapView mMapView;

Add the following code to the onCreate method after setContentView():

// after the content of this activity is set
// the map can be accessed from the layout
mMapView = (MapView)findViewById(R.id.map);

Run the app!

Hello World – Map in your App

Summary

In this post we directly integrated the ArcGIS Runtime SDK for Android into Android Studio and created the Hello World sample app. I will re-state that Android Studio and the Gradle plugin are still in an early access preview release state. Several features are either incomplete or not yet implemented so there may be breaking changes when releases are updated.

In part 2 of this series we will introduce a library module to allow multiple app modules the ability to re-use the ArcGIS Android API libraries.

7 Comments

Would you be able to add instructions for using the Android Sample Code? At https://developers.arcgis.com/android/sample-code/ there are instructions for Eclipse and IntelliJ. I tried to follow the IntelliJ instructions as things should be similar to Android Studio, but no luck.

This blog post is outdated with the release of 10.2.5. Please go to the released samples repository on GitHub and follow the README to get started with the samples. Read the install and setup guide doc to get started using the 10.2.5 SDK. And read migrating to Android Studio blog post to see about migrating existing Eclipse projects.