EventBus3.0 混淆解決方案

EventBus 3.0採用註解處理,爲了提升效率可在編譯時經過註解處理器生成索引代碼。 可是若是使用索引的話就不能混淆被Subscribe註解的方法。 爲了可以提升效率使用索引而且混淆被Subscribe註解的方法可使***gradle.plugin.greenrobot.eventbus***插件html

[TOC]java

gradle.plugin.greenrobot.eventbus

JitPack

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

Process

1. hook gradle proguard task.git

2. parse mapping.txt.github

3. analyse class file.apache

4. replace method name.app

Integration

How to use

1. integration EventBus and EventBusAnnotationProcessormaven

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

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 filegradle

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

相關文章
相關標籤/搜索