Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – On Android, how are the multiple results of Class.getResources() handled?

Posted on November 11, 2022 By

Solution 1 :

Are the rules around these class resources documented somewhere?

The Android build behavior when creating APKs is described in the Add app resources section of the user guide. Look for the heading on Resource merging.

You are correct that all of the sources are combined into a single namespace. The idea is that resource merging makes the resulting APK smaller1.

Android is not constrained to be Java(tm) compliant, and they have made different design decisions.


1 – An APK is just like a fat JAR or uberJAR in the classic Java world. You would not expect multiple copies of a resource in a fat JAR. The getResources API would only give you a single resource for any name in that context as well.

Problem :

In Java VM, a classloader can have multiple domains, generally JAR files (which are similar to APK files). Each of these domains have a single namespace. That is, every resource has a unique name.

Calling ClassLoader.getResources(“foo”) can, therefore, return multiple URLs. However, looking at Android, the build seems to flatten the resource namespace, overwriting resources with the same path. This wreaks havoc with mechanisms like the Java ServiceLoader. It also seems to void any use of the META-INF/MANIFEST.MF.

For example, if I use liba.jar and libb.jar, and both have a resource OSGI-OPT/com.example.Foo, then only one seems to end up in the APK.

Are the rules around these class resources documented somewhere?

Comments

Comment posted by Peter Kriens

Thx. I agree that Android is not Java but this deviation means in principle that Java libraries can give nasty problems, nor can the getResources() contract be followed. Ah well, don’t think they’ll change it for me though 🙂

READ  [FIXED] smallcaps - fontFeatureSettings=smcp doesn't work in Android
Powered by Inline Related Posts

Comment posted by Stephen C

In response to your comments: 1) There is (should be) no expectation that any Java(tm) library will just run on Android. There are bigger differences than this. 2) The contract is still followed. Just not in the way that a Java(tm) programmer may have expected. 3) You are right about that.

Android Tags:android, apk, dex, embedded-resource, java

Post navigation

Previous Post: [FIXED] android – how to change notification background color in nougat?
Next Post: [FIXED] android – How to make width ratio equal in XML GridView

Related Posts

[FIXED] android – How can I change the background behind the camera cutout and the app switcher? Android
[FIXED] Android method not being called, but log is being printed Android
[FIXED] java – How to change the response code of startActivityForResult in child activity Android
[FIXED] android – How to place elements one below another in constraint layout whose parent is scroll view Android
[FIXED] react native – uploaded an APK or Android App Bundle which has an activity, activity alias, service or broadcast receiver with intent filter, Android
[FIXED] android – How to get the frame of video at specific position continuously in Exoplayer Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme