Android SDK Integration Guide

Use Suggestions

This article is an integration guide document for the JMessage Android SDK standard, which is used to guide the use of the SDK. The default reader is developers who are already familiar with the basic usage of the IDE (Eclipse or Android Studio) and have a certain knowledge base of Android programming.

JMessage Android SDK version matched with this guide matches is: 2.0.0 and later.

  • The JIGUANG Documentation has all the JMessage-related guides, APIs, tutorials, and all other documents. Updated versions of this document will be posted to the site in a timely manner.
  • If you see this document but have not yet downloaded the Android SDK, please visit the SDK download page to download it.

Brief introduction of JMessage SDK package

  • demo/
    • A demo application to demonstrate the basic usage of the JMessage SDK interface
  • doc/
    • Java doc for all external interfaces contained in the JMessage sdk.
  • libs/jcore-android_v1.X.Y.jar
    • The core package of Jiguang Developer Services.
  • libs/jmessage-android-2.X.Y.jar
    • JMessage SDK Development Kit
  • libs/(cpu-type)/libjcore1xy.so
    • Native development kits for various CPU types.

Android SDK version

At present, the SDK only supports Android 2.3 or later mobile phone systems.

Automatic Integration Steps of jcenter

Instructions: JMessage supports jcenter automatic integration since version 2.1.2. Developers who use jcenter automatic integration do not need to add jar and so in the project, and jcenter will automatically complete the dependency; he does nor need to add any configuration related to JPush SDK in AndroidManifest.xml neither, instead, jcenter will automatically import them.

  • If the developer needs to modify component properties, he can define the component with the same name in the local AndroidManifest and configure the desired properties. Then use xmlns:tools to control the local component to override the component on jcenter. Example:
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2. package="com.android.tests.flavorlib.app"
  3. xmlns:tools="http://schemas.android.com/tools">
  4. <application
  5. android:icon="@drawable/icon"
  6. android:name="com.example.jpushdemo.ExampleApplication"
  7. android:label="@string/app_name" >
  8. <service android:name="cn.jpush.android.service.PushService"
  9. android:process=":multiprocess"
  10. tools:node="replace" >
  11. ……
  12. </service>
  13. ……
  14. </application>
  15. ……
  16. </manifest>
  • Confirm that jcenter support is configured in the main gradle of the android studio’s Project root directory. (Support if new project defaults the configuration)
  1. buildscript {
  2. repositories {
  3. jcenter()
  4. }
  5. ......
  6. }
  7. allprojets {
  8. repositories {
  9. jcenter()
  10. }
  11. }
  • Add substitution variables of dependency and AndroidManifest in gradle of module
  1. android {
  2. ......
  3. defaultConfig {
  4. applicationId "com.xxx.xxx" //JPush上注册的包名.
  5. ......
  6. ndk {
  7. //选择要添加的对应cpu类型的.so库。
  8. abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
  9. // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
  10. }
  11. manifestPlaceholders = [
  12. JPUSH_PKGNAME : applicationId,
  13. JPUSH_APPKEY : "你的appkey", //JPush上注册的包名对应的appkey.
  14. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
  15. ]
  16. ......
  17. }
  18. ......
  19. }
  20. dependencies {
  21. ......
  22. compile 'cn.jiguang.sdk:jmessage:2.5.0' // 此处以JMessage 2.5.0 版本为例。
  23. compile 'cn.jiguang.sdk:jcore:1.1.9' // 此处以JCore 1.1.9 版本为例。
  24. ......
  25. }

Note: If android Studio displays the following prompt after adding the above abiFilter configuration:

  1. NDK integration is deprecated in the current plugin. Consider trying the new experimental plugin.

Then add the below in the gradle.properties file of the Project root directory

  1. android.useDeprecatedNdk=true

Manually Integration Steps

  • Unzip the jmessage-sdk-android-2.X.Y.zip integrated archive.
  • Copy libs/jcore-android_v1.X.Y.jar to the project libs/ directory.
  • Copy libs/jmessage-android_2.X.Y.jar to the project libs/ directory.
  • Copy libs/(cpu-type)/libjcore1xy.so to the corresponding cpu type directory of your project

