Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qtiuto opened this issue Jan 29, 2018 · 0 comments

Comments

@qtiuto
Copy link

qtiuto commented Jan 29, 2018

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 "";
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant