0

Does the .apk format handle hardlinks? Or does it simply copy the same file over and over?

I've some a simple test and it seems like hardlinks are not handled, meaning that the size of the generated .apk increases significantly when you have multiple hardlinks to the same file. Is there a way to make the resources point to the same file instead?


The use case is the following: I have several images, which are used in some places in my application and their names contain some information about them. For example I could have an apple.jpg, apple_red.jpg, apple_red_big.jpg, apple_big.jpg, apple_green.jpg etc. My application uses the image that matches the data the most, and thus if the data is about a red, big apple it will use apple_red_big.jpg, while if the application only knows that the data regards apples it will simply use apple.jpg.

At the moment I'm not providing a different file for each resource, so apple_red.jpg is simply an hardlink to apple.jpg. In the future I might decide to add more images and thus apple_red.jpg could have its own image.

Also, since apple.jpg is used as a fallback option, it will pretty much always be a simple hardlink to an other image which happen to be also apple_something.jpg.

How can I avoid duplicating all these images in the final apk?

Bakuriu
  • 98,325
  • 22
  • 197
  • 231
  • possible duplicate of [Can you share resources across APK's?](http://stackoverflow.com/questions/4246015/can-you-share-resources-across-apks) – Ted Hopp Feb 10 '13 at 19:55
  • 1
    @TedHopp I don't see where my question is a duplicate of that one. I have a single application and want to have hardlinks to a single resource inside that application's apk archive. – Bakuriu Feb 11 '13 at 06:52
  • Ah. I had the impression you were trying to share resources between .apk files. Perhaps you could explain what you mean by hardlinks? Do you mean that you have several items in your /res directories that happen to be hard links to the same file? If so, the Android packaging tool will treat them as separate resources. Is there some reason why you can't make do with a resource file? – Ted Hopp Feb 11 '13 at 07:43
  • @TedHopp I've added more information about why I want to have hardlinks. – Bakuriu Feb 11 '13 at 08:10
  • Downvoter should explain why he thought mine was a bad question... – Bakuriu Apr 14 '14 at 11:52

1 Answers1

2

If you provide the same image under different file names (even if they are just hard links), the aapt will package them up as separate resources. Rather than using the file system to create hard links, the Android way of doing what you want is to provide alias resources. The technique is described here in the docs. Basically, you simply create a bitmap drawable that references another drawable:

file res/drawable/apple.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/apple_something" />

where you store apple_something.png in the res/drawable directory. The alias resource takes up just a tiny bit of additional room in the .apk file.

In code, both R.drawable.apple and R.drawable.apple_something would then retrieve the same image (although they would be different Drawable objects); in xml it would be @drawable/apple and @drawable/apple_something.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521