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

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<Class<?>, 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 <init>() -> <init>
    void putIndex(org.greenrobot.eventbus.meta.SubscriberInfo) -> a
    org.greenrobot.eventbus.meta.SubscriberInfo getSubscriberInfo(java.lang.Class) -> a
    void <clinit>() -> <clinit>
com.tobelinker.greenrobot.eventbus.sample.BaseActivity -> com.tobelinker.greenrobot.eventbus.sample.a:
    void <init>() -> <init>
    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 <init>(java.lang.String) -> <init>
    java.lang.String toString() -> toString
com.tobelinker.greenrobot.eventbus.sample.MainActivity -> com.tobelinker.greenrobot.eventbus.sample.MainActivity:
    org.greenrobot.eventbus.EventBus eventBus -> a
    void <init>() -> <init>
    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 <init>(java.lang.String) -> <init>
    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

转载于:https://my.oschina.net/jackyanngo/blog/776775

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值