SDK Integration

Update time: 2024/03/07 11:13:59

Instant Messaging (IM) is a stable messaging service based on the communication technology provided by CommsEase. You can use IM to build robust chatting features for your apps. Instant Messaging (NIM) SDK for Flutter offers a full-fledged instant messaging framework using Flutter, wrap nternal complexities, and provides easy-to-use APIs to facilitate quick integration with your applications. The SDK for Flutter supports Android and iOS.

Prerequisites

  • NIM SDK for Flutter is downloaded
  • Create Project in the CommsEase console and get AppKey and App Secret.
  • CommsEase IM accounts are registered. CommsEase IM accounts (account or accid) are used to log on to NIM SDK.
  • You have contacted the business manager to activate add-on services, such as data sync as required.

Development environment

Make sure your development environment meets the following requirements:

  • Flutter-dart 2.12.0 or later.

  • Development environment for each platform:

    Android
    • Android Studio 3.5 or later
    • Android 4.4 API 19 or later
    • 1.5.21 or later for kotlin-gradle-plugin.
    iOS
    • Xcode 11.0 and later.
    • iOS 9.0 or later
    • Your project must have a valid signature.
    Windows (Beta)
    • Windows 7 SP1 or later (x86-64 based 64-bit OS).
    • Visual Studio 2019
    • CMake 3.10 or later
    macOS (Beta)
    • Xcode 11.0 and later.
    • macOS 10.14 or later.
    • Your project must have a valid signature.
    Web (Beta)
    • You have installed Visual Studio Code with the flutter plugin installed.
    • Proficient in using Flutter commands.

    Windows, macOS, and Web are currently still in beta and in closed testing, so stay tuned.

Integrate the SDK

Integrate the SDK for Android, iOS, Windows, or macOS

NIM SDK for Flutter has been published to the pub library. You can download, and update the newest SDK by configuring pubspec.yaml .

  1. Add the following dependencies to your project's pubspec.yaml file.

    dependencies: 
    nim_core: ^1.1.0 
    
  2. Download the dependency packages by running the following command in the Shell or IDE.

    flutter pub get
    
Integrate the SDK for Web
  1. Add the following dependencies to your project's pubspec.yaml file.

    dependencies: 
    nim_core: ^1.1.0 
    
  2. Run the following command to download dependencies using Shell or IDE:

    flutter pub get
    
  3. Create a web folder and create an index.html file in the web folder. The template of the index file is as follows:

    The link in the following template is the link of the front-end js resource package of the Flutter SDK for Web. You must replace the link for new upgrades. Contact technical support for the link of the latest version.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8" />
        <meta content="IE=Edge" http-equiv="X-UA-Compatible" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta
        name="description"
        content="Flutter web"
        />
        <title>You page title</title>
        <script>
        // The value below is injected by flutter build, do not touch.
        var serviceWorkerVersion = null;
        </script>
        <script src="flutter.js"></script>
    </head>
    <body>
        <script>
        <!-- The link below is the link of the front-end js resource package of the Flutter SDK for Web. You must replace the link for new upgrades. Contact technical support for the link of the latest version.
        var jsConfig = [
            "https://yx-web-nosdn.netease.im/package/1664415864168/index.0.0.6.umd.js",
        ];
    
        window.onload = function () {
            // Download main.dart.js
            try {
            _flutter.loader
                .loadEntrypoint({
                serviceWorker: {
                    serviceWorkerVersion: serviceWorkerVersion,
                },
                })
                .then(function (engineInitializer) {
                return engineInitializer.initializeEngine()
                }).then(appRunner => {
                require(jsConfig, (RootService) => {
                    window.$flutterWebGlobal = {
                    rootService: new RootService.default(),
                    }
                    appRunner.runApp();
                });
                })
            } catch (error) {
            console.log(error);
            }
        };
    
        async function dartCallNativeJs(callInfo = {}) {
            const { serviceName, method, params, successCallback, errorCallback } =
                callInfo;
            try {
            console.log('dartCallNativeJs: ', callInfo)
            const service = window.$flutterWebGlobal.rootService[serviceName];
            if (!service) {
                throw new Error(`You do not implement this service: ${serviceName}`);
            }
            if (!service[method]) {
                throw new Error(`This method: ${method} is not implemented on this service: ${serviceName}`);
            }
            // Delete serviceName from the parameter
            if (params.hasOwnProperty('serviceName')) {
                delete params.serviceName;
            }
            const res = await service[method](params);
            successCallback(res)
            } catch (error) {
            console.error('dartCallNativeJs failed: ', error)
            errorCallback(error)
            throw error
            }
        }
        </script>
    </body>
    </html>
    
  4. Test whether the SDK for Web is integrated by running the flutter run -d chrome command. If you can see the following page and Chrome console call log after running the command, the integration is successful.

    FlutterWeb集成成功.png

Compilation and deobfuscation configuration for Android

After integrating the SDK for Flutter, you must add the following configuration for Android.

Compilation configuration

  1. Use kotlin-gradle-plugin and add the following configuration to the build.gradle file in the project root directory.

    groovy//file: build.gradle
    
    buildscript {
        ext.kotlin_version = '1.5.21'
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath 'com.android.tools.build:gradle:4.1.3'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        }
    }
    
  2. Configure packagingOptions in app/build.gradle. Example is as follows:

    groovy    //file: app/build.gradle
    
        // New: enable the Kotlin plugin
        apply plugin: 'kotlin-android'
    
        android {
            // Other settings
            ...
    
            packagingOptions {
                pickFirst 'lib/x86/libc++_shared.so'
                pickFirst 'lib/x86_64/libc++_shared.so'
                pickFirst 'lib/armeabi-v7a/libc++_shared.so'
                pickFirst 'lib/arm64-v8a/libc++_shared.so'
            }
        }
    

Deobfuscation configuration

Add the required classes of NIM SDK for Flutter to the anti-obfuscation list in the proguard-rules.pro file.

groovy-dontwarn com.netease.**
-keep class com.netease.** {*;}
-dontwarn org.apache.lucene.**
-keep class org.apache.lucene.** {*;}
-keep class net.sqlcipher.** {*;}
Was this page helpful?
Yes
No
  • Prerequisites
  • Development environment
  • Integrate the SDK
  • Compilation and deobfuscation configuration for Android
  • Compilation configuration
  • Deobfuscation configuration