sematext/sematext-logsene-android

Upgrade Logsene Android SDK to work on SDK version 29 and 30

tlierdotfr opened this issue · 14 comments

An exception is raised when calling Logsene.init(context) during App init with the stack below.
No trace of WorkManager on the getStarted.

Using Android Target SDK 29 and logsene 3.0.0.

2021-05-31 17:51:50.076 20205-20205/com.enterprise.edetect E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.enterprise.edetect, PID: 20205
    java.lang.RuntimeException: Unable to instantiate application com.enterprise.edetect.view.AppDelegate: java.lang.IllegalStateException: WorkManager is not initialized properly.  The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1226)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6594)
        at android.app.ActivityThread.access$1600(ActivityThread.java:231)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1952)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
     Caused by: java.lang.IllegalStateException: WorkManager is not initialized properly.  The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
        at androidx.work.WorkManager.getInstance(WorkManager.java:141)
        at com.sematext.logseneandroid.Logsene.schedulePeriodicWorker(Logsene.java:473)
        at com.sematext.logseneandroid.Logsene.init(Logsene.java:110)
        at com.enterprise.edetect.view.AppDelegate.setupServices(AppDelegate.kt:65)
        at com.enterprise.edetect.view.AppDelegate.attachBaseContext(AppDelegate.kt:34)
        at android.app.Application.attach(Application.java:390)
        at android.app.Instrumentation.newApplication(Instrumentation.java:1154)
        at android.app.LoadedApk.makeApplication(LoadedApk.java:1218)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6594) 
        at android.app.ActivityThread.access$1600(ActivityThread.java:231) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1952) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7682) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) 
otisg commented

The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider

Would you happen to know if your WorkManagerInitializer is disabled in your manifest?

Would you happen to know if your WorkManagerInitializer is disabled in your manifest?

There is no trace of WorkManager in my Manifest ...

gr0 commented

Hi @tlierdotfr. Sorry for the issues, I managed to reproduce the issue when android:allowBackup="true" is set in the application in the manifest file and when the automatic location enrichment is present. Can you try setting the android:allowBackup="false" and <meta-data android:name="LogseneAutomaticLocationEnabled" android:value="false" /> and see if it works?

gr0 commented

Meanwhile I changed the topic of the issue to generally upgrade Logsene Android SDK to work with target SDK of 29 and newer, hopefully you don't mind :)

gr0 commented

I just tested the code on Pixel XL API 29 virtual device and managed to have it running:

