dji-sdk / Mobile-SDK-Android

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

Method addObserver must be called on the main thread #772

Open midgard-as opened 3 years ago

midgard-as commented 3 years ago

Hello,

I have an issue when trying to import dji sdk to android app.

Registering app crashes the app because of this error :

java.lang.IllegalStateException: Method addObserver must be called on the main thread at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317) at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172) at dji.sdk.sdkmanager.DJISDKManager.initParams(Unknown Source:192) at dji.sdk.sdkmanager.DJISDKManager.initSDKManager(Unknown Source:15) at dji.sdk.sdkmanager.DJISDKManager.registerApp(Unknown Source:42) at dji.sdk.sdkmanager.DJISDKManager.access$700(Unknown Source:0) at dji.sdk.sdkmanager.DJISDKManager$6$1.onDownloadSuccess(Unknown Source:6) at dji.internal.ghu.ghu.fdd(Unknown Source:19) at dji.sdk.sdkmanager.DJISDKManager$6.run(Unknown Source:33) at dji.sdksharedlib.gfd.koy.run(Unknown Source:5) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764)

My build.gradle is :

` plugins { id 'com.android.application' }

apply plugin: 'kotlin-android' android { compileSdkVersion 30 buildToolsVersion "30.0.1" defaultConfig { applicationId "com.example.midgardApp" minSdkVersion 21 targetSdkVersion 30 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }

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

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}

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"
    exclude 'META-INF/rxjava.properties'
}

}

dependencies { implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation "androidx.fragment:fragment-ktx:1.3.0" implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'com.google.android.material:material:1.3.0' implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.6.0' implementation 'com.github.pedroSG94.rtmp-rtsp-stream-client-java:rtplibrary:1.9.5' implementation 'org.java-websocket:Java-WebSocket:1.5.1' implementation 'com.dji:dji-sdk:4.13.1' compileOnly 'com.dji:dji-sdk-provided:4.13.1' //OK }

repositories { google() mavenCentral() } `

//To include DJI SDK see here : https://developer.dji.com/document/76942407-070b-4542-8042-204cfb169168`

Do you have any ideas or solutions ?

Thanks !

dji-dev commented 3 years ago

Agent comment from Luce Luo in Zendesk ticket #44058:

Dear Customer,

Thank you for contacting DJI. The latest SDK is 4.14trial1, here is an official Grade sample, please refer to it. https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle

Thanks,

Luce Luo DJI Developer Support

midgard-as commented 3 years ago

Thanks, it worked when i downgraded the version of android fragment to "androidx.fragment:fragment:1.0.0" !

codeversed commented 3 years ago

It seems that the issue is that the DJI SDK is NOT compatible with Android lifecycle components 2.3+. This is a concerning issue moving forward since other dependencies our app uses require the latest lifecycle components.

@dji-dev - Will there be a fix for this in the near future?

drxeno02 commented 3 years ago

As of 05/25/21 this issue still exists. Using implementation androidx.lifecycle:lifecycle-process:2.3.1 will still require LifecycleObserver to run on the main thread.

For example,

runOnUiThread {
   ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}

For those of you trying to do this outside of an Activity, you can try this. Cheers!

Handler(Looper.getMainLooper()).post {
   ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
jazzjackrabb1t commented 3 years ago

Also did not work with implementation 'androidx.appcompat:appcompat:1.3.0'. I had to downgrade to implementation 'androidx.appcompat:appcompat:1.2.0' to get it worked.

Ebraheemb commented 3 years ago

I downgraded implementation "androidx.fragment:fragment-ktx:1.3.4" to implementation "androidx.fragment:fragment-ktx:1.2.5"

kletzander commented 3 years ago

I have the same problem even with code that worked before (2 months ago). Right now, when I compiled and executed the code on the same phone, the initialization suddenly crashes with this error. When can we expect this bug to be fixed? I don't know how others, but I can't use my application anymore. EDIT: BTW downgrading appcompat dependencies did not work for me :(

artem-bagritsevich commented 3 years ago

I have the same problem, but in my case, some libraries used in my project require "androidx.appcompat:appcompat:1.3.0" so I cannot downgrade it directly. Please fix it.

JurajBegovac commented 3 years ago

@kletzander I experienced the same but I "fixed" it by analyzing dependencies - take a look at #913

The problem is that DJI SDK is not compatible with newer androidx.lifecycle:lifecycle-runtime. It's just compatible with androidx.lifecycle:lifecycle-runtime:2.2.0 (2.3.1 is the latest stable version). And that lifecycle-runtime is commonly used in other Android libraries so you have to find out which other libraries depend on lifecycle-runtime and downgrade them.

I had to downgrade these libs (I use kotlin for gradle but I guess it's obvious):

    const val appCompat = "1.2.0"
    const val lifecycle = "2.2.0"
    const val navigation = "2.3.5"
    const val hilt = "2.32-alpha" // I have to use alpha version because of DJI not compatible with latest lifecycle-runtime :(
    const val hiltAndroidX = "1.0.0-alpha03"

Just run ./gradlew :app:dependencies to see which lifecycle-runtime you use

Example:

Screen Shot 2021-06-24 at 07 17 28

Or wait for DJI fix 🤷

chubecode commented 2 years ago

why nobody fix it ?