How does the classloader choose between multiple classes with the same package and name
I am writing an Android project that needs to use the Apache HttpClient from version 4.1 of the project. I am including this in the libs folder of the project as a jar file. My question is how does the class loader know to use the version 4 class instead of the platform supplied version 3, as they both have the same开发者_开发百科 package name and the same import statement?
I encountered a problem when using a GSON library in the past which clashed with the one included by HTC see here and wonder how these situations differ (maybe because gson is using reflection?)? Any info would be great thanks.
The classloader looks in his classes first and after that the parent classloader is asked. Thr problem may be that the parent classloader has a patched class that works in android, while the version you are providing is not. This may lead to problems.
Long story short: Classloader are stored in a tree and when the class is not found the parent classloader is asked. (custom classloaders may do something different!)
HttpClient 4 and HttpClient 3 are two totally different libraries. HttpClient 4 is a total rewrite and was designed to follow many RFC's available.
HttpClient 4 is not backward compatible to HttpClient 3.
I don't know about Android but if it's similar to how JBoss it does, for example, then you have custom class loaders that first look in the app classpath and if nothing is found there they'd check the system classpath. As I said, I'm not sure the same is true for Android, though.
精彩评论