AAPT2 compile failed: invalid dimen on Android 3.0 Canary 1

30,347

Solution 1

The workaround is to switch your development machine to a locale which uses "." as a decimal mark.

It can be changed the following way:

enter image description here

Solution 2

Below are mentioned four different solutions: A, B, C, and D; pick one that suites you:

A) Fixing Android Studio via Ubuntu .desktop launcher file

This is an Ubuntu-only alternative to the general approach on Fixing Android Studio (see below). Note that you may still want to implement the part about Fixing the shell, and perhaps even revert any modifications to studio.sh to fully confirm this fix.

I got tired of patching my studio.sh for every canary update, so I came up with a better solution that eliminates this step. It works on Ubuntu and simply involves creating a .desktop launcher that sets the sick environment variable in question.

  1. Make a note of where your Android Studio 3 is installed, e.g. ~/opt/android-studio-3.

  2. Prepare your local icon and applications directory, in case the don't already exist:

    mkdir -vp ~/.local/share/icons ~/.local/share/applications
    
  3. Create an Android Studio 3 icon that will make your launcher stand out from the default icon and save it into ~/.local/share/icons/android-studio-3.png. Or you can use the one I made by rubbing a piece of cheese on the original (~/opt/android-studio-3/bin/studio.png):

    android-studio-3.png

  4. Create an Android Studio 3 launcher file by copy and pasting this into a shell:

    cat <<-EOF > ~/.local/share/applications/android-studio-3.desktop
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Android Studio 3
    Icon=android-studio-3
    Exec=env LC_NUMERIC="en_US.UTF-8" opt/android-studio-3/bin/studio.sh "%f"
    Categories=Development;IDE;
    Terminal=false
    StartupWMClass=jetbrains-studio
    EOF
    
  5. Make it executable:

    chmod +x ~/.local/share/applications/android-studio-3.desktop
    
  6. Now for the tricky part. Ideally you should be able find, start, and create shorts for Android Studio 3 from the Dash:

For your pleasure

But personally, I almost always have trouble getting Ubuntu to detect my new or changed .desktop files. One solution is to log out and back in again. If anyone knows how to force a rescan please let me know!

B) Fixing Android Studio start script

Here's an easy, elegant, and semi-permanent fix: Only change the locale of Android Studio itself by modifying its startup script:

  1. Edit studio.sh e.g. ~/opt/android-studio/bin/studio.sh or whatever your installation path may be.

  2. Somewhere at the top of the file, below #!/bin/sh and before the first lines of code appear, add this:

    LC_NUMERIC="en_US.UTF-8".

    Here's the top part of my studio.sh for completeness:

    #!/bin/sh
    #
    # ---------------------------------------------------------------------
    # Android Studio startup script.
    # ---------------------------------------------------------------------
    #
    
    LC_NUMERIC="en_US.UTF-8"
    
    message()
    {
      TITLE="Cannot start Android Studio"
    ...
    
  3. Restart Android Studio

A note on Upgrading Android Studio or Gradle

When you later upgrade your Android Studio installation, it will detect that you've modified studio.sh. You should let the installer replace the file, and afterwards perform the patch again as described above. Finally restart Android Studio, and you'll be ready again. The other solutions are not affected by this.

C) Fixing the shell; Gradle, Jenkins, all that

Building from the shell using gradlew also requires the fix to be applied. This only affects the shell and not Android Studio. Pick one:

  1. Either specify the the fix on every invocation like this:

    LC_NUMERIC="en_US.UTF-8" ./gradlew clean assDebug

  2. Or to make this permanent for the project, edit the gradlew file in the root of the project and somewhere at the top add this:

    LC_NUMERIC="en_US.UTF-8"

    Like here:

    #!/usr/bin/env bash
    
    ################################################################################    
    ##
    ##  Gradle start up script for UN*X
    ##
    ################################################################################
    
    LC_NUMERIC="en_US.UTF-8"
    
    # Add default JVM options here. You can al...
    DEFAULT_JVM_OPTS=""
    
    APP_NAME="Gradle"
    ...
    
  3. Or you can of course also add a global and permanent fix though the use of an alias, gr:

    cat <<EOF>>~/.bash_aliases
    
    # Fixing Android Studio 3 Canary bug https://stackoverflow.com/a/44304075/2412477
    alias gr='LC_NUMERIC="en_US.UTF-8" ./gradlew'
    EOF
    

    Note this is how bash shell aliases are added on Ubuntu; if you're on a different OS perhaps you should append to ~/.bashrc or ~/.profile instead.

    Then start a new shell and now instead of invoking ./gradlew use the new alias gr:

    gr clean assDebug

