EventBus3.0 混淆解决方案

EventBus 3.0采用注解处理,为了提高效率可在编译时通过注解处理器生成索引代码。 但是如果使用索引的话就不能混淆被Subscribe注解的方法。 为了能够提高效率使用索引并且混淆被Subscribe注解的方法可以使_gradle.plugin.greenrobot.eventbus_插件

[TOC]

gradle.plugin.greenrobot.eventbus

JitPack

gradle.plugin.greenrobot.eventbus is a gradle plugin for EventBus when use proguard.

Process

1. hook gradle proguard task.

2. parse mapping.txt.

3. analyse class file.

4. replace method name.

Integration

How to use

1. integration EventBus and EventBusAnnotationProcessor

2. add the jitpack repository and plugin dependency to your project build file

buildscript {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
    dependencies {
        classpath 'com.github.JackyAnn:gradle.plugin.greenrobot.eventbus:v1.0'
    }
}

3. apply plugin to you module build file

apply plugin: 'com.tobelinker.greenrobot.eventbus'

Sample

gradle.plugin.greenrobot.eventbus.sample

  • source code generate by EventBusAnnotationProcessor

    package com.sample.index;

    import org.greenrobot.eventbus.meta.SimpleSubscriberInfo; import org.greenrobot.eventbus.meta.SubscriberMethodInfo; import org.greenrobot.eventbus.meta.SubscriberInfo; import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

    import org.greenrobot.eventbus.ThreadMode;

    import java.util.HashMap; import java.util.Map;

    /** This class is generated by EventBus, do not edit. */ public class BusIndex implements SubscriberInfoIndex { private static final Map<Class, SubscriberInfo> SUBSCRIBER_INDEX; static { SUBSCRIBER_INDEX = new HashMap, SubscriberInfo>(); putIndex(new SimpleSubscriberInfo(com.tobelinker.greenrobot.eventbus.sample.BaseActivity.class, true, new SubscriberMethodInfo[] { new SubscriberMethodInfo("showMessage", com.tobelinker.greenrobot.eventbus.sample.Message.class), new SubscriberMethodInfo("showMessage", com.tobelinker.greenrobot.eventbus.sample.EventMessage.class), })); putIndex(new SimpleSubscriberInfo(com.tobelinker.greenrobot.eventbus.sample.MainActivity.class, true, new SubscriberMethodInfo[] { new SubscriberMethodInfo("showMessage", com.tobelinker.greenrobot.eventbus.sample.Message.class), new SubscriberMethodInfo("showMessage", com.tobelinker.greenrobot.eventbus.sample.EventMessage.class), new SubscriberMethodInfo("showMessage", String.class), new SubscriberMethodInfo("showMessage", Object.class), })); } private static void putIndex(SubscriberInfo info) { SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info); } @Override public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) { SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass); if (info != null) { return info; } else { return null; } } }

  • compiled code whith proguard

    package com.a.a;

    import java.util.; import a.a.a.a.; import com.tobelinker.greenrobot.eventbus.sample.*;

    public class a implements d { private static final Map<Class, c> a; private static void a(final c c) { com.a.a.a.a.put(c.a(), c); } @Override public c a(final Class clazz) { final c c = com.a.a.a.a.get(clazz); if (c != null) { return c; } return null; } static { a = new HashMap<Class<?>, c>(); a(new a.a.a.a.b(com.tobelinker.greenrobot.eventbus.sample.a.class, true, new e[] { new e("showMessage", com.tobelinker.greenrobot.eventbus.sample.c.class), new e("showMessage", b.class) })); a(new a.a.a.a.b(MainActivity.class, true, new e[] { new e("showMessage", com.tobelinker.greenrobot.eventbus.sample.c.class), new e("showMessage", b.class), new e("showMessage", String.class), new e("showMessage", Object.class) })); } }

  • the mapping.txt

    com.sample.index.BusIndex -> com.a.a.a: java.util.Map SUBSCRIBER_INDEX -> a void () -> void putIndex(org.greenrobot.eventbus.meta.SubscriberInfo) -> a org.greenrobot.eventbus.meta.SubscriberInfo getSubscriberInfo(java.lang.Class) -> a void () -> com.tobelinker.greenrobot.eventbus.sample.BaseActivity -> com.tobelinker.greenrobot.eventbus.sample.a: void () -> void showMessage(com.tobelinker.greenrobot.eventbus.sample.Message) -> a void showMessage(com.tobelinker.greenrobot.eventbus.sample.EventMessage) -> a com.tobelinker.greenrobot.eventbus.sample.EventMessage -> com.tobelinker.greenrobot.eventbus.sample.b: java.lang.String message -> a void (java.lang.String) -> java.lang.String toString() -> toString com.tobelinker.greenrobot.eventbus.sample.MainActivity -> com.tobelinker.greenrobot.eventbus.sample.MainActivity: org.greenrobot.eventbus.EventBus eventBus -> a void () -> void onCreate(android.os.Bundle) -> onCreate void onDestroy() -> onDestroy void showMessage(com.tobelinker.greenrobot.eventbus.sample.Message) -> a void showMessage(com.tobelinker.greenrobot.eventbus.sample.EventMessage) -> a void showMessage(java.lang.String) -> a com.tobelinker.greenrobot.eventbus.sample.Message -> com.tobelinker.greenrobot.eventbus.sample.c: java.lang.String message -> a void (java.lang.String) -> java.lang.String toString() -> toString

  • the final code after processed

    package com.a.a;

    import java.util.; import a.a.a.a.; import com.tobelinker.greenrobot.eventbus.sample.*;

    public class a implements d { private static final Map<Class, c> a; private static void a(final c c) { com.a.a.a.a.put(c.a(), c); } @Override public c a(final Class clazz) { final c c = com.a.a.a.a.get(clazz); if (c != null) { return c; } return null; } static { a = new HashMap<Class<?>, c>(); a(new a.a.a.a.b(com.tobelinker.greenrobot.eventbus.sample.a.class, true, new e[] { new e("a", com.tobelinker.greenrobot.eventbus.sample.c.class), new e("a", b.class) })); a(new a.a.a.a.b(MainActivity.class, true, new e[] { new e("a", com.tobelinker.greenrobot.eventbus.sample.c.class), new e("a", b.class), new e("a", String.class), new e("showMessage", Object.class) })); } }

Notice

none

License

Apache License, Version 2.0

Copyright (c) 2016, tobelinker.com

评论

暂无评论