For up-to-date product documentation, see the IBM MobileFirst Foundation Developer Center.


Set up Android Studio projects with Gradle


Before you begin

Ensure that you set up Android Studio and the Android SDK properly. For more information about how to set up your system, see Android Studio Overview.

Note: MobileFirst SDK is compatible with Android version Ice Cream Sandwich (API level 14) and later.


Overview

The documented and supported development environment for Android applications with MobileFirst SDK is now Android Studio. To develop a new Android application with Android Studio and MobileFirst SDK, follow these steps.


Procedure

  1. If you do not already have one, create an Android application in Android Studio by using File > New > New Project wizard. Make sure the project compiles without error.

    Note: There are two versions of the build.gradle file created, one in the main project folder and one in the \apps folder.

  2. Make sure the [project]\build.gradle file has the jcenter() in list of repositories in the allprojects{} closure.

    allprojects { repositories { jcenter() // other repositories } } The following example shows a sample [project]\build.gradle file created by the Android Studio wizard:

    // Top-level build file where we can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } The actual contents of your file can vary, depending on whether other repositories or dependencies have been added.

  3. Add the following packaging options within your android{} closure in the app\build.gradle file:

    packagingOptions { pickFirst 'META-INF/ASL2.0' pickFirst 'META-INF/LICENSE' pickFirst 'META-INF/NOTICE' }

  4. Depending on whether you are installing aar files from a local copy or accessing them remotely, add the following lines to our app\build.gradle file.
    1. If you are installing from remote copies of the files add this line to the dependencies closure.

      compile 'com.ibm.mobile.foundation:ibmmobilefirstplatformfoundation:8.0.+'

      Note: In this example the latest version of 8.0 is imported. If we want to import a specific version such as 8.0.2016021411, replace with the MobileFirst version number you are using, including the major, minor, and patch numbers. The patch number is in the format YYYYMMDDHH. For example:

      compile 'com.ibm.mobile.foundation:ibmmobilefirstplatformfoundation:8.0.2016021411' The following is an example of an app/build.gradle file for adding the SDK from remote files:

      applyplugin: 'com.android.application' repositories { jcenter() } android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.myname.myapplicationandroidgradle" minSdkVersion 23 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { pickFirst 'META-INF/ASL2.0' pickFirst 'META-INF/LICENSE' pickFirst 'META-INF/NOTICE' } } dependencies { compile 'com.ibm.mobile.foundation:ibmmobilefirstplatformfoundation:8.0.+' } }

    2. Remove the proguardFiles line from the buildTypes enclosure and save the file. Proguard is not supported in IBM MobileFirstâ„¢ Platform Foundation V8.0.0. See Obfuscating Android code with ProGuard.
    3. If you are installing from local files:

      Add the following to the dependencies closure.

      compile(name:'ibmmobilefirstplatformfoundation', ext:'aar')

      Add a repositories enclosure:

      repositories { flatDir { dirs 'libs' } }

      Note: To acquire the necessary SDK files see Acquiring the MobileFirst SDK from the MobileFirst Operations Console. Copy the relevant aar files to the app\libs folder. The following is an example of an app\build.gradle file for adding the SDK from local files:

      apply plugin: 'com.android.application' repositories { flatDir { dirs 'libs' } } android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.myname.myapplicationandroidgradle" minSdkVersion 23 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } packagingOptions { pickFirst 'META-INF/ASL2.0' pickFirst 'META-INF/LICENSE' pickFirst 'META-INF/NOTICE' } } dependencies { compile(name:'ibmmobilefirstplatformfoundation', ext:'aar') }

  5. The imported SDK does not include the Javadocs. To add the Javadocs to our project see Registering Javadocs to an Android Studio Gradle project.
  6. For information on adding additional features see Adding the optional MobileFirst components with Gradle.
  7. Add the following lines to the AndroidManifest.xml file of your native app for Android:

    • <activity android:name="com.worklight.wlclient.ui.UIActivity" />

      This line adds the ability for a designated MobileFirst UI activity to run in the user application.

      Note: If we want to automate this process we can add the following task to the app\build.gradle file:

      task(addUIActivity) << { def manifestFile = file("src/main/AndroidManifest.xml") def manifestText = manifestFile.getText() if(!manifestText.contains("com.worklight.wlclient.ui.UIActivity")) { def pattern = Pattern.compile("\\</application\\>") def matcher = pattern.matcher(manifestText) def manifestContent = matcher.replaceFirst("<activity android:name=\"com.worklight.wlclient.ui.UIActivity\"/>\n</application>") manifestFile.write(manifestContent) } } preBuild.dependsOn addUIActivity

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

      This line adds internet access permissions to the user application.

  8. Rebuild your application.


Results

We can now start developing your native Android application with the IBM MobileFirst Platform Foundation SDK.


What to do next

Before we can access server resources, we must register your app. See Registering Android applications from the MobileFirst Platform CLI. For details about the mfpclient.plist file see Android client properties file. Once the app is registered we can write some initial code for testing the server connection (Some initial code for accessing the server.

Parent topic: Methods of setting up your environment