Skip to content

Commit 7013bc2

Browse files
Merge pull request #686 from greenrobot/685-failure-event-fixes
Fix ThrowableFailureEvent ProGuard rule
2 parents f588193 + eba6809 commit 7013bc2

4 files changed

Lines changed: 34 additions & 22 deletions

File tree

EventBus/src/org/greenrobot/eventbus/util/AsyncExecutor.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,18 @@
2323
import java.util.logging.Level;
2424

2525
/**
26-
* Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events of any
27-
* given type (default is {@link ThrowableFailureEvent}).
28-
*
29-
* @author Markus
26+
* Executes an {@link RunnableEx} using a thread pool. Thrown exceptions are propagated by posting failure events.
27+
* By default, uses {@link ThrowableFailureEvent}.
28+
* <p>
29+
* Set a custom event type using {@link Builder#failureEventType(Class)}.
30+
* The failure event class must have a constructor with one parameter of type {@link Throwable}.
31+
* If using ProGuard or R8 make sure the constructor of the failure event class is kept, it is accessed via reflection.
32+
* E.g. add a rule like
33+
* <pre>
34+
* -keepclassmembers class com.example.CustomThrowableFailureEvent {
35+
* &lt;init&gt;(java.lang.Throwable);
36+
* }
37+
* </pre>
3038
*/
3139
public class AsyncExecutor {
3240

@@ -103,24 +111,21 @@ private AsyncExecutor(Executor threadPool, EventBus eventBus, Class<?> failureEv
103111

104112
/** Posts an failure event if the given {@link RunnableEx} throws an Exception. */
105113
public void execute(final RunnableEx runnable) {
106-
threadPool.execute(new Runnable() {
107-
@Override
108-
public void run() {
114+
threadPool.execute(() -> {
115+
try {
116+
runnable.run();
117+
} catch (Exception e) {
118+
Object event;
109119
try {
110-
runnable.run();
111-
} catch (Exception e) {
112-
Object event;
113-
try {
114-
event = failureEventConstructor.newInstance(e);
115-
} catch (Exception e1) {
116-
eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
117-
throw new RuntimeException("Could not create failure event", e1);
118-
}
119-
if (event instanceof HasExecutionScope) {
120-
((HasExecutionScope) event).setExecutionScope(scope);
121-
}
122-
eventBus.post(event);
120+
event = failureEventConstructor.newInstance(e);
121+
} catch (Exception e1) {
122+
eventBus.getLogger().log(Level.SEVERE, "Original exception:", e);
123+
throw new RuntimeException("Could not create failure event", e1);
123124
}
125+
if (event instanceof HasExecutionScope) {
126+
((HasExecutionScope) event).setExecutionScope(scope);
127+
}
128+
eventBus.post(event);
124129
}
125130
});
126131
}

EventBus/src/org/greenrobot/eventbus/util/ThrowableFailureEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
/**
1919
* A generic failure event, which can be used by apps to propagate thrown exceptions.
20+
* Used as default failure event by {@link AsyncExecutor}.
2021
*/
2122
public class ThrowableFailureEvent implements HasExecutionScope {
2223
protected final Throwable throwable;

EventBusPerformance/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ android {
4040
}
4141
}
4242
}
43+
44+
compileOptions {
45+
sourceCompatibility JavaVersion.VERSION_1_8
46+
targetCompatibility JavaVersion.VERSION_1_8
47+
}
4348
}

eventbus-android/consumer-rules.pro

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
}
55
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
66

7-
# And if you use AsyncExecutor:
8-
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
7+
# If using AsyncExecutord, keep required constructor of default event used.
8+
# Adjust the class name if a custom failure event type is used.
9+
-keepclassmembers class org.greenrobot.eventbus.util.ThrowableFailureEvent {
910
<init>(java.lang.Throwable);
1011
}
1112

0 commit comments

Comments
 (0)