2021-05-31 23:21:02.733 3673-3673/? I/seneapplicatio: Not late-enabling -Xcheck:jni (already on)
2021-05-31 23:21:02.884 3673-3673/? E/seneapplicatio: Unknown bits set in runtime_flags: 0x8000
2021-05-31 23:21:02.884 3673-3673/? W/seneapplicatio: Unexpected CPU variant for X86 using defaults: x86
2021-05-31 23:21:03.423 3673-3740/com.sematext.logseneapplication W/libc: Unable to set property "qemu.gles" to "1": connection failed; errno=13 (Permission denied)
2021-05-31 23:21:03.572 3673-3673/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-05-31 23:21:03.572 3673-3673/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-05-31 23:21:03.623 3673-3673/com.sematext.logseneapplication E/INFO: Android version: 10
2021-05-31 23:21:03.701 3673-3737/com.sematext.logseneapplication W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2021-05-31 23:21:03.733 3673-3737/com.sematext.logseneapplication W/Gralloc3: mapper 3.x is not supported
2021-05-31 23:21:03.970 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden field Lcom/android/org/conscrypt/OpenSSLSocketFactoryImpl;->sslParameters:Lcom/android/org/conscrypt/SSLParametersImpl; (greylist, reflection, allowed)
2021-05-31 23:21:03.971 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden field Lcom/android/org/conscrypt/SSLParametersImpl;->x509TrustManager:Ljavax/net/ssl/X509TrustManager; (greylist, reflection, allowed)
2021-05-31 23:21:03.413 3673-3673/com.sematext.logseneapplication W/RenderThread: type=1400 audit(0.0:12): avc: denied { write } for name="property_service" dev="tmpfs" ino=7377 scontext=u:r:untrusted_app_27:s0:c133,c256,c512,c768 tcontext=u:object_r:property_socket:s0 tclass=sock_file permissive=0 app=com.sematext.logseneapplication
2021-05-31 23:21:05.056 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Lcom/android/org/conscrypt/ConscryptFileDescriptorSocket;->setUseSessionTickets(Z)V (greylist,core-platform-api, reflection, allowed)
2021-05-31 23:21:05.056 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Lcom/android/org/conscrypt/ConscryptFileDescriptorSocket;->setHostname(Ljava/lang/String;)V (greylist,core-platform-api, reflection, allowed)
2021-05-31 23:21:05.056 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->setAlpnProtocols([B)V (greylist,core-platform-api, reflection, allowed)
2021-05-31 23:21:05.224 3673-3857/com.sematext.logseneapplication W/seneapplicatio: Accessing hidden method Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B (greylist,core-platform-api, reflection, allowed)
2021-05-31 23:21:05.390 3673-3857/com.sematext.logseneapplication E/Logsene: Unable to index all documents. Response: {"took":1,"errors":true,"items":[]}
    Request: { "index" : { "_index": "95efcdaf-5ca4-4c48-9975-69cd601036f7", "_type" : "example" } }
    {"activity":"MainActivity","action":"started","@timestamp":"2021-05-31T21:21:03.6+0000","meta":{"versionName":"3.0.0","versionCode":1,"osRelease":"10","osType":"Android","uuid":"e5e5831e-c7af-4e3c-a676-192e59fd0893","user":"user@example.com","userType":"free"}}
2021-05-31 23:21:05.408 3673-3735/com.sematext.logseneapplication I/WM-WorkerWrapper: Worker result SUCCESS for Work [ id=bd7d85fa-b0c8-4fee-bdef-b20ab3b72423, tags={ com.sematext.logseneandroid.LogWorker, com.sematext.android.LogWorker.interval } ]
2021-05-31 23:21:18.584 3673-3673/com.sematext.logseneapplication E/INFO: Sending single message to Sematext Cloud
2021-05-31 23:21:19.432 3673-3673/com.sematext.logseneapplication E/INFO: Sending single message to Sematext Cloud
2021-05-31 23:21:24.650 3673-5597/com.sematext.logseneapplication E/INFO: Sending 1000 messages to Sematext Cloud
2021-05-31 23:21:25.383 3673-5629/com.sematext.logseneapplication E/INFO: Sending 1000 messages to Sematext Cloud

If you could try the changes that I asked @tlierdotfr it would be great.

Thanks for your reply @gr0 unfortunately even after changing android:allowBackup to false and adding <meta-data android:name="LogseneAutomaticLocationEnabled" android:value="false" /> in my Manifest I still have the same Exception at Logsene.init call.

I double check on my gradle file and I'm sorry but my compileSdkVersion is 28 (and not 29).
I missread with TargetSdkVersion which is set to 29 :-/

gr0 commented

HI @tlierdotfr, I'll try replicating that, but so far things were working for me on SDK 28. Can you try running the example in from the https://github.com/sematext/sematext-logsene-android/tree/master/app and see if that works for you?

It would be also good if you could share your manifest file with me - either here or to rafal.kuc(at)sematext.com.

Sorry @gr0 , I have tried running the example on a simulator with Android 30 and after another with Android 28 and both crashed with following Exception :

2021-06-02 21:46:47.201 4031-4031/com.sematext.logseneapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.sematext.logseneapplication, PID: 4031
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sematext.logseneapplication/com.sematext.logseneapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.PackageManager android.content.Context.getPackageManager()' on a null object reference
        at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:96)
        at android.app.Activity.requestPermissions(Activity.java:4480)
        at androidx.core.app.ActivityCompat.requestPermissions(ActivityCompat.java:502)
        at com.sematext.logseneandroid.LogseneLocationListener.<init>(LogseneLocationListener.java:31)
        at com.sematext.logseneandroid.Logsene.init(Logsene.java:107)
        at com.sematext.logseneapplication.MainActivity.onCreate(MainActivity.java:25)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
gr0 commented

I can replicate the same behavior on SDK 30. I'll try to push some things aside and work on a fix for that, but I can promise that it will happen this week. Of course, any PRs are welcome :)

gr0 commented

@tlierdotfr btw, for a quick workaround, you can just remove the initialization of location retrieval, by removing the following lines in Logsene class in the init method:

if (logsene.automaticLocationEnabled) {
  logsene.locationListener = new LogseneLocationListener(context);
}

The LogseneLocationListener seems to be the cause of the issue.

@gr0 I managed to make the example work by setting to false the <meta-data android:name="LogseneAutomaticLocationEnabled" android:value="false" /> in my manifest file.

With the example running I was able to find out my WorkManager problem : I have put the logsene.init in the attachBaseContext method of AppDelegate instead of the onCreate.

Now it's working fine ;-)
Thanks for your support !

@gr0 as you have renamed the title of this issue and because the example does not work outOfThebox, I have not closed this issue.
I let you handle it ;-)

gr0 commented

Thanks :) I need to change the location listener and how it interacts with PackageManager, so that the geo location is supported on SDK 29+ :)

gr0 commented

@tlierdotfr I finally had time and released a new version of the library, which is now working with SDK 30 - https://github.com/sematext/sematext-logsene-android/releases/tag/3.2.0. The details about the changes are in the release notes. Once again thanks for reporting that!