dji-sdk / Mobile-SDK-Android

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

DJISDKManager.getInstance().registerApp() is crashing on newer androidx.lifecycle #913

Closed JurajBegovac closed 3 years ago

JurajBegovac commented 3 years ago

Crash stacktrace:

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:12)         at
dji.sdk.sdkmanager.DJISDKManager.registerApp(Unknown Source:42)         at
dji.sdk.sdkmanager.DJISDKManager.access$800(Unknown Source:0)         at
dji.sdk.sdkmanager.DJISDKManager$6$1.onDownloadSuccess(Unknown Source:8)         at 
dji.internal.ghu.ghu.fdd(UnknownSource:19)         at 
dji.sdk.sdkmanager.DJISDKManager$6.run(Unknown Source:37)         at 
dji.sdksharedlib.gfd.koy.run(Unknown Source:5)         at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)         at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)         at
java.lang.Thread.run(Thread.java:923)

From lifecycle version 2.3.0 (which was released in February this year) there is a change:

LifecycleRegistry now verifies that its methods are called on main thread. 
It was always a requirement for lifecycles of activities, fragments etc. 
An addition of observers from non-main threads resulted in hard to catch crashes in runtime. For LifecycleRegistry objects
that owned by your own components, you can explicitly opt out from checks by using LifecycleRegistry.createUnsafe(...), 
but then you have to ensure that a proper synchronization is in place when this LifecycleRegistry is accessed from 
different threads.

Source: https://developer.android.com/jetpack/androidx/releases/lifecycle#version_230_3

Please fix it asap :)

dji-dev commented 3 years ago

Agent comment from Luce Luo in Zendesk ticket #49087:

Dear Customer,

Thank you for contacting DJI. We will record this problem and you can use the recommended setting for the being time. https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle

Thanks,

Luce Luo DJI Developer Support

°°°

JurajBegovac commented 3 years ago

Hmmm well not sure if recommended settings fit other code but I'll try to use it

JurajBegovac commented 3 years ago

Yeah well in that case I cannot use latest appCompat, materialDesign, navigation etc etc :/

JurajBegovac commented 3 years ago

I had to downgrade to these versions:

    const val appCompat = "1.2.0"
    const val lifecycle = "2.2.0"
    const val navigation = "2.3.5"
    const val hilt = "2.32-alpha"
    const val hiltAndroidX = "1.0.0-alpha03"

This is not so nice :/

Qubiz commented 3 years ago

I am currently experiencing the same issue. I would like to be able to use the latest Jetpack Compose and Navigation features, however this issue is blocking that transition. A fix or workaround would be great.

Hoker- commented 3 years ago

DJISDKManager.getInstance().registerApp() is not running on main thread,currently does not support the latest lifecycle.

jazzjackrabb1t commented 3 years ago

772 Releated open issue

Michael-DJI commented 3 years ago

4.15 will fix it.

JurajBegovac commented 3 years ago

@Michael-DJI Ok and when could we expect that version? :)

Qubiz commented 3 years ago

I am using version 4.15 now, but this mentioned issue still occurs. Are you sure it is fixed @Michael-DJI? Is anyone else still experiencing this issue?

Michael-DJI commented 3 years ago

@Qubiz is the same crash stack info?

Qubiz commented 3 years ago

@Michael-DJI Yes, it is the same crash stack-trace as in the original post.

Hoker- commented 3 years ago

https://github.com/dji-sdk/Mobile-SDK-Android/blob/master/Sample%20Code/app/build.gradle image the sample has update lifecycle to 2.3.1,It works well。 Can you reproduce on the sample code? @Qubiz

Qubiz commented 3 years ago

@Hoker- Thanks for the reply. I have now tried version 2.3.1 for the dependencies in my project:

implementation("androidx.lifecycle:lifecycle-extensions") {
    version {
        strictly("2.0.0-rc01")
    }
}
implementation("androidx.lifecycle:lifecycle-common-java8") {
    version {
        strictly("2.3.1")
    }
}
implementation("androidx.lifecycle:lifecycle-runtime-ktx") {
    version {
        strictly("2.3.1")
    }
}
implementation("androidx.lifecycle:lifecycle-process") {
    version {
        strictly("2.3.1")
    }
}
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx") {
    version {
        strictly("2.3.1")
    }
}

However, this still produces the same error for me:

  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:12)
      at dji.sdk.sdkmanager.DJISDKManager.registerApp(Unknown Source:42)
      at dji.sdk.sdkmanager.DJISDKManager.access$800(Unknown Source:0)
      at dji.sdk.sdkmanager.DJISDKManager$6$1.onDownloadSuccess(Unknown Source:8)
      at dji.internal.ghu.ghu.fdd(Unknown Source:19)
      at dji.sdk.sdkmanager.DJISDKManager$6.run(Unknown Source:37)
      at dji.sdksharedlib.gfd.koy.run(Unknown Source:5)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
      at java.lang.Thread.run(Thread.java:919)

I am not sure what I am doing wrong to be honest.

I will try the sample code next, to see if I can reproduce it there.

Michael-DJI commented 3 years ago

@Qubiz thank you, we will wait for your latest testing result on the sample code.

Qubiz commented 3 years ago

@Michael-DJI I tested the sample code and I can't reproduce the error when using the initial gradle configuration.

However, I did manage to reproduce the error when adding the UXSDK as a replacement for the MSDK like this:

implementation('com.dji:dji-uxsdk:4.14', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.15'

I suspect that the UXSDK v4.14 still uses the MSDK v4.14?

The error does not occur anymore when I explicitly add the com.dji:dji-sdk:4.15' dependency as well:

implementation('com.dji:dji-uxsdk:4.14', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
implementation('com.dji:dji-sdk:4.15', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.15'
Hoker- commented 3 years ago

@Michael-DJI I tested the sample code and I can't reproduce the error when using the initial gradle configuration.

However, I did manage to reproduce the error when adding the UXSDK as a replacement for the MSDK like this:

implementation('com.dji:dji-uxsdk:4.14', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.15'

I suspect that the UXSDK v4.14 still uses the MSDK v4.14?

The error does not occur anymore when I explicitly add the com.dji:dji-sdk:4.15' dependency as well:

implementation('com.dji:dji-uxsdk:4.14', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
implementation('com.dji:dji-sdk:4.15', {
    exclude module: 'library-anti-distortion'
    exclude module: 'fly-safe-database'
})
compileOnly 'com.dji:dji-sdk-provided:4.15'

https://mvnrepository.com/artifact/com.dji/dji-uxsdk/4.14 image yes,uxsdk v4.14 still uses MSDK v4.14,you need to replace manually。