The clear disadvantage of #2 is that this has to be applied to all projects manually. The advantage, I think, is that this will automatically be overwritten when a new gradlew is installed, much like studio.sh gets replaced, so you get to test if the bug has been fixed =)

D) Disabling APPT2 all together

Personally I wouldn't do this, but I've added it for completeness since it definitely is a way to make appt2 stop giving errors. Add this line to your gradle.properties: android.enableAapt2=false

Solution 3

I solved this problem by adding the following line to the gradle.properties files

android.enableAapt2=false

Solution 4

Make sure you are not adding any units(dp) when using format="float"

I was facing the same problem because I auto-generated dimens using Android Studio using Extract dimen resource and it added unit type like:

<item name="margin_top" type="dimen" format="float">51.75dp</item>

It should be:

<item name="margin_top" type="dimen" format="float">51.75</item>

Solution 5

This issue has been fixed in the newest Android Studio stable. Upgrading your Android Studio to 3.0 should solve this issue for you (also no need to disable AAPT2).

Share:
30,347
sposnjak
Author by

sposnjak

Updated on July 08, 2022

Comments

  • sposnjak
    sposnjak almost 2 years

    I am playing around with Instant Apps for Android.I installed all the correct packages and tired to create new Application with Instant App support (checked the box for Instant App when crating new application). The problem is that I always run into a problem with compile tools. Does anybody else have this problem and was able to find any workaround.

    My environment:

    • Android Studio 3.0 Canary 1
    • Compile SDK: 25
    • Build Tools: "26.0.0 rc2"
    • Gradle plugin:3.0.0-alpha1
    • Gradle: tried both gradle-4.0-milestone1 and 2
    • Java 1.8/1.7
    • OS: tried both Windows 10 and Linux Ubuntu 16.4 LTS

    The error:

    Error: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
    aapt2 compile -o /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/res/merged/androidTest/debug /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml
    Issues:
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:520 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:521 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:568 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:594 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:595 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:597 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:598 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:599 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:600 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:601 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:602 invalid dimen
     - ERROR: /home/simonp/Work/201705_InstantApp/InstantAppTest/feature/build/intermediates/incremental/mergeDebugAndroidTestResources/merged.dir/values/values.xml:603 invalid dimen
    :feature:mergeDebugAndroidTestResources FAILED
    

    The line in question contains (520):

    <item format="float" name="abc_disabled_alpha_material_dark" type="dimen">0.30</item>
    

    Hopefully I provided enough information to you to resolve the problem. Thank you.

  • Salim
    Salim about 7 years
    This worked for me. Apply system-wide was not necessary. But restarting Android-Studio is not enough. You need to log-out and log-in again (or restart your system) to make the changes active for gradle build.
  • sposnjak
    sposnjak about 7 years
    It works! For linux you should do: export LC_NUMERIC="en_US.UTF-8" and then start Android Studio in the same prompt
  • rzehan
    rzehan about 7 years
    I added export LC_NUMERIC="en_US.UTF-8" to the beginning of my studio.sh and it works fine. I certainly wouldn't change my system wide locale just to work around some bug.
  • Stephan Henningsen
    Stephan Henningsen about 7 years
    ...wat? Recommending someone to change their system wide locale just because of one bug in one app is crazy. It will impact how your date is formatted e.g. in email clients, even language and pretty much everything else too.
  • saturov
    saturov about 7 years
    @StephanHenningsen what did you expect from alpha-release? this is just workaround if you are strongly need to use this version of IDE
  • Stephan Henningsen
    Stephan Henningsen about 7 years
    You misunderstood; I'm not commenting on the bug itself; I'm commenting on the work-around.
  • lelloman
    lelloman about 7 years
    this seems a much better workaround than the accepted answer
  • vladaman
    vladaman about 7 years
    For some reason this does not work for me. I got Android Studio 3.0 Canary 3 and after setting all this the merge problem persists. My locale is en_US.UTF-8
  • user2520215
    user2520215 about 7 years
    Well I'm on windows. Any suggestions?
  • lelloman
    lelloman about 7 years
    I just updated to canary 3 and came back here to copy paste again, it's still working for me
  • aberaud
    aberaud about 7 years
    @vladaman This fixes the obvious floating point bug, however Android Studio 3.0 canary 3 is still stricter than before, and some bugs in your resource files that where previously accepted will now cause a merge error. Note that as of today, the design library preview is bugged and won't build on Android Studio 3.0 canary 3.
  • Romuald DANSOU
    Romuald DANSOU about 7 years
    @EugenPechanec Yes
  • Eugen Pechanec
    Eugen Pechanec about 7 years
    That doesn't make any sense. If you were missing the repository when you needed it, you wouldn't get as far as merging resources. Build would fail with missing dependencies. If you didn't need the repository, having it specified wouldn't change a thing. OP wouldn't get this far in build process if they haven't already included google maven repo reference in their project. Your answer does not provide solution to the problem at hand. You must have made other changes too.
  • Maxr1998
    Maxr1998 about 7 years
    Still happening on canary 4, and because I use the design library it also happens with the fix.
  • Stephan Henningsen
    Stephan Henningsen about 7 years
    I don't see how installing these packages should fix the problem at hand in the OP. How is this problem related to a deflate compression method? I don't even have lib32z1 installed myself, and my installation works after applying this stackoverflow.com/a/44304075/2412477; I recommend anyone to try that before installing potentially unnecessary packages.
  • Stephan Henningsen
    Stephan Henningsen about 7 years
    @Maxr1998 That's weird... I just upgraded to canary 4, applied the fix, and my Android Studio can successfully rebuild my app that has dependencies { ... compile 'com.android.support:design:25.4.0' ... } because it uses an android.support.design.widget.TextInputLayout. I can also rebuild from shell, but remember that the fix must be applied here too, i.e.: LC_NUMERIC="en_US.UTF-8" ./gradlew clean assDebug.
  • Stephan Henningsen
    Stephan Henningsen about 7 years
    I added a new section on fixing the shell as well.
  • Maxr1998
    Maxr1998 about 7 years
    @Stephan Henningsen not sure what I could have done wrong, though I'm actually using the SDK, tools and libs in version 26.X. I already fixed the error myself by just disabling aapt2 by setting android.enableAapt2 (or similar) to false in my gradle.properties. Thanks a lot for the helpful answer anyway :)
  • Etienne Lawlor
    Etienne Lawlor about 7 years
    What about on mac OSX
  • Stephan Henningsen
    Stephan Henningsen almost 7 years
    @toobsco42 Have you tried this stackoverflow.com/a/44430789/2412477 ? Note that I'm not on a Mac, so I don't know if this works for you.
  • Etienne Lawlor
    Etienne Lawlor almost 7 years
    There instructions are for Ubuntu. I don't know what steps you need to take on Mac osx
  • Stephan Henningsen
    Stephan Henningsen almost 7 years
    Have you tried searching for studio.sh and modifying it?
  • Stephan Henningsen
    Stephan Henningsen almost 7 years
    To all you Ubuntu users: I've added a new approach which I think could be the best. It Works On My Machine, but let me know what you think and if there're any errors.
  • ksugiarto
    ksugiarto almost 7 years
    I think this answer deserves its explanation.
  • lukassos
    lukassos almost 7 years
    this answer helped but does not solve the issue just turns off the gradle module that caused it, if you need the aapt2 then definitely add export LC_NUMERIC="en_US.UTF-8" to your .bashrc which worked like charm for me
  • devconsole
    devconsole almost 7 years
    Simpler solution for the .desktop launcher: let Studio do most of the work for you. Run Tools -> Create Desktop Entry, exit studio, open ~/.local/share/applications/jetbrains-studio.desktop, add env LC_NUMERIC="en_US.UTF-8" to the beginning of the Exec line.
  • Stephan Henningsen
    Stephan Henningsen almost 7 years
    @devconsole Yep, that's the file I started with, but I like having things explicit so I renamed it and modified the name and icon. The reason for this is that I have both Android Studio 2 and 3 installed side-by-side, and if you make the .dekstop file from the IDEs, you cannot tell Android Studio 2 and 3 apart ... and one will probably overwrite the other's .desktop file =\
  • comodoro
    comodoro almost 7 years
    It still occurs in my AS 3 beta2, and adding export LC_NUMERIC="en_US.UTF-8" to the beginning of the shell script sometimes does not work, probably a cache thing.
  • xdevs23
    xdevs23 almost 7 years
    the LC_NUMERIC="en_US.UTF-8" method works on jenkins too
  • HendraWD
    HendraWD over 6 years
    what about MAC OS users?
  • HendraWD
    HendraWD over 6 years
    How to fix this on MacOS without disabling AAPT2?
  • Stephan Henningsen
    Stephan Henningsen over 6 years
    @HendraWD I don't have a Macintosh, but I'd assume solution C should work on all unix-like systems. But it doesn't? What about setting LC_ALL instead?
  • Mehbube Arman
    Mehbube Arman over 6 years
    I am developing in a windows environment, what change do I need for this?
  • XurajB
    XurajB almost 6 years
    android.enableAapt2=false is being deprecated and will be removed by the end of 2018.
  • Samuel Owino
    Samuel Owino over 5 years
    This might sound dumb but, how do I get to those settings on Android Studio 3.2.1?