Ant Build AAPT Crunch is stripping draw9 information from Library resource images

1.8k views Asked by At

I have a project that is using a library project. The Library Project has draw9 (9.png) files and when building the apk via Ant it is stripping the draw9 info on scaleable and fillable areas. The result is that the app just stretches the images without using draw9 info. In Android's build.xml

 <target name="-package-resources" depends="-crunch">

this is calling crunch, which updates the pre-processed PNG cache (anyone know where this cache is held?).

-crunch runs a task in aapt called crunch. But I have not been able to find any information on the arguments. It seems to be taking files from the Res folder and putting it in the out res folder (by default bin/res).

So I even cleared the task so it would not run crunch, and still images weren't stretched properly. I then replaced it with a straight copying of the png files to the out res folder, and still same issue. I even tried ant clean in between these tasks and I got the same problem. Which leads me to believe the cache is being held outside the project area, but where?

Note when I build it with Eclipse all is fine.

The other solution that works for me is to copy these images from the Library into the Main Project, but it's not the ideal solution.

Someone else seems to have had issues with this crunch/resource packaging with aapt in ant build uses cache from other projects

In this bug report they mention about the cache only using timestamps to check whether to update the cache or not.

This seems to be the source code for aapt, but I had trouble trying to find where this cache is held from reading it.

Figure out the problem

For me what was causing the problem was that I was using different folders (intentionally) in Ant for my res, src and bin. There is a refernce project.library.res.folder.path which gets filled in dependency macro. It points to the default res, and bin folders from the project. So what I did was to change them to point my new folders. But in doing that I was changing the older they were listed. It started mixing up the resource files into different library project I was using when aapt was being run. When I fixed the order it was compiling fine. I also did it for project.library.bin.r.file.path.

2

There are 2 answers

1
AndroidGecko On BEST ANSWER

I've had exactly the same problem yesterday.

I have one codebase which I use to build different flavours of the app (diff package-name and some specific resources) using ant.

build.xml worked fine but 9patches were corrupted while using app (as you say, treated as png and stretched).

I nailed down the problem to -crunch task within ant (just as you did).

In my case I use two different res folders so I changed original crunch task from this

<!-- Updates the pre-processed PNG cache -->
    <target name="-crunch">
        <exec executable="${aapt}" taskName="crunch">
            <arg value="crunch" />
            <arg value="-v" />
        <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-C" />
            <arg path="${out.res.absolute.dir}" />
        </exec>
    </target>

to this

 <target name="-crunch">
        <exec executable="${aapt}" taskName="crunch">
            <arg value="crunch" />
            <arg value="-v" />
            <arg value="-S" />
            <arg path="${resource.flavour.dir}" />
            <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-C" />
            <arg path="${out.res.absolute.dir}" />
        </exec>
    </target>

but this produced mentioned 9patch artefacts (also in libraries like fb) .. so I thought I will change this to process resourceflavour.dir only if this is not a library which gave proper result (no 9pathc artefacts)

<target name="-crunch">
    <do-only-if-not-library elseText="Library project: do not package resources..." >
        <exec executable="${aapt}" taskName="crunch">
            <arg value="crunch" />
            <arg value="-v" />
            <arg value="-S" />
            <arg path="${resource.flavour.dir}" />
            <arg value="-C" />
            <arg path="${out.res.absolute.dir}" />
        </exec>
    </do-only-if-not-library>
        <exec executable="${aapt}" taskName="crunch">
            <arg value="crunch" />
            <arg value="-v" />
            <arg value="-S" />
            <arg path="${resource.absolute.dir}" />
            <arg value="-C" />
            <arg path="${out.res.absolute.dir}" />
        </exec>
</target>

This might give you a clue to proper solution in your case.

5
johsin18 On

I think you do have to have crunch the resource of the library project.

Try

aapt crunch -v -S res -C bin/res

in your library's project folder, and rebuild. That helped for me. Now the question is how to automate that.