Android JShare Integration Guide

Use Suggestions

This article is a standard integration guide document for the JShare Android SDK. The matching SDK version is V1.0.0 and later.

  • If you want a quick test, please refer to this article to run through the Demo within minutes.
  • All documents, including all guides, APIs, and tutorials, are available on the Jiguang Doc website. Updated versions of this document will be posted to the site in a timely manner.
  • Jiguang Community Website: If you have doubts about the documents and problems with the products, you can ask questions in the Jiguang community and you will receive respond in a timely manner.

Description of Product Function

JShare SDK allows your application to support multi-platform sharing. It does not take time to understand and integrate the SDK of each social sharing platform, which can effectively reduce the package size.

Main Scenes:

  • Share the sharing content to major social platforms such as QQ, WeChat, Sina Weibo, Facebook, and Twitter.
  • Access to key platforms such as QQ, WeChat, Sina Weibo, Facebook and Twitter.
  • Access personal information on QQ, WeChat, Sina Weibo, Facebook, Twitter and other major platforms for third party login.

Content of jshare-android-1.x.y-release.zip

  • JGShareSDK.xml o Reference file embedded SDK configuration of each platform in client
  • AndroidManifest.xml o Configuration file embedded SDK reference in client.
  • libs/jcore-android-1.x.y.jar o Core package of Jiguang Developer Services
  • jshare-android-1.x.y.jar o JShare SDK core package
  • jshare-wechat-android-1.x.y.jar o JShare WeChat platform packagge
  • jshare-qq-android-1.x.y.jar o JShare QQ platform package
  • jshare-sina-android-1.x.y.jar o JShare Sina Weibo package
  • jshare-facebook-android-1.x.y.jar o JShareFacebook platform package
  • jshare-twitter-android-1.x.y.jar o JShare Twitter platform package
  • libs/(cpu-type)/libjcore1xy.so o Native development kits for various CPU types.
  • example o It is a complete Android project that demonstrates the basic usage of the JShare SDK and can be used as a reference.

Android SDK Version

JShare SDK supports Android 2.3 and above Android systems.

Jcenter Automatic Integration

Note: Developers who use jcenter auto-integration do not need to add jar and so in the project. Jcenter will automatically complete the dependencies.

  • Configure jcenter in gradle.
  • Configure third-party platform information.
  • Configure WeChat callbacks (if you do not need to share with WeChat, skip it).
  • Configure project signatures.
  • • Refer to the example project or interface documentation to use the JShare SDK.