Instruction: For the developer using android studio, if he uses the jniLibs folder to import the so file, he only needs to copy all the cpu type folder into; if the so file is added in the module’s libs folder, pay attention to add the following configuration in the gradle configuration of module:

  1. android {
  2. ......
  3. sourceSets {
  4. main {
  5. jniLibs.srcDirs = ['libs']
  6. ......
  7. }
  8. ......
  9. }
  10. ......
  11. }

Configure AndroidManifest.xml

Configure the AndroidManifest.xml of the application project according to the AndroidManifest.xml sample file in the demo of the SDK archive.

The main steps are:

  • Copy the part with the comment “Required”
  • Replace the section labeled “package name of your application” with the current application package name
  • Replace the section labeled “AppKey for your app” with the key for registering the app on Portal, for example: 9fed5bcb7b9b87413678c407

Tips

If android studio is used, the value of applicationId can be referenced in the AndroidManifest and configured under the defaultConfig node in the build.gradle configuration. For example:

  1. defaultConfig {
  2. applicationId "cn.jmessage.example" // <--您应用的包名
  3. ……
  4. }

Use ${applicationId} in AndroidManifest to refer to the package name defined in gradle

AndroidManifest example

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest package="您自己的包名"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:versionCode="2"
  5. android:versionName="1.0.1">
  6. <permission
  7. android:name="您自己的包名.permission.JPUSH_MESSAGE"
  8. android:protectionLevel="signature"/>
  9. <!-- Required -->
  10. <uses-permission android:name="您自己的包名.permission.JPUSH_MESSAGE" />
  11. <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
  12. <uses-permission android:name="android.permission.INTERNET" />
  13. <uses-permission android:name="android.permission.WAKE_LOCK" />
  14. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  15. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  16. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  17. <uses-permission android:name="android.permission.VIBRATE" />
  18. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  19. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  20. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  21. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  22. <!-- Optional. Required for location feature -->
  23. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  24. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  25. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  26. <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
  27. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  28. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  29. <uses-permission android:name="android.permission.GET_TASKS" />
  30. <application
  31. android:name="Your Application Name"
  32. android:icon="@mipmap/ic_launcher"
  33. android:label="@string/app_name">
  34. <!-- Required SDK 核心功能-->
  35. <!-- 可配置android:process参数将PushService放在其他进程中 -->
  36. <service
  37. android:name="cn.jpush.android.service.PushService"
  38. android:enabled="true"
  39. android:exported="false">
  40. <intent-filter>
  41. <action android:name="cn.jpush.android.intent.REGISTER" />
  42. <action android:name="cn.jpush.android.intent.REPORT" />
  43. <action android:name="cn.jpush.android.intent.PushService" />
  44. <action android:name="cn.jpush.android.intent.PUSH_TIME" />
  45. </intent-filter>
  46. </service>
  47. <!-- Required SDK核心功能-->
  48. <receiver
  49. android:name="cn.jpush.android.service.PushReceiver"
  50. android:enabled="true"
  51. android:exported="false">
  52. <intent-filter android:priority="1000">
  53. <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED_PROXY" />
  54. <category android:name="您自己的包名" />
  55. </intent-filter>
  56. <intent-filter>
  57. <action android:name="android.intent.action.USER_PRESENT" />
  58. <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
  59. </intent-filter>
  60. <!-- Optional -->
  61. <intent-filter>
  62. <action android:name="android.intent.action.PACKAGE_ADDED" />
  63. <action android:name="android.intent.action.PACKAGE_REMOVED" />
  64. <data android:scheme="package" />
  65. </intent-filter>
  66. </receiver>
  67. <!-- Required SDK核心功能-->
  68. <receiver
  69. android:name="cn.jpush.android.service.AlarmReceiver"
  70. android:exported="false" />
  71. <!-- Required since JCore 1.1.7. SDK 核心功能-->
  72. <provider
  73. android:name="cn.jpush.android.service.DataProvider"
  74. android:authorities="您自己的包名.DataProvider"
  75. android:exported="true" />
  76. <!-- Required JMessage SDK核心功能-->
  77. <receiver
  78. android:name="cn.jpush.im.android.helpers.IMReceiver"
  79. android:enabled="true"
  80. android:exported="false">
  81. </receiver>
  82. <!-- Required. For publish channel feature -->
  83. <!-- JPUSH_CHANNEL 是为了方便开发者统计APK分发渠道。-->
  84. <!-- 例如: -->
  85. <!-- 发到 Google Play 的APK可以设置为 google-play; -->
  86. <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
  87. <!-- 目前这个渠道统计功能的报表还未开放。-->
  88. <meta-data
  89. android:name="JPUSH_CHANNEL"
  90. android:value="developer-default" />
  91. <!-- Required. AppKey copied from Portal -->
  92. <meta-data
  93. android:name="JPUSH_APPKEY"
  94. android:value="您自己的appkey" />
  95. </application>
  96. </manifest>

