dexmaker icon indicating copy to clipboard operation
dexmaker copied to clipboard

Older Android system doesn't have the method call addDexPath.And it's not safe to get current data dir from classloader

Open qtiuto opened this issue 7 years ago • 0 comments

A solution for the first is to set the dexElements field of the dexPathList Field in the BaseDexClassLoader.class,It's ok after api 4. And a better solution for the second is to invoke the static method currentPackageName() in ActivityThread.class. Here's the code:

private static String getPackageName(){
        try {
            if(sCurrentPackageName==null) sCurrentPackageName=Class.forName("android.app.ActivityThread").
                    getDeclaredMethod("currentPackageName");
            if (Build.VERSION.SDK_INT>=18||Looper.getMainLooper()==Looper.myLooper())
                return String.valueOf(sCurrentPackageName.invoke(null));
            else {
                final String[] ret=new String[1];
                Handler handler=new Handler(Looper.getMainLooper(), new Handler.Callback() {
                    @Override
                    public boolean handleMessage(Message msg) {
                        try {
                            ret[0]= (String) sCurrentPackageName.invoke(null);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        synchronized (ret){
                            ret.notify();
                        }
                        return true;
                    }
                });
                handler.sendEmptyMessage(0);
                synchronized (ret){
                    ret.wait();
                }
                return ret[0];
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

qtiuto avatar Jan 29 '18 15:01 qtiuto