将 MobileFirst Foundation SDK 添加到 Android 应用程序

improve this page | report issue


概述

IBM Mobile Foundation SDK 包含通过 Maven Central 可用并且可添加到 Android Studio 项目的依赖关系集合。 依赖关系与核心函数和其他函数对应:

  • IBMMobileFirstPlatformFoundation - 实现客户机到服务器连接,并处理认证和安全方面、资源请求及其他必需的核心函数。
  • IBMMobileFirstPlatformFoundationJSONStore - 包含 JSONStore 框架。 有关更多信息,请查看针对 Andoid 的 JSONStore 教程
  • IBMMobileFirstPlatformFoundationPush - 包含“推送通知”框架。 有关更多信息,请查看通知教程

在此教程中,了解如何使用 Gradle 将 MobileFirst 本机 SDK 添加到新的或现有的 Android 应用程序。 您还可以了解如何配置 MobileFirst Server 以识别应用程序,查找有关添加到项目的 MobileFirst 配置文件的信息。

先决条件:

跳转至:

添加 MobileFirst 本机 SDK

遵循下面的指示信息将 MobileFirst 本机 SDK 添加到新的或现有的 Android Studio 项目,以将应用程序注册到 MobileFirst Server 实例。

在您开始之前,确保 MobileFirst Server 正在运行。
如果您使用本地安装的服务器:从命令行窗口,浏览至服务器的文件夹,并且在 Mac 或 Linux OS 上运行命令 ./run.sh,或者在 Windows 上运行 run.cmd

创建 Android 应用程序

创建 Android Studio 项目或使用现有项目。

添加 SDK

  1. Android → Gradle Scripts 中,选择 build.gradle (Module: app) 文件。

  2. apply plugin: 'com.android.application' 之后添加以下行:

    repositories{
         jcenter()
    }
    
  3. android 节内添加以下行:

    packagingOptions {
         pickFirst 'META-INF/ASL2.0'
         pickFirst 'META-INF/LICENSE'
         pickFirst 'META-INF/NOTICE'
    }
    
  4. dependencies 节内添加以下行:

    compile group: 'com.ibm.mobile.foundation',
         name: 'ibmmobilefirstplatformfoundation',
         version: '8.0.+',
         ext: 'aar',
         transitive: true
    

    或者在单行上:

    compile 'com.ibm.mobile.foundation:ibmmobilefirstplatformfoundation:8.0.+'
    
  5. Android → 应用程序 → 清单中,打开 AndroidManifest.xml 文件。 在 application 元素上方添加以下许可权:

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
    
  6. 将 MobileFirst UI 活动添加到现有 activity 元素旁:

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

如果显示“Gradle 同步”请求,接受该请求。

手动添加 MobileFirst 本机 SDK

您还可以手动添加 MobileFirst SDK:

要手动添加 MobileFirst SDK,请先通过 MobileFirst Operations Console → 下载中心 → SDK 选项卡下载 SDK .zip 文件。 在完成上面的步骤之后,也遵循下面的信息。

  • 解压缩下载的 .zip 文件并将相关的 aar 文件放置到 app\libs 文件夹中。
  • 将以下内容添加到 dependencies 结尾:
    compile(name:'ibmmobilefirstplatformfoundation', ext:'aar')
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'   
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
  • 将以下内容添加到 repositories 结尾:
    repositories {
        flatDir {
            dirs 'libs'
        }
    }

注册应用程序

  1. 打开命令行窗口并浏览至 Android Studio 项目的根目录。

  2. 运行以下命令:

     mfpdev app register
    

mfpdev app register CLI 命令先连接到 MobileFirst Server 以注册应用程序,之后在 Android Studio 项目的 [project root]/app/src/main/assets/ 文件夹中生成 mfpclient.properties 文件,然后向其添加标识 MobileFirst Server 的元数据。

提示:您还可以从 MobileFirst Operations Console 注册应用程序:

  1. 装入 MobileFirst Operations Console。
  2. 单击应用程序旁边的新建按钮以注册新应用程序,并遵循屏幕上的指示信息。
  3. 注册应用程序之后,浏览至应用程序的配置文件选项卡,然后复制或下载 mfpclient.properties 文件。 遵循屏幕上指示信息将此文件添加到您的项目。

创建 WLClient 实例

在使用任何 MobileFirst API 之前,请先创建 WLClient 实例:

WLClient.createInstance(this);

注:在整个应用程序生命周期中只应创建一次 WLClient 实例。 建议使用 Android 应用程序类来创建。

更新 MobileFirst 本机 SDK

要使用最新发行版更新 MobileFirst 本机 SDK,请在 build.gradle 文件中找到发行版版本号并相应地更新 version 属性。
请参阅上面的步骤 4。

可以在 SDK 的 JCenter 存储库中找到 SDK 发行版。

已生成 MobileFirst 本机 SDK 工件

mfpclient.properties

此文件位于 Android Studio 项目的 ./app/src/main/assets/ 文件夹中,可定义用于在 MobileFirst Server 上注册 Android 应用程序的客户机端属性。

| 属性 | 描述 | 示例值 | |———————|———————————————————————|—————-| | wlServerProtocol | 与 MobileFirst Server 的通信协议。 | http 或 https | | wlServerHost | MobileFirst Server 的主机名。 | 192.168.1.63 | | wlServerPort | MobileFirst Server 的端口。 | 9080 | | wlServerContext | MobileFirst Server 上应用程序的上下文根路径。 | /mfp/ | | languagePreferences | 为客户机 sdk 系统消息设置缺省语言。 | zh |

对 Javadoc 和 Android 服务的支持

有关对 Javadoc 和 Android 服务的支持的信息,请参阅其他信息页面。

接下来要学习的教程

集成 MobileFirst 本机 SDK 之后,您现在可以:

Inclusive terminology note: The Mobile First Platform team is making changes to support the IBM® initiative to replace racially biased and other discriminatory language in our code and content with more inclusive language. While IBM values the use of inclusive language, terms that are outside of IBM's direct influence are sometimes required for the sake of maintaining user understanding. As other industry leaders join IBM in embracing the use of inclusive language, IBM will continue to update the documentation to reflect those changes.
Last modified on June 01, 2020