Pergunta

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?

Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top