Required permissions description

Authority Use
Your Package.permission.JPUSH_MESSAGE Officially defined permissions allow the application to receive broadcast messages sent by the JPUSH internal code.
RECEIVE_USER_PRESENT Allows the app to receive lighted screens or unlock broadcasts.
INTERNET Allows apps to access the web.
WAKE_LOCK Allows the app to run after the phone screen is turned off
READ_PHONE_STATE Allows the app to access the phone’s status.
WRITE_EXTERNAL_STORAGE Allows the application to write to external storage.
READ_EXTERNAL_STORAGE Allows the app to read external storage.
WRITE_SETTINGS Allows the application to read and write system settings.
VIBRATE Allows the application to vibrate.
MOUNT_UNMOUNT_FILESYSTEMS Allows the app to mount/unmount external file systems.
ACCESS_NETWORK_STATE Allows the app to obtain network information status, such as whether the current network connection is valid.

Add code

Basic API

  • setDebugMode set debug mode
  1. // You can enable debug mode in developing state. You should close debug mode when release.
  2. JMessageClient.setDebugMode(boolean debugEnalbed)
  • init initialization SDK
  1. JMessageClient.init(Context context)

Call sample code (see example project)

  • init only needs to be called once at application startup

  • The following code customizes an Application class, and needs to be configured in AndoridManifest.xml. Please refer to the AndroidManifest.xml fragment above, or the demo project.

  1. public class IMDebugApplication extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. JMessageClient.setDebugMode(true);
  6. JMessageClient.init(this);
  7. }
  8. }

Test confirmation

  • Verify that the required permissions have been added. If the necessary permissions are not added, the log will indicate an error.
  • Verify that the AppKey (generated on the Portal) has been correctly written to Androidmanifest.xml.
  • Verify that the init(context) interface is called when the program starts
  • Verify that the test phone (or emulator) has successfully connected to the network + client shortly after the init invoked. If everything is ok, there should be log information for the long connection of logging in successfully. The log information will be as follows:
  1. [JMessageClient] JMessage SDK init finished!
  2. .......
  3. [ConnectingHelper] Login succeed!

As shown in the figure, the client startup is divided into 4 steps:

  • Check the appKey and channel of metadata. If it does not exist, the startup fails
  • Initialize the JMessage SDK to check the validity of library files such as JNI. If the library file is invalid, the startup fails.
  • Check Androidmanifest.xml. If permissions for Required do not exist, the startup fails.
  • Connect server to login in. If there is a network problem, the long connection login fails, or if there are problems in the first three steps, JMessage SDK won’t be initiated.

Points to Note When Integrating

Since the structure of the jar package has changed from the JMessage 2.0.0 version, there are some considerations for integration developers need to pay attention to

If you previously integrated JMessage

