Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate EventBus Android artifact #670

Merged
merged 25 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
bf8e861
removed error dialog classes
andob May 10, 2021
9153576
android sdk proxy
andob May 10, 2021
b8a44d8
android sdk proxy implementation
andob May 10, 2021
21c6b56
refactorings
andob May 10, 2021
53f24c5
Update Gradle [6.7.1 -> 6.8.3]
greenrobot-team Jun 21, 2021
a396776
Downgrade Android plugin to work with IntelliJ IDEA.
greenrobot-team Jun 21, 2021
1d5e79a
CI: use GitHub Actions.
greenrobot-team Jun 21, 2021
b74299b
Embed ProGuard rules for Android.
greenrobot-team Jun 21, 2021
3f4ee0c
Use common compile SDK for eventbus-android.
greenrobot-team Jun 21, 2021
51a5c05
Switch to maven-publish plugin.
greenrobot-team Jun 22, 2021
831071e
Start development of next version.
greenrobot-team Jun 22, 2021
a110323
Make eventbus-java an exposed dependency of eventbus.
greenrobot-team Jun 22, 2021
bc0b3f3
Rename EventBus project so it is added as dependency in POM.
greenrobot-team Jun 22, 2021
740a862
Update Android references and messages.
greenrobot-team Jun 22, 2021
df94b23
Update Java test projects.
greenrobot-team Jun 22, 2021
adc7b9f
Drop explicit eventbus-java dependency from Android test projects.
greenrobot-team Jun 22, 2021
45352be
Re-add deprecated test classes for Android.
greenrobot-team Jun 22, 2021
e573ffa
Regression: correctly check for Android main thread.
greenrobot-team Jun 29, 2021
5efb61f
Regression: restore AndroidLogger log output.
greenrobot-team Jun 29, 2021
8980e2d
Regression: Remove added suppression.
greenrobot-team Jun 29, 2021
ece8c27
Publishing: add license back to POM.
greenrobot-team Jul 13, 2021
2d6910e
Update README
greenrobot-team Jul 13, 2021
d74c3ba
Prevent removal or renaming of AndroidComponentsImpl.
greenrobot-team Jul 13, 2021
ee34ff6
Add README for Android library.
greenrobot-team Jul 13, 2021
846e665
Drop AndroidComponentsAvailabilityOnJavaTest, ensured implicitly by o…
greenrobot-team Jul 13, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will build a Java project with Gradle
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle

name: Java CI with Gradle

on:
push:
pull_request:

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Cache Gradle packages
uses: actions/cache@v2
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Gradle Info
run: ./gradlew -version
- name: Build with Gradle
run: ./gradlew build
- name: Cleanup Gradle Cache
# Remove some files from the Gradle cache, so they aren't cached by GitHub Actions.
# Restoring these files from a GitHub Actions cache might cause problems for future builds.
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
35 changes: 0 additions & 35 deletions .travis.yml

This file was deleted.

49 changes: 20 additions & 29 deletions EventBus/build.gradle
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
apply plugin: 'java'

archivesBaseName = 'eventbus'
group = 'org.greenrobot'
version = '3.2.0'
sourceCompatibility = 1.7

dependencies {
compileOnly 'com.google.android:android:4.1.1.4'
compileOnly 'com.google.android:android-test:4.1.1.4'
compileOnly 'com.google.android:annotations:4.1.1.4'
compileOnly 'com.google.android:support-v4:r7'
}
version = '3.3.0-SNAPSHOT'
java.sourceCompatibility = JavaVersion.VERSION_1_8
java.targetCompatibility = JavaVersion.VERSION_1_8

sourceSets {
main {
Expand All @@ -21,39 +14,37 @@ sourceSets {
}
}

apply from: rootProject.file("gradle/publish.gradle")

javadoc {
failOnError = false
title = "EventBus ${version} API"
options.bottom = 'Available under the Apache License, Version 2.0 - <i>Copyright &#169; 2012-2020 <a href="https://greenrobot.org">greenrobot.org</a>. All Rights Reserved.</i>'
}

task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
archiveClassifier.set("javadoc")
from 'build/docs/javadoc'
}

task sourcesJar(type: Jar) {
archiveClassifier.set("sources")
from sourceSets.main.allSource
classifier = 'sources'
}

artifacts {
archives jar
archives javadocJar
archives sourcesJar
}

