dji-sdk / Mobile-SDK-Android

DJI Mobile SDK for Android: http://developer.dji.com/mobile-sdk/
Other
991 stars 580 forks source link

Android 12 CommonCallback error #1155

Open Adatrader opened 2 years ago

Adatrader commented 2 years ago

I am getting this error on Android 12 devices with DJI SDK 4.16. I can link proguard, manifest, etc if that would help. Any ideas?

E/AndroidRuntime: FATAL EXCEPTION: pool-3-thread-1
    Process: com.drone_app, PID: 9701
    java.lang.NoClassDefFoundError: Failed resolution of: Ldji/common/util/CommonCallbacks$CompletionCallback;
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76)
        at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1177)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1457)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7458)
        at android.app.ActivityThread.access$1700(ActivityThread.java:310)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2281)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:226)
        at android.os.Looper.loop(Looper.java:313)
        at android.app.ActivityThread.main(ActivityThread.java:8669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "dji.common.util.CommonCallbacks$CompletionCallback" on path: DexPathList[[zip file "/data/app/~~m70fr00KcJrZ6z9kIQ==/com.drone_app-31kermXUY-y4PaR0suw==/base.apk"],nativeLibraryDirectories=[/data/app/~~m70fr006z9kIQ==/com.drone_app-31kermXUYo7Z-y4PaR0suw==/lib/arm64, /data/app/~~m70fr00K9kIQ==/com.drone_app-31kermX-y4PaR0suw==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateApplication(AppComponentFactory.java:76) 
        at androidx.core.app.CoreComponentFactory.instantiateApplication(CoreComponentFactory.java:52) 
        at android.app.Instrumentation.newApplication(Instrumentation.java:1177) 
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1457) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7458) 
        at android.app.ActivityThread.access$1700(ActivityThread.java:310) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2281) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loopOnce(Looper.java:226) 
        at android.os.Looper.loop(Looper.java:313) 
        at android.app.ActivityThread.main(ActivityThread.java:8669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 

App gradle:

apply plugin: 'com.android.application'
apply plugin: "com.google.protobuf"
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

repositories {
    mavenLocal()
}

android {
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.drone_app"
        minSdkVersion 23
        compileSdk 31
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        ndk {
            // On x86 devices that run Android API 23 or above, if the application is targeted with API 23 or
            // above, FFmpeg lib might lead to runtime crashes or warnings.
            abiFilters 'armeabi-v7a', 'arm64-v8a'
        }
    }

    buildTypes {
        release {
            shrinkResources false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            shrinkResources false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    packagingOptions {
        doNotStrip "*/*/libdjivideo.so"
        doNotStrip "*/*/libSDKRelativeJNI.so"
        doNotStrip "*/*/libFlyForbid.so"
        doNotStrip "*/*/libduml_vision_bokeh.so"
        doNotStrip "*/*/libyuv2.so"
        doNotStrip "*/*/libGroudStation.so"
        doNotStrip "*/*/libFRCorkscrew.so"
        doNotStrip "*/*/libUpgradeVerify.so"
        doNotStrip "*/*/libFR.so"
        doNotStrip "*/*/libDJIFlySafeCore.so"
        doNotStrip "*/*/libdjifs_jni.so"
        doNotStrip "*/*/libsfjni.so"
        doNotStrip "*/*/libDJICommonJNI.so"
        doNotStrip "*/*/libDJICSDKCommon.so"
        doNotStrip "*/*/libDJIUpgradeCore.so"
        doNotStrip "*/*/libDJIUpgradeJNI.so"
        doNotStrip "*/*/libDJIWaypointV2Core.so"
        doNotStrip "*/*/libAMapSDK_MAP_v6_9_2.so"
        doNotStrip "*/*/libDJIMOP.so"
        doNotStrip "*/*/libDJISDKLOGJNI.so"
        exclude 'META-INF/rxjava.properties'
        exclude 'assets/location_map_gps_locked.png'
        exclude 'assets/location_map_gps_3d.png'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
        disable 'GoogleAppIndexingWarning', 'HardcodedText', 'InvalidPackage'
        textReport true
        textOutput "stdout"
    }
}

protobuf {
    protoc {
      if (osdetector.os == "osx") {
        artifact = 'com.google.protobuf:protoc:3.19.2:osx-x86_64'
      } else {
        artifact = 'com.google.protobuf:protoc:3.19.2'
      }
    }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.47.0' // CURRENT_GRPC_VERSION
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java { option 'lite' }
            }
            task.plugins {
                grpc { // Options added to --grpc_out
                    option 'lite' }
            }
        }
    }
}

