88

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.

Volo
  • 28,673
  • 12
  • 97
  • 125
sposnjak
  • 1,491
  • 2
  • 15
  • 25
  • 4
    As a workaround for getting the build working, I currently disabled the aapt2 and it works for testing out the build ... you can do it by setting android.enableAapt2=false in your gradle.properties file. I think it might be a studio bug not sure ... – BruceWayne May 27 '17 at 17:33
  • Other solution is https://stackoverflow.com/a/33943562/5125608 It's works for me. – anlijudavid May 29 '17 at 18:01
  • 5
    FYI latest canary build, 5, is released and Google did mentioned below that they still got issue with this AAPT2 as follow, **AAPT2. We are continuing to stabilize AAPT2 which enables incremental resource processing. If your build fails due to resource processing issue, please send us a bug report. To temporarily disable AAPT, set android.enableAapt2=false in your gradle.properties file. Roboelectric is currently not compatible with AAPT2** – Infinite Loops Jul 04 '17 at 16:35

7 Answers7

106

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

Stephan Henningsen
  • 3,665
  • 1
  • 22
  • 29
  • 10
    this seems a much better workaround than the accepted answer – lelloman Jun 02 '17 at 11:46
  • 2
    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 – vladaman Jun 05 '17 at 08:48
  • 1
    I just updated to canary 3 and came back here to copy paste again, it's still working for me – lelloman Jun 06 '17 at 11:48
  • 1
    @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. – aberaud Jun 06 '17 at 12:35
  • Still happening on canary 4, and because I use the design library it also happens with the fix. – Maxr1998 Jun 16 '17 at 20:38
  • @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 Jun 18 '17 at 21:44
  • I added a new section on fixing the shell as well. – Stephan Henningsen Jun 18 '17 at 22:36
  • 1
    @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 :) – Maxr1998 Jun 19 '17 at 23:29
  • 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. – Stephan Henningsen Aug 03 '17 at 19:55
  • 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. – devconsole Aug 21 '17 at 16:52
  • @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 =\ – Stephan Henningsen Aug 21 '17 at 20:24
  • How to fix this on MacOS without disabling AAPT2? – HendraWD Oct 27 '17 at 11:21
  • @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? – Stephan Henningsen Oct 27 '17 at 12:59
57

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

saturov
  • 784
  • 7
  • 12
  • 6
    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. – Salim May 27 '17 at 13:00
  • 14
    It works! For linux you should do: export LC_NUMERIC="en_US.UTF-8" and then start Android Studio in the same prompt – sposnjak May 30 '17 at 07:16
  • 17
    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. – rzehan May 31 '17 at 14:36
  • 7
    ...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. – Stephan Henningsen Jun 01 '17 at 10:00
  • @StephanHenningsen what did you expect from alpha-release? this is just workaround if you are strongly need to use this version of IDE – saturov Jun 01 '17 at 16:26
  • 1
    You misunderstood; I'm not commenting on the bug itself; I'm commenting on the work-around. – Stephan Henningsen Jun 01 '17 at 18:04
  • 2
    Well I'm on windows. Any suggestions? – user2520215 Jun 05 '17 at 16:50
  • 1
    What about on mac OSX – Etienne Lawlor Jun 22 '17 at 05:58
  • @toobsco42 Have you tried this https://stackoverflow.com/a/44430789/2412477 ? Note that I'm not on a Mac, so I don't know if this works for you. – Stephan Henningsen Jul 04 '17 at 07:55
  • There instructions are for Ubuntu. I don't know what steps you need to take on Mac osx – Etienne Lawlor Jul 04 '17 at 08:41
  • Have you tried searching for `studio.sh` and modifying it? – Stephan Henningsen Jul 07 '17 at 20:52
  • 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. – comodoro Aug 28 '17 at 12:46
  • the `LC_NUMERIC="en_US.UTF-8"` method works on jenkins too – xdevs23 Sep 05 '17 at 07:30
  • This might sound dumb but, how do I get to those settings on Android Studio 3.2.1? – Samuel Owino Dec 28 '18 at 19:24
33

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

android.enableAapt2=false
Hoshouns
  • 2,420
  • 23
  • 24
1

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).

Izabela Orlowska
  • 7,431
  • 2
  • 20
  • 33
1

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>

Atiq
  • 14,435
  • 6
  • 54
  • 69
-6

add

maven{
 url 'https://maven.google.com'
}

to repositories work for me

  • @EugenPechanec Yes – Romuald DANSOU Jun 12 '17 at 14:38
  • 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. – Eugen Pechanec Jun 12 '17 at 15:53
-6

Required libraries for 64-bit machines:

If you are running a 64-bit version of Ubuntu, you need to install some 32-bit libraries with the following command:

sudo apt-get install libc6:i386 libncurses5:i386 libstdc++6:i386 lib32z1 libbz2-1.0:i386

If you are running 64-bit Fedora, the command is:

sudo yum install zlib.i686 ncurses-libs.i686 bzip2-libs.i686
legoscia
  • 39,593
  • 22
  • 116
  • 167
  • 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 https://stackoverflow.com/a/44304075/2412477; I recommend anyone to try that before installing potentially unnecessary packages. – Stephan Henningsen Jun 17 '17 at 09:25