uploadArchives {
repositories {
mavenDeployer {
// Common setup is defined in publish.gradle.

pom.project {
name 'EventBus'
description 'EventBus is a publish/subscribe event bus optimized for Android.'
}
apply from: rootProject.file("gradle/publish.gradle")
// Set project-specific properties
afterEvaluate {
publishing.publications {
mavenJava(MavenPublication) {
artifactId = "eventbus-java"

from components.java
artifact javadocJar
artifact sourcesJar
pom {
name = "EventBus"
description = "EventBus is a publish/subscribe event bus."
packaging = "jar"
}
}
}
}
7 changes: 7 additions & 0 deletions EventBus/src/org/greenrobot/eventbus/EventBus.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.greenrobot.eventbus;

import org.greenrobot.eventbus.android.AndroidDependenciesDetector;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -139,6 +140,12 @@ public EventBus() {
* ThreadMode} and priority.
*/
public void register(Object subscriber) {
if (AndroidDependenciesDetector.isAndroidSDKAvailable() && !AndroidDependenciesDetector.areAndroidComponentsAvailable()) {
// Crash if the user (developer) has not imported the Android compatibility library.
throw new RuntimeException("It looks like you are using EventBus on Android, " +
"make sure to add the \"eventbus\" Android library to your dependencies.");
}

Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
Expand Down
23 changes: 4 additions & 19 deletions EventBus/src/org/greenrobot/eventbus/EventBusBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package org.greenrobot.eventbus;

import android.os.Looper;

import org.greenrobot.eventbus.android.AndroidLogger;
import org.greenrobot.eventbus.android.AndroidComponents;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -146,8 +143,7 @@ public EventBusBuilder addIndex(SubscriberInfoIndex index) {
/**
* Set a specific log handler for all EventBus logging.
* <p/>
* By default all logging is via {@link android.util.Log} but if you want to use EventBus
* outside the Android environment then you will need to provide another log target.
* By default, all logging is via {@code android.util.Log} on Android or System.out on JVM.
*/
public EventBusBuilder logger(Logger logger) {
this.logger = logger;
Expand All @@ -165,24 +161,13 @@ Logger getLogger() {
MainThreadSupport getMainThreadSupport() {
if (mainThreadSupport != null) {
return mainThreadSupport;
} else if (AndroidLogger.isAndroidLogAvailable()) {
Object looperOrNull = getAndroidMainLooperOrNull();
return looperOrNull == null ? null :
new MainThreadSupport.AndroidHandlerMainThreadSupport((Looper) looperOrNull);
} else if (AndroidComponents.areAvailable()) {
return AndroidComponents.get().defaultMainThreadSupport;
} else {
return null;
}
}

static Object getAndroidMainLooperOrNull() {
try {
return Looper.getMainLooper();
} catch (RuntimeException e) {
// Not really a functional Android (e.g. "Stub!" maven dependencies)
return null;
}
}

/**
* Installs the default EventBus returned by {@link EventBus#getDefault()} using this builders' values. Must be
* done only once before the first usage of the default EventBus.
Expand Down
20 changes: 5 additions & 15 deletions EventBus/src/org/greenrobot/eventbus/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.greenrobot.eventbus;

import android.os.Looper;
import org.greenrobot.eventbus.android.AndroidLogger;

import org.greenrobot.eventbus.android.AndroidComponents;
import java.util.logging.Level;

public interface Logger {
Expand Down Expand Up @@ -64,19 +62,11 @@ public void log(Level level, String msg, Throwable th) {

class Default {
public static Logger get() {
// also check main looper to see if we have "good" Android classes (not Stubs etc.)
return AndroidLogger.isAndroidLogAvailable() && getAndroidMainLooperOrNull() != null
? new AndroidLogger("EventBus") :
new Logger.SystemOutLogger();
}

static Object getAndroidMainLooperOrNull() {
try {
return Looper.getMainLooper();
} catch (RuntimeException e) {
// Not really a functional Android (e.g. "Stub!" maven dependencies)
return null;
if (AndroidComponents.areAvailable()) {
return AndroidComponents.get().logger;
}

return new SystemOutLogger();
}
}

Expand Down
22 changes: 0 additions & 22 deletions EventBus/src/org/greenrobot/eventbus/MainThreadSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package org.greenrobot.eventbus;

import android.os.Looper;

/**
* Interface to the "main" thread, which can be whatever you like. Typically on Android, Android's main thread is used.
*/
Expand All @@ -25,24 +23,4 @@ public interface MainThreadSupport {
boolean isMainThread();

Poster createPoster(EventBus eventBus);

class AndroidHandlerMainThreadSupport implements MainThreadSupport {

private final Looper looper;

public AndroidHandlerMainThreadSupport(Looper looper) {
this.looper = looper;
}

@Override
public boolean isMainThread() {
return looper == Looper.myLooper();
}

@Override
public Poster createPoster(EventBus eventBus) {
return new HandlerPoster(eventBus, looper, 10);
}
}

}
2 changes: 1 addition & 1 deletion EventBus/src/org/greenrobot/eventbus/Poster.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @author William Ferguson
*/
interface Poster {
public interface Poster {

/**
* Enqueue an event to be posted for a particular subscription.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.greenrobot.eventbus.android;

import org.greenrobot.eventbus.Logger;
import org.greenrobot.eventbus.MainThreadSupport;

public abstract class AndroidComponents {

private static final AndroidComponents implementation;

static {
implementation = AndroidDependenciesDetector.isAndroidSDKAvailable()
? AndroidDependenciesDetector.instantiateAndroidComponents()
: null;
}

public static boolean areAvailable() {
return implementation != null;
}

public static AndroidComponents get() {
return implementation;
}

public final Logger logger;
public final MainThreadSupport defaultMainThreadSupport;

public AndroidComponents(Logger logger, MainThreadSupport defaultMainThreadSupport) {
this.logger = logger;
this.defaultMainThreadSupport = defaultMainThreadSupport;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.greenrobot.eventbus.android;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@SuppressWarnings("TryWithIdenticalCatches")
public class AndroidDependenciesDetector {

public static boolean isAndroidSDKAvailable() {

try {
Class<?> looperClass = Class.forName("android.os.Looper");
Method getMainLooper = looperClass.getDeclaredMethod("getMainLooper");
Object mainLooper = getMainLooper.invoke(null);
return mainLooper != null;
}
catch (ClassNotFoundException ignored) {}
catch (NoSuchMethodException ignored) {}
catch (IllegalAccessException ignored) {}
catch (InvocationTargetException ignored) {}

return false;
}

private static final String ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME = "org.greenrobot.eventbus.android.AndroidComponentsImpl";

public static boolean areAndroidComponentsAvailable() {

try {
Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);
return true;
}
catch (ClassNotFoundException ex) {
return false;
}
}

public static AndroidComponents instantiateAndroidComponents() {

try {
Class<?> impl = Class.forName(ANDROID_COMPONENTS_IMPLEMENTATION_CLASS_NAME);
return (AndroidComponents) impl.getConstructor().newInstance();
}
catch (Throwable ex) {
return null;
}
}
}
Loading