Configuration of gradle

  • Verify that jcenter support is configured in the main gradle of the android studio’s Project root. (Default configuration of new project is supported) ``` buildscript { repositories {
    1. jcenter()
    } …… }

allprojects { repositories { jcenter() } }

  1. * module gradle 中添加依赖和 AndroidManifest 的替换变量。

android { …… defaultConfig { applicationId “com.xxx.xxx” //极光控制台创建应用时填写的应用包名. ……

  1. ndk {
  2. //选择要添加的对应 cpu 类型的.so 库。
  3. abiFilters 'armeabi', 'armeabi-v7a', 'armeabi-v8a'
  4. // 还可以添加 'x86', 'x86_64', 'mips', 'mips64'
  5. }
  6. manifestPlaceholders = [
  7. JSHARE_PKGNAME : applicationId,
  8. JPUSH_APPKEY : "你的 appkey", //极光控制台创建应用得到的 AppKey.
  9. JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
  10. TENCENT_APPID: " QQ 开发者应用的 appID ",//腾讯开放平台注册应用得到的 appId
  11. FACEBOOK_APPID: " facebook 开发者应用的 appID ",// facebook 注册应用得到的 appId
  12. ]
  13. ......
  14. }
  15. ......

} dependencies { …… compile ‘cn.jiguang.sdk:jshare:1.5.0’ // 此处以 JShare 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jshare-qqmodel:1.5.0’ // 此处以 jshare-qqmodel 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jshare-wechatmodel:1.5.0’ // 此处以 jshare-wechatmodel 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jshare-sinamodel:1.5.0’ // 此处以 jshare-sinamodel 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jshare-facebookmodel:1.5.0’ // 此处以 jshare-facebookmodel 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jshare-twittermodel:1.5.0’ // 此处以 jshare-twittermodel 1.5.0 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 compile ‘cn.jiguang.sdk:jcore:1.1.7’ // 此处以 JCore 1.1.7 版本为例,具体版本请参考压缩包 libs 的 jar 包版本。 …… }

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

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

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

android.useDeprecatedNdk=true

  1. ## Manual Integration Steps
  2. * Unzip jshare-android-1.x.y-release.zip
  3. * Copy libs/jcore-android-1.x.y.jar to the libs directory of project.
  4. * Copy libs/jshare-android-1.x.y.jar to the libs directory of project.
  5. * Copy libs/(cpu-type)/libjcore1xy.so to the directory where your project stored the corresponding cpu type.
  6. * Copy the jar package of libs/jshare-xx.jar platform to the libs directory of project as needed.
  7. * Configure AndroidManifest.xml as described below.
  8. * Configure third-party platform information.
  9. * Configure WeChat callbacks (if you do not need to share with WeChat, skip it).
  10. * Configure project signatures
  11. * Refer to example project or interface documentation to use the JShare SDK.
  12. **Note:** Developers using android studio, if using the jniLibs folder to import so files, he only needs to copy in all cpu type folders; if so files are added to libs folder of the module, pay attention to add the following configuration in the gradle configuration of the module:

android { …… sourceSets { main { jniLibs.srcDirs = [‘libs’] …… } …… } …… }

  1. ### Configure AndroidManifest.xml
  2. Configure the AndroidManifest.xml of the application project according to the AndroidManifest.xml sample file in the SDK archive.
  3. * Copy the part with the comment "Required"
  4. * Replace the section labeled "Package name of your application" with the current application package
  5. * Replace the section labeled "AppKey for your application" with the key for registering the application on the portal, for example: 9fed5bcb7b9b87413678c407
  6. #### Example of AndroidManifest

<?xml version=”1.0” encoding=”utf-8”?>

  1. <!-- Required -->
  2. <uses-permission android:name="android.permission.RECEIVE_USER_PRESENT" />
  3. <uses-permission android:name="android.permission.INTERNET" />
  4. <uses-permission android:name="android.permission.WAKE_LOCK" />
  5. <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  6. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  7. <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  8. <uses-permission android:name="android.permission.VIBRATE" />
  9. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  10. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  11. <uses-permission android:name="android.permission.WRITE_SETTINGS" />
  12. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
  13. <!-- Optional. Required for location feature -->
  14. <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  15. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  16. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
  17. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  18. <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
  19. <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
  20. <uses-permission android:name="android.permission.GET_TASKS" />
  21. <application
  22. android:name=".MyApplication"
  23. android:allowBackup="true"
  24. android:icon="@mipmap/ic_launcher"
  25. android:label="@string/app_name"
  26. android:supportsRtl="true"
  27. android:theme="@style/AppTheme">
  28. <activity android:name=".MainActivity">
  29. <intent-filter>
  30. <action android:name="android.intent.action.MAIN" />
  31. <category android:name="android.intent.category.LAUNCHER" />
  32. </intent-filter>
  33. </activity>
  34. <activity android:name=".SelectPlatActivity"/>
  35. <activity android:name=".ShareTypeActivity"/>
  36. <!-- Required SDK 核心功能-->
  37. <activity
  38. android:name="cn.jiguang.share.android.ui.JiguangShellActivity"
  39. android:configChanges="keyboardHidden|orientation|screenSize"
  40. android:exported="true"
  41. android:launchMode="singleTask"
  42. android:screenOrientation="portrait"
  43. android:theme="@android:style/Theme.Translucent.NoTitleBar"
  44. android:windowSoftInputMode="stateHidden|adjustResize">
  45. <!-- Optional QQ 分享回调-->
  46. <!-- scheme为“tencent” 前缀再加上 QQ 开发者应用的 appID;例如 appID为123456,则 scheme=“tencent123456” -->
  47. <intent-filter>
  48. <data android:scheme="tencent+appID" />
  49. <action android:name="android.intent.action.VIEW" />
  50. <category android:name="android.intent.category.BROWSABLE" />
  51. <category android:name="android.intent.category.DEFAULT" />
  52. </intent-filter>
  53. <!-- Optional 新浪微博分享回调 -->
  54. <intent-filter>
  55. <action android:name="com.sina.weibo.sdk.action.ACTION_SDK_REQ_ACTIVITY" />
  56. <category android:name="android.intent.category.DEFAULT" />
  57. </intent-filter>
  58. <!-- Optional 新浪微博私信回调 -->
  59. <intent-filter>
  60. <action android:name="android.intent.action.VIEW" />
  61. <category android:name="android.intent.category.DEFAULT" />
  62. <category android:name="android.intent.category.BROWSABLE" />
  63. <data android:scheme="jsharesdk" android:host="sinaweibo"/>
  64. </intent-filter>
  65. </activity>
  66. <!-- Optional 微信分享回调,wxapi 必须在包名路径下,否则回调不成功 -->
  67. <activity
  68. android:name=".wxapi.WXEntryActivity"
  69. android:theme="@android:style/Theme.Translucent.NoTitleBar"
  70. android:exported="true" />
  71. <!-- Optional facebook 配置,authorities 必须为 com.facebook.app.FacebookContentProvider+APP_ID -->
  72. <provider
  73. android:authorities="com.facebook.app.FacebookContentProvider 您申请的 facebook 的 AppId"
  74. android:name="cn.jiguang.share.facebook.FacebookContentProvider"
  75. android:exported="true"
  76. />
  77. <!-- User defined. For test only 用户自定义的广播接收器 -->
  78. <receiver android:name="cn.jiguang.share.demo.FaceBookUploadReceiver">
  79. <intent-filter>
  80. <action android:name="com.facebook.platform.AppCallResultBroadcast" />
  81. </intent-filter>
  82. </receiver>
  83. <!-- Required. For publish channel feature -->
  84. <!-- JPUSH_CHANNEL 是为了方便开发者统计 APK 分发渠道。-->
  85. <!-- 例如: -->
  86. <!-- 发到 Google Play 的 APK 可以设置为 google-play; -->
  87. <!-- 发到其他市场的 APK 可以设置为 xxx-market。 -->
  88. <!-- 目前这个渠道统计功能的报表还未开放。-->
  89. <meta-data
  90. android:name="JPUSH_CHANNEL"
  91. android:value="developer-default" />
  92. <!-- Required. AppKey copied from Portal -->
  93. <meta-data
  94. android:name="JPUSH_APPKEY"
  95. android:value="您应用的 Appkey" />
  96. </application>

  1. ## Configure Third-party Platform Information
  2. The 1.5.0 release supports two methods for configuring third-party platform information
  3. 1 Configure JGShareSDK.xml
  4. 2 Set in the code. Developers should choose one of the methods. Versions prior to 1.5.0 only supports the configuration of JGShareSDK.xml.
  5. ### JGShareSDK.xml configures third-party platform information
  6. Whether you use automatic integration or manual integration, you need to configure JGShareSDK.xml. The main steps are
  7. * Copy or create JGShareSDK.xml into the asset directory of the project directory.
  8. * Replace the relevant AppKeys and AppSecrets in JGShareSDK.xml with the information obtained by your own applications created on third-party platforms.
  9. * Configure each platform as needed. Unwanted platforms can be deleted.
  10. #### Example of JGShareSDK.xml

<?xml version=”1.0” encoding=”utf-8”?>

  1. <!-- 如果不需要支持某平台,可缺省该平台的配置-->
  2. <SinaWeibo
  3. AppKey="新浪微博的 AppKey"
  4. AppSecret="新浪微博 AppSecret"
  5. RedirectUrl="微博开放平台填写的授权回调页"/>
  6. <QQ
  7. AppId="QQ 的 AppId"
  8. AppKey="QQ 的 AppKey"/>
  9. <Wechat
  10. AppId="微信的 AppId"
  11. AppSecret="微信的 AppSectet"/>
  12. <Facebook
  13. AppId="facebook 的 appId"
  14. AppName="facebook 后台填写的名称"
  15. />
  16. <Twitter
  17. ConsumerKey="twitter 的 ConsumerKey"
  18. ConsumerSecret="twitter 的 ConsumerSecret"
  19. />

  1. ### Code sets third-party platform information
  2. For details, see [SDK Initialization API](https://docs.jiguang.cn/jshare/client/Android/android_api/#api_1) and [Third-Party Platform Information Settings API](https://docs.jiguang.cn/jshare/client/Android/android_api/#api_2);
  3. ## Configure Callbacks of WeChat Platform
  4. * Create a wxapi directory in the corresponding directory of your package name, and add a WXEntryActivity class under the wxapi directory. This class inherits from WeChatHandleActivity (For example, if the package name of application is cn.jiguang.share.demo, the new class added is shown as below)
  5. ![](http://i.imgur.com/2URxXFr.png)
  6. **Note:** If you overwrite the onCreate method, onNewIntent method, you must call the parent method, otherwise you cannot get the result of sharing, for example

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }

@Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); }

  1. * Add exported attribute in the manifest file with the set to true, for example

  1. ## Configure Facebook Platform
  2. * Add the ContentProvider configuration of Facebook in the manifest file

  1. **Note:** The authorities of provider must be "com.facebook.app.FacebookContentProvider"+"AppId"
  2. * If need to get the results of uploaded pictures and videos on facebook, you can be customize BroadCastReceiver, inherit FacebookBroadcastReceiver, and overwrite onSuccessfulAppCall, onFailedAppCall method

  1. **Note:** The action of the receiver must be "com.facebook.platform.AppCallResultBroadcast"
  2. ## Configure Project Signature
  3. ### Signature Configuration of Android Studio Graphical Interface
  4. Enter Project Structure and select your project to integrate JShare. See specific configuration as below
  5. ![](http://i.imgur.com/ahk1DoN.png)
  6. ![](http://i.imgur.com/2oh4IKp.png)
  7. ### Manual Configuration of Android Studio
  8. * Add signature configuration inside the android of project's build.gradle, for example:

signingConfigs { debug { storeFile file(“jshare.jks”) //签名文件路径 storePassword “sdkteam” keyAlias “jshare” keyPassword “sdkteam” //签名密码 } release { storeFile file(“jshare.jks”) //签名文件路径 storePassword “sdkteam” keyAlias “jshare” keyPassword “sdkteam” //签名密码 } }

  1. * Then use the signature configuration in buildTypes of the project's build.gradle , for example:

buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-project.txt’ signingConfig signingConfigs.debug } debug { signingConfig signingConfigs.debug } }

  1. ### Environment Configuration of Eclipse
  2. * In the Preferences of Eclipse, select Android -> Build, as shown below::
  3. ![](http://i.imgur.com/mKPb3De.png)
  4. * Specify the path of the Custom debug keystore option as the debug.keystore file in the directory of sdk demo project and apply the configuration as shown below:
  5. ![](http://i.imgur.com/TgxykaK.png)
  6. ### Note
  7. * The application package name, application signature, AppID and AppKey registered by the third-party platform must be in one-to-one correspondence. Otherwise, sharing will fail.
  8. * The signature of the application must correspond to the signature filled in on the third-party platform, otherwise it will not be shared.
  9. * Sina Weibo supports sharing without installing client. Note that RedirectUrl in JGShareSDK.xml must be consistent with the information on Weibo open platform. Otherwise, an error will occur.
  10. ## Add Code
  11. The API interfaces provided by the JShare SDK are mainly focused on the cn.jiguang.share.android.api.JShareInterface class. For usage, refer to the example or the API interface documentation.
  12. ### Basic API
  13. * init initialization SDK

public static void init(Context context)

  1. * init initialization SDK. Versions after 1.5.0 support setting third-party platform information in the code

public static void init(Context context, PlatformConfig platformConfig)

  1. * setDebugMode

public static void setDebugMode(boolean enable)

  1. Note: This interface needs to be called before the init interface to avoid the situation where some logs are not printed. In the case of multi-process, it is recommended to call in onCreate in the custom Application.
  2. ### Test Confirmation
  3. * Confirm that the required files have been added to the project.
  4. * Confirm that Androidmanifest.xml is properly configured.
  5. * Confirm that the third-party platform information is correctly configured.
  6. * Determine which platform is configured based on the following logs.
  7. * Confirm that the application's package name and signature are the same as the information filled in the third-party background.

[PlatformManager] platform Wechat has configured [PlatformManager] platform SinaWeibo has configured [PlatformManager] platform QQ has configured [PlatformManager] platform Facebook has configured [PlatformManager] platform Twitter has configured

  1. **Note:** If configuration of a platform fails, there will be log information, for example:

[PlatformManager] QQ configure fail, please check project config: make sure jshare-qq-android-v.x.y.jar has build in your project.

  1. ## Obfuscation Configuration

-dontwarn cn.jiguang. -keep class cn.jiguang. { ; } -dontwarn cn.jpush. -keep class cn.jpush. { ; } -keep public class com.sina.* { ; } ```