Migrating to Android Studio

Android Studio is the official IDE for Android and is the default IDE for ArcGIS Runtime SDK for Android.  This post will guide you through migrating your ArcGIS Android projects from Eclipse to Android Studio.

Import Eclipse project to Android Studio

Importing your Eclipse projects directly into Android Studio is the preferred approach as it will detect many source libs and replace them with maven dependencies, e.g. Android Support Library, which means you no longer have to maintain the libraries manually.  Additionally, it will rewrite the project using the new canonical Android Gradle project structure.

Import directly into Android Studio

  1. In Android Studio, close any projects currently open.  You should see the Welcome to Android Studio window.
  2. Click on Import Non-Android Studio project.
  3. Navigate  to the directory containing your Eclipse ADT project and click OK.
  4. Ensure your Import Destination Directory is correct and click Next.
  5. Click OK if the directory does not exist.
  6. Android Studio presents you with some import options, leave all options selected and click Finish.

Once your import is complete, Android Studio displays an import summary, describing all the changes it’s made to your project. This summary contains details about which files were moved during the import process, and where you can find them in the new Android Gradle layout, plus information on any third party libraries or JAR files that Android Studio has replaced with Gradle dependencies.

Have a look for any ArcGIS Android native libs that it tried to restructure. Your project should open up in the default Android view.  Please read about the Android project view if you are new to Android Studio.

  • Remove any ArcGIS native libs that the importer restructures to app/src/main/jniLibs.  With your project in Android view, remove the ArcGIS Android native libs, e.g. libruntimecore_java.so, from apps > jniLibs.

Note:  Android Studio will copy any jar dependencies in your Eclipse project that could not be resolved by a Maven dependency to your migrated projects libs folder and add them to your app dependencies. Remove any dependencies that can be replaced by a maven repository, e.g. compile files(‘libs/arcgis-android-api.jar’).

Add ArcGIS Android support

The Android project view groups all the build files for all modules at the top level of the project hierarchy.  Please read about Android’s Gradle build system if you are new to Android Studio.  Notice their are 2 build.gradle files at the top of  Gradle Scripts: the project and app build scripts.  We will add the ArcGIS Maven repository URL to the project build script then update the app module build script to include the ArcGIS dependency.

Add ArcGIS Maven repository

  • Open your project level build.gradle file and add our arcgis maven repository to the list of repositories in the allprojects task:
maven {
    url 'http://dl.bintray.com/esri/arcgis'
}

Your project build.gradle file should look like the following:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'http://dl.bintray.com/esri/arcgis'
        }
    }
}

Add the ArcGIS Android library project AAR bundle dependency

  • Open your app module build.gradle file and add the following dependency at the bottom of the script:
dependencies {
    // ArcGIS Android 10.2.5 API
    compile 'com.esri.arcgis.android:arcgis-android:10.2.5'
}

Required packaging options

  • Gradle may complain about duplicate files in the build apk. You can exclude those files by adding the packagingOptions directive to your android task.  Add the following directive inside the android task:
packagingOptions{
exclude 'META-INF/LGPL2.1'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}

Your app build.gradle file should look like the following:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "com.esri.arcgis.android.samples.helloworld"
        minSdkVersion 15
        targetSdkVersion 19
    }

    packagingOptions{
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
    }

    buildTypes {
        release {
        minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
   }
}

dependencies {
    // ArcGIS Android 10.2.5 API
    compile 'com.esri.arcgis.android:arcgis-android:10.2.5'
}

Update Android Manifest

Updating your android manifest is optional.  The app module gradle build script determines which API to build your project against and what the min sdk level support is so it can be removed from your manifest.

  • Open your project AndroidManifest.xml file
  • Remove the <uses-sdk> element
  • Remove the android:versionCode and android:versionName parameters from the <manifest> element.

Your manifest should look like the following:

<?xml version="1.0" encoding="utf-8"?>
<manifest
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.esri.arcgis.android.samples.helloworld">

  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

  <uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
  android:icon="@drawable/icon"
  android:label="@string/app_name"
  android:allowBackup="true" >
    <activity
      android:name=".HelloWorld"
      android:configChanges="orientation"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>

</manifest>

Sync and Run your app

You are now ready to sync your project up with Gradle.

  • Click the Sync Now link in the upper right hand corner of Android Studio.
  •  Run your app!

Conclusion

Android Studio provides an easy migration workflow for most Eclipse projects.  All of the projects dependencies come from the maven repository which offers better dependency management in this release and updating to future releases.  Happy Coding!

This entry was posted in App Developers, Apps, Developer, Mobile and tagged , , , , , . Bookmark the permalink.

Leave a Reply

2 Comments

  1. fbenitez says:

    Hi Dan,

    Can I create and post this tutorial in Spanish for the for the Hispanic community?

  2. angelaikz says:

    i would suggest you to try DuplicateFilesDeleter , it can help resolve duplicate files issue.