saswatanand / ella

Binary Instrumentation of Android Apps
70 stars 17 forks source link

multidex support #9

Open ms1995 opened 6 years ago

ms1995 commented 6 years ago

It seems that current implementation supports instrumenting only one dex in an app.

ms1995 commented 6 years ago

My prototype for fixing this problem in a not-that-elegant way:

  1. Create wrapper classes for the main Ella class in the runtime. Each of those classes will be merged to one additional DEX file. Make sure that those classes have different names. A wrapper class looks like,
public abstract class EllaWrapper {

    private static Method m_method;

    static {
        try {
            Class<?> ella_class = Class.forName("com.apposcopy.ella.runtime.Ella");
            m_method = ella_class.getDeclaredMethod("m", int.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void m(int mId) {
        try {
            m_method.invoke(null, mId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}

Basically, I use Java reflection to get the reference to the main class at very beginning, and subsequent calls to m will use the reference directly.

  1. Modify Ella instrumentation to merge the main Ella classes to classes.dex and wrapper classes to classes2.dex, classes3.dex, etc. This should make sure that main Ella classes are always loaded first (before wrapper classes try to find them via reflection).

  2. Modify Ella instrumentation to target the Ella classes that are included in the respective DEX files when instrumenting each method in the app. Otherwise the instrumentor will say that it cannot find reference to the Ella main class.

One issue of this approach is that, if classes.dex already has too many methods (already reaching the 64K limitation), the approach will not work since main Ella classes cannot be merged into the main DEX file.

Hope this helps.

njusunyi commented 6 years ago

@ms1995 Hi, I'm facing the same problem. Do you have a working prototype? Could you kindly share your code? Thanks a lot!

ms1995 commented 6 years ago

@yisun92 I do have a working prototype and will release it later. Thanks.

ms1995 commented 6 years ago

@yisun92

Please see my working version at,

https://github.com/ms1995/ella-customized

njusunyi commented 6 years ago

@yisun92

Please see my working version at,

https://github.com/ms1995/ella-customized

Thanks! I'll have a try