Open wzyzb opened 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 { *; }
这个写在README里面吧, @JeremyLiao ,我也踩了这个坑
混淆规则为: -dontwarn android.arch.lifecycle.LiveData -keep class android.arch.lifecycle.LiveData { ; } -keep class android.arch.lifecycle.LifecycleRegistry { ; } -keep class android.arch.core.internal.SafeIterableMap { *; }
这个写在README里面吧, @JeremyLiao ,我也踩了这个坑
好的,多谢提醒
混淆规则为: -dontwarn android.arch.lifecycle.LiveData -keep class android.arch.lifecycle.LiveData { ; } -keep class android.arch.lifecycle.LifecycleRegistry { ; } -keep class android.arch.core.internal.SafeIterableMap { *; }
赞一个
因为Android对反射加强管理了,版本兼容容易出现问题,所以一直不太希望代码中过多的使用反射去实现功能,所以这几天参考作者的思路对项目进行了重构,重构基于作者的两个项目进行了合并,如有好的建议欢迎大家一起讨论
请参考: -keep class com.jeremyliao.livedatabus.* { ; } -dontwarn com.jeremyliao.livedatabus.**