Android: “Class loader may fail for processes that host multiple applications”【转】
来源:互联网 发布:车辆识别软件 手机 编辑:程序博客网 时间:2024/06/07 14:52
new DefaultHttpClient().execute(httpRequest);报错了:
W/ActivityThread(26997): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
而且是个别机器报错。
网上搜到一个解决方案,记录一下,以后对java的class loader需要做更加深入的研究。
Background information
The message means that Android has setup a dummy ClassLoader
with Thread.currentThread().setContextClassLoader()
, and something tries to use that dummy class loader. Thesomething can be a lot of things, it's hard to tell exactly what from the information given. There is a trick you can try though, see below. Anyway, Android sets up the dummy class loader when there is a risk that the process might contain code from more than one APK. More specifically, Android looks in your manifest if you have usedandroid:sharedUserId
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... android:sharedUserId="triggers.dummy.loader" >
or if you run in a non-standard android:process
<application android:process="triggers.dummy.loader">
How to get rid of the warning
There are two things you can do to get rid of the warning:
- Don't use
android:sharedUserId
orandroid:process
- Explicitly set what APK
ClassLoader
to use before running any other code
To go with solution 2, there are some key insights you need. First, for any classAnyClass
in an APK, AnyClass.class.getClassLoader()
will return the sameClassLoader
. Second,
AnyClass obj = new AnyClass();Thread.currentThread().setContextClassLoader(obj.getClass().getClassLoader())
is the same as
Thread.currentThread().setContextClassLoader(AnyClass.class.getClassLoader())
Third, you need to call Thread.currentThread().setContextClassLoader(getClass().getClassLoader())
before the code that callsThread.currentThread().getContextClassLoader()
. Fourth, When many APKs are involved, you need to callThread.setContextClassLoader(getClass().getClassLoader())
after the last APK has been loaded (otherwise loading the last APK will overwrite what you have set manually). Because of that, it would be a good idea to find out who is using the context class loader in you case by using the below debug trick. Then, right before that, you callThread.setContextClassLoader(getClass().getClassLoader())
for a class from the desired APK, typically the APK that is loaded first (or, in the case when only one APK is involved, that APK ;). Fifth, the context class loader isper thread, which you need to keep in mind if your application is multi-threaded.
Debug trick
If you want to find out what code that calls ClassLoader.getResources(), this should work:
Thread.currentThread().setContextClassLoader(new ClassLoader() { @Override public Enumeration<URL> getResources(String resName) throws IOException { Log.i("Debug", "Stack trace of who uses " + "Thread.currentThread().getContextClassLoader()." + "getResources(String resName):", new Exception()); return super.getResources(resName); }});
if you do this early enough, you should see in the logcat a stack trace that goes back to whoever callsgetResources()
on the dummy class loader.
- Android: “Class loader may fail for processes that host multiple applications”【转】
- Multiple Processes
- multithread and Multiple processes
- Oracle Applications Multiple Organizations Access Control for Custom Code
- Native C applications for Android
- Memory Analysis for Android Applications
- Memory Analysis for Android Applications
- Memory Analysis for Android Applications
- MSBuild 4.0 or VS2010 may fail to compile a 32bit application targeted for .Net Framework
- Covering multiple processes(Purify/Purecov)
- Android挂断电话以及Java Class Loader
- Class Loader
- Class Loader
- Class loader
- Class Loader
- Class Loader
- Host key verification fail…
- Android universal image Loader for android
- Leetcode-Best Time to Buy and Sell Stock -java
- 滚动条弹窗固定位置显示
- build/core/base_rules.mk:166: *** frameworks/base/opengl /libs: MODULE.TARGET.ETC.egl.cfg already de
- 黑马程序员—package
- mysql命令行
- Android: “Class loader may fail for processes that host multiple applications”【转】
- SDUT2876_走楼梯(大数)
- neicun
- oracle默认监听端口更改
- uva 11127 - Triple-Free Binary Strings
- 大话数据结构——读书笔记(2)
- Service的自我认知
- ssh连接 反应慢的解决办法
- 97 调用其他程序中Activity