Search code examples
androidgradleandroid-gradle-pluginaapt2

AAPT2 error - appcompat-v7 files not found in mergeDebugResources


I have a Hudson build job that's failing with the error

 Error: java.util.concurrent.ExecutionException: com.android.builder.internal.aapt.v2.Aapt2Exception: AAPT2 error: check logs for details

and in the logs, lots of lines like this:

C:\Windows\System32\config\systemprofile\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.1.0.aar\3bbc697d357b69b5ea5f78a35f99a514\res\color\abc_hint_foreground_material_dark.xml: error: file not found.

I've found a few questions on AAPT2 errors like this, for example this and this but they don't seem to have a fix.

So why is the build failing?


Solution

  • As per the first linked question, disabling AAPT2 seems like a bad idea, as AAPT might be removed at some point in the future.

    The second linked question at least gives a clue, with the accepted answer there being that that version of gradle doesn't handle non-ASCII characters in the .gradle cache path. But in our case, there aren't any non-ASCII characters in the cache path being logged.

    In fact, from the logs, gradle is looking for the cache inside C:\Windows\System32\config\systemprofile\.gradle, which is only accessible with admin privileges. Not somewhere we want to be caching libraries.

    This answer, along with this post on the gradle forums are what led me to the solution. Short version is, by default gradle caches downloaded libraries in the user area, and there seems to be a bug in Java where it will incorrectly determine the users home directory. This results in the gradle cache being put somewhere it really shouldn't be.

    To fix the problem, then, we just need to set gradle's user home directory. This blog post has a bunch of ways to do that. One way is to create a GRADLE_USER_HOME environment variable, which has the advantage of applying to all gradle builds on the machine.