+

Search Tips | Advanced Search

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


Set up Android Studio projects with Gradle

The supported development environment for Android applications with MobileFirst SDK is Android Studio. Before beginning, set up Android Studio and the Android SDK properly. MobileFirst SDK is compatible with Android version Ice Cream Sandwich (API level 14) and later.


Procedure

  1. Create an Android application in Android Studio...

      File > New > New Project wizard

    Make sure the project compiles without error.

    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. If we are installing aar files from remote copies, add this line to the dependencies closure in file app\build.gradle ...

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

    In this example the latest version of 8.0 is imported. 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'

    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.+'  
          }
      }

    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.

    If we 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. Optional: Register Javadocs to an Android Studio Gradle project.

  6. Add 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: 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 Register 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