Empty Project Import MSDK

2024-11-18
No Rating

This tutorial explains how to migrate the MSDK package and UXSDK open-source framework from the MSDK V5 Sample to a user's empty project.

Suggested Versions

  • Android Studio:Android Studio Koala | 2024.1.1
  • Java Runtime:17 or 11
  • Kotlin:1.8.10
  • Gradle:7.6.2
  • Android Gradle Plugin:7.4.2
  • minSdkVersion:23
  • targetSdkVersion:34
  • If using a non-recommended version, please adapt the entire integration process according to the actual version. Android Studio Koala 2024.1.1 comes with Java Runtime 17 integrated by default, which can generally be used directly without any additional configuration.
  • Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported. If your app uses Kotlin synthetics for view binding, use this guide to migrate to Jetpack view binding: Migrate from Kotlin synthetics to Jetpack view binding.

Obtain a Empty Project Integrated with MSDK

If you prefer not to manually configure your project following the tutorial below, you can download the [empty project with MSDK integrated]open in new window,and follow this document to start the final configuration and debugging from the Testing and Debugging section.

Create a New Empty Project

  1. On the Android Studio launch page, select New Project > Phone and Tablet > Empty Views Activity.
  2. Complete the configuration according to the image below.
    • Name:MSDKSample
    • Package name:com.example.msdksample
    • Minimum SDK:24 or higher
    • Build configuration language:Groovy DSL (build.gradle)
empty project

Edit the build.gradle (Project) File

Go to build.gradle (Project) under Gradle Scripts. Replace all the content with the following code segment, which provides the correct reference library.

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
		// Suggested Android Gradle Plugin Version
        classpath "com.android.tools.build:gradle:7.4.2"

		//Suggested Kotlin version
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION" 
	    // ... (remaining code)
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Edit the build.gradle (Module: app) File

  1. In the android section, configure sdkversion and ndk in defaultConfig, and set up packagingOptions.

Note the versions:

  • minSdkVersion needs to be upgraded to a minimum version number of 23.
  • MSDK only supports arm64-v8a architecture.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
 
