2

a simple question but Ive realised im not sure of the answer for this one....

If I am creating an android application with a library package named

com.example.one

and then i create another app and include another package with the name

com.example.one

which has a slightly refactored class, could this cause any problems in either of the apps?

The reason i ask is recently I had a problem with some google source code and it was down to the fact that a device manufactorer had included the same libs in the custom OS that I had used in my apk, and it was not happy! (or so i was told)

If anyone can fill me in here, as i obviosuly dont understand something quite findamental here :)

thankx

EDIT: a good link on the diff between Android and Java package names http://blog.javia.org/android-package-name/

4 Answers 4

4

The classloader can't load two versions of the same class. It picks just one. But which one gets picked is undefined. So yes - it causes troubles.

Some platforms (java-ee) have options to specify jar precendence for these cases. I don't know about android.

Update: If my initial understanding is not correct, i.e. you are not having the same jar (library) twice on the classpath, but instead are starting 2 separate apps with different versions of the jar - then they won't interfere with each other (and hence no problems)

7
  • ok so it can cause problems, so if i write any reusable library code I should always make sure that it's interface does not change in case of breaking any clients that use the older versions?
    – Dori
    Dec 2, 2010 at 14:10
  • that's a wider discussion. You can change an API if the clients can simply replace the jar. If a jar ships with a platform and can't be changed, then you must not change the API.
    – Bozho
    Dec 2, 2010 at 14:12
  • wait, but what if one client on the platform replaces the jar and one does not, how does either client know which version will be loaded?
    – Dori
    Dec 2, 2010 at 14:18
  • well, it depends what the platform is. Is it an android phone, a cloud environment.. ?
    – Bozho
    Dec 2, 2010 at 14:25
  • 1
    @Bozho - I think the question was misread - 2 separate apps are using two different versions of the same .jar - that shouldn't cause any problems in Android as I believe the entire app is loaded in it's own sandbox (there's no sharing of .jar files across apps) - see the answer from user149316 below.
    – KevinDTimm
    Dec 2, 2010 at 14:25
3

Actually, it shouldn't cause problems in the case of two separate Android apps. Android Apps run in Sandboxes, i.e. the Classloader of app A does not see any classes of app B and vice-versa. This is obviously different for system wide-libraries. They are accessible by the classloader (of course) and will cause troubles if you have the same class in your app.

2
  • @user149316 - apparently you read the whole question while I just glossed over some of the words (and missing the real meaning). kudos and props!
    – KevinDTimm
    Dec 2, 2010 at 14:24
  • thanks to all for the answers. This explains the problemt si had before and also means i could create my library code (for multiple apps) with peace of mind, thank you :)
    – Dori
    Dec 2, 2010 at 15:11
3

Yes, this can cause problems, but only if you have classnames that conflict within the package.

This doesn't seem to be an issue with your app though as you have two apps and two libraries (see answer from LordT)

0

Any easy workaround is to include the the app/library name in the package. For example:

com.app1.example.one
com.app2.example.one

That eliminates confusion & interference between the two packages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.