dependencies {
    compileOnly fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.multidex:multidex:2.0.0'
    implementation 'com.squareup:otto:1.3.8'
    implementation 'com.squareup.retrofit2:retrofit:2.0.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.0.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'io.socket:socket.io-client:2.0.1'
    implementation('com.dji:dji-sdk:4.16', {
        exclude module: 'library-anti-distortion'
        exclude module: 'fly-safe-database'
    })
    compileOnly 'com.dji:dji-sdk-provided:4.16'
    implementation ('com.dji:dji-uxsdk:4.16', {
        exclude group: 'com.amap.api'
        exclude module: 'dji-sdk'
    })
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.core:core:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-rc01'
    implementation 'androidx.annotation:annotation:1.0.0'
    implementation 'com.jakewharton:butterknife:10.0.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'
    implementation "androidx.core:core-ktx:1.6.0"
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.0.0-rc01"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9"
    implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9"

    implementation 'androidx.lifecycle:lifecycle-common-java8:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-runtime:2.3.1'
    implementation 'androidx.lifecycle:lifecycle-process:2.3.1'
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1"

    implementation 'com.google.guava:guava:29.0-android'
    implementation 'org.apache.commons:commons-math3:3.6.1'

    implementation 'io.grpc:grpc-protobuf-lite:1.47.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-stub:1.47.0' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-android:1.47.0'
    implementation 'io.grpc:grpc-okhttp:1.47.0'
    implementation 'org.apache.tomcat:annotations-api:6.0.53'
}
DJI-William commented 2 years ago

First of all, Your MSDK can upgrade to 4.16.1. I don't find anything wrong in your build.gradle file. How about your AndroidManifest.xml file? Have you add android:extractNativeLibs="true"?

Adatrader commented 2 years ago

I do have extractNativeLibs="true" but am unable to use 4.16.1 due to unrelated bug causing camera gimbal to rotate 90 degrees to left. AndroidManifest.xml below:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.drone_app">

    <!-- Permissions and features -->
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-feature
        android:name="android.hardware.usb.host"
        android:required="false" />
    <uses-feature
        android:name="android.hardware.usb.accessory"
        android:required="true" />

    <application
        android:name=".application.DroneApplication"
        android:allowBackup="true"
        android:extractNativeLibs="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.DroneApp"
        android:usesCleartextTraffic="true">

        <uses-library
            android:name="org.apache.http.legacy"
            android:required="false" /> <!-- DJI SDK -->
        <uses-library android:name="com.android.future.usb.accessory" />

        <meta-data
            android:name="com.dji.sdk.API_KEY"
            android:value="xxx" />

        <activity
            android:name="dji.sdk.sdkmanager.DJIAoaControllerActivity"
            android:theme="@android:style/Theme.Translucent" >
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
            </intent-filter>

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

        <service
            android:name="dji.sdk.sdkmanager.DJIGlobalService"
            tools:ignore="Instantiatable" />
        <!-- DJI SDK -->
        <activity
            android:name=".activities.ConnectionActivity"
            android:configChanges="orientation"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED" />
            </intent-filter>

            <meta-data
                android:name="android.hardware.usb.action.USB_ACCESSORY_ATTACHED"
                android:resource="@xml/accessory_filter" />
        </activity>
        <activity
            android:name=".activities.MainActivity"
            android:screenOrientation="landscape" />
        <activity
            android:name=".activities.MediaActivity"
            android:screenOrientation="landscape" />
    </application>
Adatrader commented 2 years ago

From debugging, error gets triggered with this method and specifically the setVirtualStickModeEnabled call inside the Application class:

public static void releaseVirtualStickMode() {
        Aircraft aircraft = getAircraftInstance();
        if (aircraft == null)
            return;
        aircraft.getFlightController().setVirtualStickModeEnabled(false, (djiError) -> {});
}
DJI-William commented 2 years ago

I think this bug was introduced in 4.16.1 version, we will fix this in 4.16.2.

Adatrader commented 2 years ago

Ok, thank you for looking into this issue. Do you have an idea when 4.16.2 will be released?

DJI-William commented 2 years ago

2 weeks later.

Kubessandra commented 2 years ago

Same issue

java.lang.NoClassDefFoundError: Failed resolution of: Ldji/common/util/CommonCallbacks$CompletionCallback;
     aircraft.flightController.getSerialNumber(object: CommonCallbacks.CompletionCallbackWith<String> {
        override fun onSuccess(serial: String) {
          promise.resolve(serial)
        }

        override fun onFailure(error: DJIError?) {
          promise.reject(error.toString(), error?.description)
        }
      })

I fixed it by using:

android {
    defaultConfig {
        multiDexEnabled true
    }
}

The fix, resolved some issue but now I have it when I try to call .getName() on an aircraft