Older Android system doesn't have the method call addDexPath.And it's not safe to get current data dir from classloader
- Dominant language
- Java
- Stars
- 2k
- Forks
- 262
- PR merge metrics
- No merged PRs in 30d
Description
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 "";
}
```
Contributor guide
Research direction
Locate the Android code that uses addDexPath and obtains the current data directory, then inspect compatibility with older Android systems. Compare the existing behavior with the proposed BaseDexClassLoader.dexElements and ActivityThread.currentPackageName approaches; done means both cases work safely across the stated API levels.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100