Problems and solutions encountered in Android development using Bmob

Recently, I had a task to use Android to connect to Bmob. During the process, I encountered many problems. I summarized this article by reading the problem solutions of various bloggers, and just regard it as my own study notes.

1. The official recommendation is to import the SDK automatically.

existappofbuild.gradleaddition in sentence sentence依赖文件

dependencies {
    implementation 'io.github.bmob:android-sdk:3.8.20'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
    implementation 'com.squareup.okhttp3:okhttp:4.8.1'
    implementation 'com.squareup.okio:okio:2.2.2'
    implementation 'com.google.code.gson:gson:2.8.5'
}

Note:

[1], starting from v3.6.8-rc2, the data SDK in the remote warehouse includes libbmob.so and the resource files required for automatic update components. Developers no longer need to configure libbmob.so, add dependent jars, or copy resource files for automatically updated components. They only need to add the following dependencies.

[2]. Each version of im depends on a specific version of bmob-sdk:

  • bmob-im:1.1.8--->bmob-sdk:3.3.5
  • bmob-im:1.1.9--->bmob-sdk:3.4.3
  • bmob-im:2.0.1--->bmob-sdk:3.4.6-0304
  • bmob-im:2.0.2--->bmob-sdk:3.4.6-0304
  • bmob-im:2.0.3--->bmob-sdk:3.4.6
  • bmob-im:2.0.4--->bmob-sdk:3.4.6
  • bmob-im:2.0.5--->bmob-sdk:3.4.7-aar
  • bmob-im:2.0.6--->bmob-sdk:3.5.0

in

  • bmob-sdk:3.4.6-0304It is a transitional version of Bmob Android SDK, mainly used for NewIM_v2.0.1 and v2.0.2
  • bmob-sdk:3.4.6The relevant dependency packages can be seen in the comments [3]

[3], bmob-sdk:3.4.6 depends on the following packages:

implementation 'cn.bmob.android:bmob-sdk:3.4.6'
implementation 'com.squareup.okhttp:okhttp:2.4.0'//CDN文件服务使用okhttp相关包进行文件的上传和下载(必填)
implementation 'com.squareup.okio:okio:1.4.0'

[4], bmob-sms is suitable for developers who only need to use the Bmob SMS function, and bmob-sdk contains the SMS function of bmob-sms, please do not add it repeatedly.

[5], BmobSDK's official repository:bmob-android-sdk, developers can go to this repository to view the latest versions. SDK, we will try our best to keep updated with the SDK released on the official website.

 

2. Manual import

        Manual import method: Download the Android version of the SDK for data services in the Bmob SDK Download Center.

                 Because the official website download requires entering GitHub, some students may not be able to enter. Here I will share my Baidu network disk. If you want

                 The official SDK can be obtained by yourself:

                 Link: https://pan.baidu.com/s/1mc1p-pgu8pfdt3xlfvj8fa?pwd=r4so  
                 Extract code: R4SO 
--Sharing from Baidu Netdisk super member V1

        After downloading, copy the contents of the libs folder in the locally imported SDK folder to the libs directory in the project.

7a18538227d646a4b0905a51c134ee21.png

26620d594c3d428ca97e343f05e3ef2f.png

The androidx-core-1.0.0 marked in the picture reports an error after importing. The error information is all about androidx-core-1.0.0. It should conflict with the existing version, so there is no need to import this. No error is reported after okio-2.1.0 is imported, but the activity that operates with bmob data will crash, so the okio-2.1.0 jar package cannot be added locally. Details will be given later.

 


3. After the import is successful, add dependencies

  1. Add the SO library directory configuration in the file of app:buid.gradle

    android {
        sourceSets {
            main.jniLibs.srcDirs = ['libs']
       }
    }
    
    
    
    //这里要注意,原因后文说
    dependencies {
        implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.5'
        implementation 'com.squareup.okio:okio:3.0.0'
    }
  2. Click Sync to synchronize the configuration.

4. Configure AndroidManifest.xml

      Add the corresponding permissions in your application's AndroidManifest.xml file:


<!--允许联网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!--获取GSM(2g)、WCDMA(联通3g)等网络状态的信息  -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!--获取wifi网络状态的信息 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!--保持CPU 运转,屏幕和键盘灯有可能是关闭的,用于文件上传和下载 -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!--获取sd卡写的权限,用于文件上传和下载-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--允许读取手机状态 用于创建BmobInstallation-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="cn.bmob.example"
        android:versionCode="1"
        android:versionName="1.0">

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="cn.bmob.example.MainActivity"
            android:screenOrientation="portrait"
            android:label="@string/app_name">

                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
    </application>
</manifest>

5.Configure ContentProvider

<application>
···
<provider
    android:name="cn.bmob.v3.util.BmobContentProvider"
    android:authorities="你的应用包名.BmobContentProvider">
</provider>
···
</application>

6.Initialize BmobSDK

Initialize the Bmob function in the onCreate() method of Application started by your application. The code looks like this:


       //提供以下两种方式进行初始化操作:

        //第一:默认初始化
        Bmob.initialize(this, "Your Application ID");
        // 注:自v3.5.2开始,数据sdk内部缝合了统计sdk,开发者无需额外集成,传渠道参数即可,不传默认没开启数据统计功能
        //Bmob.initialize(this, "Your Application ID","bmob");

        //第二:自v3.4.7版本开始,设置BmobConfig,允许设置请求超时时间、文件分片上传时每片的大小、文件的过期时间(单位为秒),
        //BmobConfig config =new BmobConfig.Builder(this)
        设置appkey
        //.setApplicationId("Your Application ID")
        请求超时时间(单位为秒):默认15s
        //.setConnectTimeout(30)
        文件分片上传时每片的大小(单位字节),默认512*1024
        //.setUploadBlockSize(1024*1024)
        文件的过期时间(单位为秒):默认1800s
        //.setFileExpiration(2500)
        //.build();
        //Bmob.initialize(config);

The configuration ends here. Next are some errors I encountered.

①Lorg/reactivestreams/Publisher
Solution: The reactive-streams.jar package is missing

//在app的build.gradle中添加如下依赖
implementation “org.reactivestreams:reactive-streams:1.0.2”


②Failed resolution of: Lokhttp3/MediaType;
Solution: The okio package must be imported through implementation and cannot be imported locally

//在app的build.gradle中添加如下依赖
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.5'
implementation 'com.squareup.okio:okio:3.0.0'


————————————————
Copyright statement: This article quotes the original article of CSDN blogger "little-cheng", original link : https://blog.csdn.net/m0_46356518/article/details/106483100, thank you here!

 

Guess you like

Origin blog.csdn.net/Walch123/article/details/127758056