This is the first time I've used the code obfuscation feature provided by Android Studio, and I got a bug. When I enabled code obfuscation, the compilation run reported an error.If I turn off code obfuscation, it works fine.How can I solve this problem?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.systemui, PID: 10439
java.lang.IllegalAccessError: tried to access field android.os.Looper.sThreadLocal from class org.greenrobot.eventbus.android.AndroidDependenciesDetector
at org.greenrobot.eventbus.android.AndroidDependenciesDetector.isAndroidSDKAvailable(AndroidDependenciesDetector.java:12)
at org.greenrobot.eventbus.android.AndroidComponents.<clinit>(AndroidComponents.java:11)
at org.greenrobot.eventbus.Logger$Default.get(Logger.java:65)
at org.greenrobot.eventbus.EventBusBuilder.getLogger(EventBusBuilder.java:157)
at org.greenrobot.eventbus.EventBus.<init>(EventBus.java:114)
at org.greenrobot.eventbus.EventBus.<init>(EventBus.java:110)
at org.greenrobot.eventbus.EventBus.getDefault(EventBus.java:88)
at com.android.systemui.MainActivity.registerEventBus(MainActivity.java:81)
at com.android.systemui.MainActivity.onCreate(MainActivity.java:70)
at android.app.Activity.performCreate(Activity.java:5281)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
The error points to registering the Eventbus method.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerEventBus();
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
init();
Intent intent = new Intent(this, SystemService.class);
startService(intent);
}
private void registerEventBus() {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
}
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onMessageEvent(UpdateMessage message){
EventBus.getDefault().removeStickyEvent(message);
updateTime();
}
in build.gradle
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
in proguard-rules.pro
-keepattributes *Annotation*
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
-keep class android.**{*;}