greenrobot / EventBus

Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.
http://greenrobot.org/eventbus/
Apache License 2.0
24.68k stars 4.66k forks source link

Androidx migration #627

Closed mdzyuba closed 2 years ago

mdzyuba commented 4 years ago

Hello,

I would like to contribute an AndroidX upgrade as well as the project upgrade to the Android library (aar).

Thank you,

Mykola

RocketRider commented 4 years ago

This would be great, as I am trying to remove the jetifier in my project.

eku commented 4 years ago

@mdzyuba What is the benefit of this change? Can EventBus still be used with older environments or is an upgrade to AndroidX forced, which I found unacceptable.

mdzyuba commented 4 years ago

@mdzyuba What is the benefit of this change? Can EventBus still be used with older environments or is an upgrade to AndroidX forced, which I found unacceptable.

The main benefit of this upgrade is to make EventBus compatible with projects that are using current Android libraries (AndroidX). This change does not restrict EventBus for older environments.

Currently EventBus depends on Android Support Libraries v.4.1.1.4 released in 2015. Google has deprecated Support Libraries and released an open source alternative - AndroidX in 2018. In order to keep the applications up to date with latest Android APIs, Android developers have to upgrade their projects to AndroidX. The process of migration to AndroidX is described here: https://developer.android.com/jetpack/androidx/migrate. Mixing Support Libraries and AndroidX libraries in any level of the dependency graph is problematic and not always possible. The Android Support Libraries classes had been moved to new Java packages under androidx.*. The new Android gradle plugin provides a Jetifier tool that migrates classes in Support Libraries v.28 to AndroidX v1.0 at the build time. However, is does not work well for older Android Support libraries, which is the case with EventBus. Here is the current code:

https://github.com/greenrobot/EventBus/blob/master/EventBus/build.gradle#L27 dependencies { provided 'com.google.android:android:4.1.1.4'

Here is the Maven repository with details on the library release date: https://mvnrepository.com/artifact/com.google.android/android/4.1.1.4 Date | (Dec 28, 2015)

If we want EventBus to be useful for the current Android development, we have to update its dependencies and migrate the code to AndroidX. This is the main goal of this pull request.

Malachiasz commented 4 years ago

Any update on that? Next to incremental build, dropping android support libs is the most important change to keep the library from dying.

greenrobot-team commented 4 years ago

Associated issue: https://github.com/greenrobot/EventBus/issues/552

andob commented 3 years ago

@mdzyuba "Mixing Support Libraries and AndroidX libraries in any level of the dependency graph is problematic and not always possible."

what do you mean by that? can you give us some examples?

since 2018 until now I have been using EventBus with Jetifier extensively, got absolutely no problems.

mdzyuba commented 3 years ago

@mdzyuba "Mixing Support Libraries and AndroidX libraries in any level of the dependency graph is problematic and not always possible."

what do you mean by that? can you give us some examples?

since 2018 until now I have been using EventBus with Jetifier extensively, got absolutely no problems.

Yes, Jetifier works and can be used. However, there are a few reasons not to do that. Here are some.

Generally, mixing two versions of the same library used by different dependencies in the same apk leads to build time issues and requires extra work on resolving conflicts in dependencies.

Mixing different versions of the same android support libraries is not safe. It can lead to crashes. Please see https://stackoverflow.com/questions/42374151/all-com-android-support-libraries-must-use-the-exact-same-version-specification and https://stackoverflow.com/questions/53938393/android-studio-library-error-mixing-versions-can-lead-to-runtime-crashes-and-a for more details.

Even if mixing libs does not lead to crashes, it leads to duplication of code. Removing the duplication in builds takes extra work. Also, running Jetifier takes time. This post https://adambennett.dev/2020/08/disabling-jetifier/ shows that disabling Jetifier improves build speed.

Jetifier was designed as a temporary solution to help with transitioning from android support libs to androidX. There is no reason to keep using it if the project can move to AndroidX.

ooyellow58 commented 2 years ago

it is a pity not to migrate to AndroidX

greenrobot-team commented 2 years ago

@ooyellow58 See https://github.com/greenrobot/EventBus/issues/552#issuecomment-982645670 A preview release with Android X support is now available.