Configure the Oracle Mobile Hub SDK with your Mobile Application

You must configure the Oracle Mobile Hub SDK with your mobile application before you write any code.

Add the SDK

In a basic app setup, without intervening frameworks, you'll add the Android client SDK to an app.

  1. Unzip the Android client SDK zip file.

  2. Copy the SDK jars into the libs folder in your app's project. If this folder doesn't exist, create it at the same level in your hierarchy as your src and build folders.

  3. In Android Studio, select File, then New, and then New Module to start the wizard to import the IDMMobileSDK.jar into the project.

  4. Click Import .JAR/.AAR Package, and follow the wizard to import the IDMMobileSDK.jar into the project.
  5. In the source tree for the application, create a folder called assets (at the same level as the java and res folders).

  6. In the SDK bundle, locate the oracle_mobile_cloud_config.xml file and copy it to the assets folder.

  7. In your app's build.gradle file, make sure the following are among the dependencies registered so that the SDK libraries are available to the app.

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.squareup.okhttp3:okhttp:3.9.0'
        compile 'org.slf4j:slf4j-jdk14:1.7.13'
        //to enable the app to receive notifications, include the following:
        compile 'com.google.firebase:firebase-messaging:11.0.2'
    		compile 'com.facebook.android:facebook-android-sdk:4.25.+'
    }
  8. If the app is designed to receive notifications as part of analytics campaigns, add the following line at the very bottom of build.gradle.

    apply plugin: 'com.google.gms.google-services'
  9. Open assets/oracle_mobile_cloud_config.xml and fill in the environment details for the mobile backend that the app will be using.

Configure SDK Properties

To use the client SDK in an Android app, you need to add a oracle_mobile_cloud_config.xml configuration file to the app and fill it in with environment details for your backend in Oracle Mobile Hub. In turn, the SDK classes use this information to construct HTTP headers for REST calls made to Oracle Mobile Hub.

You package the configuration file in your app’s main bundle in the assets folder at the same level as the java and res folders. For example, in the sample GettingStarted app, it’s in /GettingStarted/src/main/assets.

The file is essentially divided into the following parts:

  • The mobileBackend element and its contents.

    You include this part if you are using a backend with the app. The SDK classes use the environment and authentication details you specify there to access the backend and construct HTTP headers for REST calls made to APIs.

  • Elements that apply to the configuration as a whole, such as logLevel and oAuthTokenEndpoint. These elements generally, but don’t have to, appear at the top of the file.

The following code sample shows the structure of a oracle_mobile_cloud_config.xml file.

<config>

  <!--This value is required if you are using OAuth to authenticate against the mobile backend-->
  <oAuthTokenEndPoint>YOUR_OAUTH_TOKEN_END_POINT<oAuthTokenEndPoint>
  <!--Set to true if you want to get logging information-->
  <enableLogger>true</enableLogger>
  <logLevel>DEBUG</logLevel>
  <!--Whether to log HTTP call request and response bodies and headers-->
  <logHTTP>true</logHTTP>

  <!-- Include the mobileBackend element and its sub-elements if you are going 
       to be using a backend to access custom and platform APIs.-->
  <mobileBackend>
    <name>MBE_NAME</name>
    <baseUrl>BASE_URL</baseUrl>
    <enableAnalytics>true</enableAnalytics>
    <authentication>
      <!--possible values for type are [oauth, basic, facebook, tokenExchange ]-->
      <type>AUTH_TYPE</type>
      <oauth>
        <clientId>CLIENT_ID</clientId>
        <clientSecret>CLIENT_SECRET</clientSecret>
        <enableOffline>true</enableOffline>
      </oauth>
      <basic>
        <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
        <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        <enableOffline></enableOffline>
      </basic>
      <facebook>
        <appId>FACEBOOK_APP_ID</appId>
        <scopes>public_profile,user_friends,email,user_location,user_birthday</scopes>
        <basic>
          <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
          <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        </basic>
      </facebook>
      <tokenExchange>
        <! tokenExchange can contain an 'oauth' sub-element or a 'basic' sub-element.
        <oauth>
          <clientId>CLIENT_ID</clientId>
          <clientSecret>CLIENT_SECRET</clientSecret>
        </oauth>
        <basic>
          <mobileBackendId>MOBILE_BACKEND_ID</mobileBackendID>
          <anonymousKey>ANONYMOUS_KEY</anonymousKey>
        </basic>
      <tokenExchange>
    </authentication>
    <!-- additional properties go here -->
  </mobileBackend>

</config>

The values that you need to fill in for a given backend can be found on the Settings and App Profile pages for that mobile backend.

Here are some more notes on the file’s elements.

  • oAuthTokenEndPoint — The URL of the OAuth server from where your application gets its authentication token. This key needs to be provided for all applications that rely on OAuth to authenticate. You get this from the backend’s Settings page.

  • logLevel — Determines how much SDK logging is displayed in the application's console. The default value is ERROR. Other possible values (in increasing level of detail) are WARNING, INFO, and DEBUG. It is also possible to specify NONE.

  • enableLogger — When set to true, logging is included in your application.

  • logHTTP — When set to true, the SDK logs the HTTP and HTTPS headers in requests and responses.

  • mobileBackend — An element containing authentication details for your backend and other optional details, such as synchronization properties.

    You get the authentication details, such as the OAuth and HTTP credentials, from the backend’s Settings page.

  • mobileBackend/baseUrl — The base URL for all APIs that you call through the backend. You get this from the backend’s Settings page.

  • mobileBackend/authentication — Contains the following sub-elements:

    • The type sub-element, with possible values of oauth, basic, facebook, and tokenExchange.

    • One or more sub-elements for authentication types, each containing authentication credentials.

      You can also add the offlineEnabled key and set its value to true.

  • enableOffline — If set to true, offline login will be allowed. This applies only to the Basic and OAuth login types. For this to work, you also need to add the following to the application's AndroidManifest.xml file:
    <receiver android:name="oracle.cloud.mobile.network.NetworkHelper"
      <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
    </receiver>

Configure Your Android Manifest File

Permissions for operations such as accessing the network and finding the network state are controlled through permission settings in your application's manifest file, AndroidManifest.xml. These permissions are required:

  • permission.INTERNET — Allows your app to access open network sockets.

  • permission.ACCESS_NETWORK_STATE — Allows your app to access information about networks.

Other permissions are optional. For example, there are a number of permissions necessary for the app to be able to receive notifications. For a rundown on the available permissions, see Android Manifest Permissions in the Google documentation.

Add the permissions at the top of your AndroidManifest.xml file, as shown in the following example:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="oracle.cloud.mobile.sample" >
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

  <application>
<provider
	android:name="com.facebook.FacebookContentProvider"
	android:authorities="com.facebook.app.FacebookContentProvider430660953811847"
	android:exported="true" />    
<receiver 
	android:name="oracle.cloud.mobile.network.NetworkHelper"
      <intent-filter>
        <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
      </intent-filter>
    </receiver>

    (.....)
  </application>
</manifest>

Adding the client SDK to your application may require you to configure your AndroidManifest.xml file to add new permissions or activities. For example, if you add the Notifications individual SDK library, you may also need to add a new broadcast receiver.