For developers who previously integrated versions before JMessage 2.0.0, the previous JMessage contained the full functionality of Push, so he only needs to integrates JMessage to obtain the full functionality of both JMessage and JPush.

The new JMessage 2.0.0 will no longer include the functionality of JPush. JMessage and JPush will be integrated separately as two relatively independent modules in the future. Therefore, for developers who have integrated JMessage (pre-2.0.0 version) before, if he needs to use JPush related functions after updating JMessage to 2.0.0, please refer to the JPush3.0.0 Integration Document to integrate JPush into the project.


Points to note when manually integrating JPush based on JMessage:

  • Version requirements: The corresponding JCore requires version 1.1.0 or later, and JPush requires version 3.0.0 or later.
  • Replacement of jcore: The downloaded JPush SDK zip package also contains a jar package named jcore-android_v1.XY. When integrating, you need to pay attention to only save one jcore jar in the project. If the version of the jcore jar package in JPush and JMessage is inconsistent, then just save the updated version. So file likewise.
  • Manifest configuration: For configuration of the necessary components in the manifest, since manifest examples of JMessage and JPush contain some common component configurations, developers who have integrated JMessage 2.0.0 (and above), only need to copy the component configuration of JPush part, including (but not limited to the following components, may vary according to the later JPush version, please refer to the Integration Document of JPush3.0.0):

    • cn.jpush.android.service.DaemonService
    • cn.jpush.android.ui.PushActivity
    • cn.jpush.android.service.DownloadService
    • cn.jpush.android.ui.PopWinActivity
    • custom broadcast receiver of JPush user (if needed)
  • Don’t forget to add the res resource in the JPush SDK

  • Plus initial code of JPush: JPushInterface.init(context)

If you previously integrated JPush

If developers who have integrated JPush 3.0.0 or above,need the IM function, he can directly configure the JMessage by referring to the above “jcenter automatic integration procedure” or “manual integration procedure” section, among which the following items need be pay attention to:


Points to note when manually integrating JMessage based on JPush:

  • Version requirements: JCore requires version 1.1.0 or later, and the corresponding JMessage requires version 2.0.0 or later.
  • Replacement of jcore: The downloaded JMessage SDK zip package also contains a jar package named jcore-android_v1.XY. When integrating, you need to pay attention to only save one jcore jar in the project. If the version of the jcore jar package in JPush and JMessage is inconsistent, then just save the updated version. So file likewise.
  • Configuration of Manifest: Because manifest examples of JMessage and JPush contain some common component configurations, developers who already integrate JPush 3.0.0 (and above), just need to copy the component configuration of the JMessage section, which includes (but not limited to the following components, may change according to the JMessage after the version changes, specifically refer to the “AndroidManifest example” above)

    • cn.jpush.im.android.helpers.IMReceiver
  • Plus initialization code of JMessage: JMessageClient.init(context)

JMessage Obfuscation

  • Please download the 4.x version of proguard.jar and replace your Android Sdk “tools\proguard\lib\proguard.jar”

  • Add the following configuration to the project’s obfuscated file

  1. -dontoptimize
  2. -dontpreverify
  3. -keepattributes EnclosingMethod,Signature
  4. -dontwarn cn.jpush.**
  5. -keep class cn.jpush.** { *; }
  6. -dontwarn cn.jiguang.**
  7. -keep class cn.jiguang.** { *; }
  8. -keepclassmembers class ** {
  9. public void onEvent*(**);
  10. }
  11. #========================gson================================
  12. -dontwarn com.google.**
  13. -keep class com.google.gson.** {*;}
  14. #========================protobuf================================
  15. -keep class com.google.protobuf.** {*;}

Sample of IM Scene Code

JMessage provides a complete JChat application under the IM scenario. It is an IM App. If you only need IM features, you can turn it into your own IM App with the following two changes:

  • Change Logo
  • Register your app on the JPush web console and update Appkey obtained to JChat

JChat Android Project Source Code, open source on Github, for your reference.

Technical Support

E-mail us at: support@jiguang.cn