snowplow / snowplow-android-tracker

Snowplow event tracker for Android. Add analytics to your Android apps and games
http://snowplowanalytics.com
Apache License 2.0
110 stars 63 forks source link

Using global context when creating a tracker causes crashes on API 23 #601

Closed frodstan closed 1 year ago

frodstan commented 1 year ago

Describe the bug Initializing the sdk with a global context on Android 6 throws an exception. I have tried both with a static context and with context generator.

FATAL EXCEPTION: main Process: com.example.snowplow_test, PID: 17105 java.lang.NoClassDefFoundError: com.snowplowanalytics.core.globalcontexts.GlobalContextPluginConfiguration$$ExternalSyntheticLambda0 at com.snowplowanalytics.core.globalcontexts.GlobalContextPluginConfiguration.getEntitiesConfiguration(GlobalContextPluginConfiguration.kt:28) at com.snowplowanalytics.snowplow.configuration.PluginConfigurationKt.toStateMachine(PluginConfiguration.kt:62) at com.snowplowanalytics.core.tracker.ServiceProvider$makeTracker$builder$1.invoke(ServiceProvider.kt:293) at com.snowplowanalytics.core.tracker.ServiceProvider$makeTracker$builder$1.invoke(ServiceProvider.kt:262) at com.snowplowanalytics.core.tracker.Tracker.<init>(Tracker.kt:356) at com.snowplowanalytics.core.tracker.ServiceProvider.makeTracker(ServiceProvider.kt:297) at com.snowplowanalytics.core.tracker.ServiceProvider.getOrMakeTracker(ServiceProvider.kt:187) at com.snowplowanalytics.core.tracker.ServiceProvider.<init>(ServiceProvider.kt:87) at com.snowplowanalytics.snowplow.Snowplow.createTracker(Snowplow.kt:237) at com.example.snowplow_test.MainActivity.initializeSnowplow(MainActivity.kt:68) at com.example.snowplow_test.MainActivity.onCreate(MainActivity.kt:37)

To Reproduce To reproduce this issue, create a new Android app from template with minimum Android SDK 23.

Add snowplow library (version 5.0.0 - 5.1.0, 4.x.x has worked for us previously).

Initialize the sdk in MainActivity onCreate() with a global context sent to Snowplow.createTracker() Pasting in from documentation here will cause the crash: https://docs.snowplow.io/docs/collecting-data/collecting-from-own-applications/mobile-trackers/custom-tracking-using-schemas/global-context/#static-entities

Run the app on a device or emulator with Android 6

Expected behavior Snowplow will have initialized and is ready to send events

Device information (please complete the following information):

matus-tomlein commented 1 year ago

Hi @frodstan,

I think this is caused by the fact that the global context implementation uses Java 8 features underneath.

In order to make this work on older Android SDKs, you will need to add Java 8 desugaring support to your app. You can follow the instructions here. Basically you need to use Android Gradle Plugin v4 and add this to your build.gradle:

android {
    compileOptions {
        // Flag to enable support for the new language APIs
        coreLibraryDesugaringEnabled true
        // Sets Java compatibility to Java 8
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
}

You can see the build.gradle for our demo app which has min SDK version set to 21 and works with global context as an example.

Hope this helps! Let us know if you run into any problems.

frodstan commented 1 year ago

Awesome, this fixed the issue. Thank you!

robertossan commented 1 year ago

Hi @matus-tomlein ,

I know that this problem is already "solved" using the proposed workaround, but Is there any other way to do it that does not involve having to add another dependency other than snowplow?

I´d prefer not having to include this third dependency