JeremyLiao / LiveEventBus

:mailbox_with_mail:EventBus for Android,消息总线,基于LiveData,具有生命周期感知能力,支持Sticky,支持AndroidX,支持跨进程,支持跨APP
Apache License 2.0
3.89k stars 541 forks source link

混淆怎么配置? #8

Open wzyzb opened 6 years ago

JeremyLiao commented 6 years ago

请参考: -keep class com.jeremyliao.livedatabus.* { ; } -dontwarn com.jeremyliao.livedatabus.**

liyujiang-gzu commented 6 years ago

由于以下部分使用了反射获取mObservers、mVersion及mLastVersion,所以不能混淆android.arch.lifecycle.LiveData及android.arch.lifecycle.LiveData.ObserverWrapper

        private void hook(@NonNull Observer<T> observer) throws Exception {
            //get wrapper's version
            Class<LiveData> classLiveData = LiveData.class;
            Field fieldObservers = classLiveData.getDeclaredField("mObservers");
            fieldObservers.setAccessible(true);
            Object objectObservers = fieldObservers.get(this);
            Class<?> classObservers = objectObservers.getClass();
            Method methodGet = classObservers.getDeclaredMethod("get", Object.class);
            methodGet.setAccessible(true);
            Object objectWrapperEntry = methodGet.invoke(objectObservers, observer);
            Object objectWrapper = null;
            if (objectWrapperEntry instanceof Map.Entry) {
                objectWrapper = ((Map.Entry) objectWrapperEntry).getValue();
            }
            if (objectWrapper == null) {
                throw new NullPointerException("Wrapper can not be bull!");
            }
            Class<?> classObserverWrapper = objectWrapper.getClass().getSuperclass();
            Field fieldLastVersion = classObserverWrapper.getDeclaredField("mLastVersion");
            fieldLastVersion.setAccessible(true);
            //get livedata's version
            Field fieldVersion = classLiveData.getDeclaredField("mVersion");
            fieldVersion.setAccessible(true);
            Object objectVersion = fieldVersion.get(this);
            //set wrapper's version
            fieldLastVersion.set(objectWrapper, objectVersion);
        }
    }

所以混淆规则为:

-dontwarn android.arch.lifecycle.LiveData
-keep class android.arch.lifecycle.LiveData { *; }
VeHan commented 5 years ago

这个写在README里面吧, @JeremyLiao ,我也踩了这个坑

VeHan commented 5 years ago

混淆规则为: -dontwarn android.arch.lifecycle.LiveData -keep class android.arch.lifecycle.LiveData { ; } -keep class android.arch.lifecycle.LifecycleRegistry { ; } -keep class android.arch.core.internal.SafeIterableMap { *; }

JeremyLiao commented 5 years ago

这个写在README里面吧, @JeremyLiao ,我也踩了这个坑

好的,多谢提醒

JeremyLiao commented 5 years ago

混淆规则为: -dontwarn android.arch.lifecycle.LiveData -keep class android.arch.lifecycle.LiveData { ; } -keep class android.arch.lifecycle.LifecycleRegistry { ; } -keep class android.arch.core.internal.SafeIterableMap { *; }

赞一个

codyer commented 5 years ago

因为Android对反射加强管理了,版本兼容容易出现问题,所以一直不太希望代码中过多的使用反射去实现功能,所以这几天参考作者的思路对项目进行了重构,重构基于作者的两个项目进行了合并,如有好的建议欢迎大家一起讨论

33

https://github.com/codyer/LiveEventBus