android {
    namespace 'com.example.msdksample'
    compileSdkVersion 34 // MSDK supported version
 
    defaultConfig {
        applicationId "com.example.msdksample"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"
 
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
 
        ndk {
            abiFilters 'arm64-v8a' // MSDK only supports arm64-v8a architecture
        }
 
        // MSDK related so library
        packagingOptions {
            pickFirst 'lib/arm64-v8a/libc++_shared.so'
            pickFirst 'lib/armeabi-v7a/libc++_shared.so'
        }
 
        // MSDK related so library
        packagingOptions {
            doNotStrip "*/*/libconstants.so"
            doNotStrip "*/*/libdji_innertools.so"
            doNotStrip "*/*/libdjibase.so"
            doNotStrip "*/*/libDJICSDKCommon.so"
            doNotStrip "*/*/libDJIFlySafeCore-CSDK.so"
            doNotStrip "*/*/libdjifs_jni-CSDK.so"
            doNotStrip "*/*/libDJIRegister.so"
            doNotStrip "*/*/libdjisdk_jni.so"
            doNotStrip "*/*/libDJIUpgradeCore.so"
            doNotStrip "*/*/libDJIUpgradeJNI.so"
            doNotStrip "*/*/libDJIWaypointV2Core-CSDK.so"
            doNotStrip "*/*/libdjiwpv2-CSDK.so"
            doNotStrip "*/*/libFlightRecordEngine.so"
            doNotStrip "*/*/libvideo-framing.so"
            doNotStrip "*/*/libwaes.so"
            doNotStrip "*/*/libagora-rtsa-sdk.so"
            doNotStrip "*/*/libc++.so"
            doNotStrip "*/*/libc++_shared.so"
            doNotStrip "*/*/libmrtc_28181.so"
            doNotStrip "*/*/libmrtc_agora.so"
            doNotStrip "*/*/libmrtc_core.so"
            doNotStrip "*/*/libmrtc_core_jni.so"
            doNotStrip "*/*/libmrtc_data.so"
            doNotStrip "*/*/libmrtc_log.so"
            doNotStrip "*/*/libmrtc_onvif.so"
            doNotStrip "*/*/libmrtc_rtmp.so"
            doNotStrip "*/*/libmrtc_rtsp.so"
        }
  1. In dependencies, configure the SDK package and modify the other referenced libraries to their corresponding versions.

Note: {$SDK_VERSION} is the latest version of MSDK. To replace the version, refer to Edit the gradle.properties File.

dependencies {
            implementation project(":android-sdk-v5-uxsdk")
 
            implementation "com.dji:dji-sdk-v5-aircraft:$SDK_VERSION"
            compileOnly "com.dji:dji-sdk-v5-aircraft-provided:$SDK_VERSION"
 
            implementation 'androidx.appcompat:appcompat:1.6.1'
            implementation 'com.google.android.material:material:1.8.0'
            testImplementation 'junit:junit:4.13.2'
            androidTestImplementation 'androidx.test.ext:junit:1.1.5'
            androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
        }

Edit the AndroidManifest.xml File

Refer to the AndroidManifest.xmlopen in new window file in the MSDK V5 Sample for the following modifications. Pay attention to the positions between code segments and tags.

  1. Add the basic permissions required by the SDK. Before that, make sure you understand which Android system permissions are required by MSDK V5open in new window and permission definitionsopen in new window.
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
  1. Add USB-related permissions within the manifest tag for connecting to the remote controller.
<uses-feature
    android:name="android.hardware.usb.host"
    android:required="false"/>
<uses-feature
    android:name="android.hardware.usb.accessory"
    android:required="true"/>
  1. Add the declaration of the MyApplication file within the application tag, which is required for loading at startup.
  <application
    android:name=".MyApplication"
    //... (remaining code)
  1. Modify the application tag attributes with the following code segment to configure the app key.

Visit the developer websiteopen in new window to apply for an app key. When applying for the app key, the "Package Name" should be com.example.msdksample. Add the following code to the application item, and keep android:name unchanged and replace your app key in android:value with the app key you applied for.

<meta-data
    android:name="com.dji.sdk.API_KEY"
    android:value="your app key"/>
  1. Configure USB accessory notification within the activity tag.
<intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

<meta-data
    android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
    android:resource="@xml/accessory_filter" />

Create a MyApplication.kt File

  1. Create a new MyApplication.kt file under the com.example.msdksample folder, at the same level as MainActivity.kt.
  2. Write the following code in the file, which is used to import the SDK decryption and protection package.

Note: Pay attention to the consistency of the package name: com.example.msdksample

package com.example.msdksample

import android.app.Application
import android.content.Context
import android.util.Log
import dji.v5.common.error.IDJIError
import dji.v5.common.register.DJISDKInitEvent
import dji.v5.manager.SDKManager
import dji.v5.manager.interfaces.SDKManagerCallback

class MyApplication : Application() {

	private val TAG = this::class.simpleName

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        com.cySdkyc.clx.Helper.install(this)
    }

    override fun onCreate() {
        super.onCreate()
        SDKManager.getInstance().init(this,object:SDKManagerCallback{
            override fun onInitProcess(event: DJISDKInitEvent?, totalProcess: Int) {
                Log.i(TAG, "onInitProcess: ")
                if (event == DJISDKInitEvent.INITIALIZE_COMPLETE) {
                    SDKManager.getInstance().registerApp()
                }
            }
            override fun onRegisterSuccess() {
                Log.i(TAG, "onRegisterSuccess: ")
            }
            override fun onRegisterFailure(error: IDJIError?) {
                Log.i(TAG, "onRegisterFailure: ")
            }
            override fun onProductConnect(productId: Int) {
                Log.i(TAG, "onProductConnect: ")
            }
            override fun onProductDisconnect(productId: Int) {
                Log.i(TAG, "onProductDisconnect: ")
            }
            override fun onProductChanged(productId: Int)
            {
                Log.i(TAG, "onProductChanged: ")
            }
            override fun onDatabaseDownloadProgress(current: Long, total: Long) {
                Log.i(TAG, "onDatabaseDownloadProgress: ${current/total}")
            }
        })
    }
}

Edit the MainActivity.kt File

  1. Delete the ui folder in the same directory as MainActivity.kt.
  2. Replace the content of the MainActivity.kt file with the following code.

Note: Pay attention to the consistency of the package name: com.example.msdksample

package com.example.msdksample
 
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
 
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Create a activity_main.xml File

  1. In the app > src > main > res directory, create a new layout folder and create a new activity_main.xml file in the folder.
  2. Replace the content of the activity_main.xml file with the following code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  1. In the app > src > main > res > values directory, replace the content of the themes.xml file with the following code.
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.MSDKSample" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.MSDKSample" parent="Base.Theme.MSDKSample" />
</resources>

Import UXSDK Open-Source Framework

  1. Copy the entire UXSDK project (android-sdk-v5-uxsdkopen in new window) to the root directory of the MyApplication project path, at the same level as the app folder.

  2. Edit the gradle.properties file, adding the following code to configure the correct version.

#buildconfig
ANDROID_MIN_SDK_VERSION=23 //DJI MSDK supported version
ANDROID_TARGET_SDK_VERSION=34 //DJI MSDK supported version
ANDROID_COMPILE_SDK_VERSION=34 //DJI MSDK supported version
  1. Create a new dependencies.gradle file in the same directory as build.gradle (project) and import the contents of dependencies.gradleopen in new window into it. Add the following code segment at the beginning of the build.gradle (Project) file.
apply from:rootProject.file('dependencies.gradle')
  1. Adjust the content of the settings.gradle file as the following code segment.
rootProject.name = "My Application"
include ':app'
include ':android-sdk-v5-uxsdk'
  1. In the folder directory app > res > values > strings.xml, edit the "name" attribute of this string in the file. The meaning of this code is the name of your app.
<string name="your_app_name">My Application</string>

In actual development, you can edit the id and value of this code above. Just keep the id name consistent with the corresponding id name in the AndroidManifest.xml file in the project. In this example, the application's id name is "your_app_name".

<application
        android:label="@string/your_app_name">

Edit the gradle.properties File

  1. Use the KOTLIN_VERSION and SDK_VERSION sections in the gradle.properties file to change the Kotlin version and MSDK version. For example, you can add the following two lines of code after the #buildconfig section to use Kotlin version 1.8.10 and MSDK version 5.10.0.
KOTLIN_VERSION=1.8.10
SDK_VERSION=5.10.0

Note: To avoid compatibility problem in the compilation process, change the configuration sections in gradle.properties as follows.

android.useAndroidX=true
android.enableJetifier=true
android.nonTransitiveRClass=false

Congrats! By now, you have completed the configuration of the empty project.

Testing and Debugging

  1. Select "Sync Now" to synchronize the project.

  2. Click on the hammer icon on the interface, build your project, and do necessary debugging.

Due to the limitations of the empty project, occasional errors might occur during the build process because the UXSDK package content can not be properly referenced. This does not affect the normal function of the project, and you should continue to connect your device to test the app. If you find a solution to such errors during the integration process, feel free to provide feedback via documentation. Possible errors may include:

Execution failed for task ':android-sdk-v5-uxsdk:kaptGenerateStubsDebugKotlin'.
  1. Connect the DJI remote controller with screen or your mobile device, enable the corresponding developer options, and once your device is displayed on the top right corner of Android Studio, select the 'run'app' triangle icon to test whether the project is good to run.
If you have any comments or confusion about our documentation, you can click here to give feedback and we will get back to you as soon as possible.