AAPT2 compile failed: invalid dimen on Android 3.0 Canary 1 - android

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.

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.
Make a note of where your Android Studio 3 is installed, e.g. ~/opt/android-studio-3.
Prepare your local icon and applications directory, in case the don't already exist:
mkdir -vp ~/.local/share/icons ~/.local/share/applications
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):
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
Make it executable:
chmod +x ~/.local/share/applications/android-studio-3.desktop
Now for the tricky part. Ideally you should be able find, start, and create shorts for Android Studio 3 from the Dash:
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:
Edit studio.sh e.g. ~/opt/android-studio/bin/studio.sh or whatever your installation path may be.
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"
...
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:
Either specify the the fix on every invocation like this:
LC_NUMERIC="en_US.UTF-8" ./gradlew clean assDebug
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"
...
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

The workaround is to switch your development machine to a locale which uses "." as a decimal mark.
It can be changed the following way:

I solved this problem by adding the following line to the gradle.properties files
android.enableAapt2=false

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

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>

add
maven{
url 'https://maven.google.com'
}
to repositories work for me

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

Related

Android Gradle Could not reserve enough space for object heap

I've installed Android Studio 1.1.0. I haven't done anything yet like start new Android application or import anything. Somehow it is trying to build something and it throws sync error.
Error:Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at http://gradle.org/docs/2.2.1/userguide/gradle_daemon.html
Please read the following process output to find out more:
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.
I've already checked at gradle.org/.../gradle_daemon.html but couldn't find anything that helps me to solve the problem.
It isn't a memory problem because I've 8GB of physical memory and no other program running.
For Android Studio 1.3 : (Method 1)
Step 1 : Open gradle.properties file in your Android Studio project.
Step 2 : Add this line at the end of the file
org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m
Above methods seems to work but if in case it won't then do this (Method 2)
Step 1 : Start Android studio and close any open project (File > Close Project).
Step 2 : On Welcome window, Go to Configure > Settings.
Step 3 : Go to Build, Execution, Deployment > Compiler
Step 4 : Change Build process heap size (Mbytes) to 1024 and Additional build process to VM Options to -Xmx512m.
Step 5 : Close or Restart Android Studio.
in gradle.properties, you can even delete
org.gradle.jvmargs=-Xmx1536m
such lines or comment them out. Let android studio decide for it. When I ran into this same problem, none of above solutions worked for me. Commenting out this line in gradle.properties helped in solving that error.
Add the following line in MyApplicationDir\gradle.properties
org.gradle.jvmargs=-Xmx1024m
I tried several solutions, nothing seemed to work. Setting my system JDK to match Android Studio's solved the problem.
Ensure your system java
java -version
Is the same as Androids
File > Project Structure > JDK Location
My fix using Android Studio 3.0.0 on Windows 10 is to remove entirely any jvm args from the gradle.properties file.
I am using the Android gradle wrapper 3.0.1 with gradle version 4.1. No gradlew commands were working, but a warning says that it's trying to ignore any jvm memory args as they were removed in 8 (which I assume is Java 8).
I ran into the same issue, here's my post:
Android Studio - Gradle build failing - Java Heap Space
exec summary: Windows looks for the gradle.properties file here:
C:\Users\.gradle\gradle.properties
So create that file, and add a line like this:
org.gradle.jvmargs=-XX\:MaxHeapSize\=256m -Xmx256m
as per #Faiz Siddiqui post
Faced this issue on Android studio 4.1, windows 10.
The solution that worked for me:
1 - Go to gradle.properties file which is in the root directory of the project.
2 - Comment this line or similar one (org.gradle.jvmargs=-Xmx1536m) to let android studio
decide on the best compatible option.
3 - Now close any open project from File -> close project.
4 - On the Welcome window, Go to Configure > Settings.
5 - Go to Build, Execution, Deployment > Compiler
6 - Change Build process heap size (Mbytes) to 1024 and VM Options to -Xmx512m.
Now close the android studio and restart it. The issue will be gone.
Solution for Android Studio 2.3.3 on MacOS 10.12.6
Start Android Studios with more heap memory:
export JAVA_OPTS="-Xms6144m -Xmx6144m -XX:NewSize=256m -XX:MaxNewSize=356m -XX:PermSize=256m -XX:MaxPermSize=356m"
open -a /Applications/Android\ Studio.app

Error executing aapt: Return code 132 problems

Since updating my Android SDK about a month ago I've had endless problems leaving me with a non-functional development environment.
Initially the problem was with the v7-appcompat support library which threw up about 100 errors relating to various resources. It was fairly obvious they were resources relating to Android v5 and a quick search on Stack Overflow meant I realised the v7-appcompat library was targeting API 19. Changing the relevant settings to target API 21 fixed the resource errors.
However, the problem I've had since then is any attempt to build the library project quickly finishes with...
Error executing aapt: Return code 132
...and the library project isn't built correctly.
My original setup was with Eclipse Kepler but then I tried Android Studio v1.0.1 (same aapt error code) and today I installed Eclipse Luna to a separate directory from Kepler AND a fresh installation of ADT and SDK. Each version of Eclipse also have their own workspaces.
So basically I have two versions of Eclipse (each with their own ADT / SDK setups and workspaces) and Android Studio with its own directory structure and all three exhibit the same problem.
The question is, how do I go about diagnosing this issue? The common denominator is obviously an aapt issue but I have no idea how to fix it.
I'm running on a Debian Wheezy (32-bit) setup and I've got Java 1.7 installed correctly (as recommended).
There seems to be an issue with aapt on some 32 bit systems. It throws an error 132 on app:processDebugResources which is when aapt is 'crunching' png files ready for inclusion in the apk package. You can see more details at https://code.google.com/p/android/issues/detail?id=75110
Some people have reported that 'fixing' the offending png files (e.g. by opening in Gimp and saving) sorts the problem. When they are 'built in' to appcompat it's much harder to do of course.
Before getting into detail can you just say if you are indeed running on a 32 bit processor, and if so which one? use lscpu to find out.
OK, if anyone is interested, here is a work around so you can use your old laptop to build apps with Android Studio. Basically you strip all the png files in the appcompat-v7 library and then do a build using two versions of aapt. Yuk.
Find the appcompat-v7.aar which will be somewhere like ~/Android/Sdk/extras/android/m2repository/com/android/support/appcompat-v7/22.0.0
Extract it (it's a zip file really) to a temporary directory
Strip all the png files: find . -name "*.png" -exec mogrify -strip {} \;
Pack the files back up again: pushd appcompat-v7-22.0.0; zip -r ../appcompat-v7-22.0.0.aar; popd
Move the new aar file to replace the original (that may be enough to get a good build but it wasn't for me)
Now find an old version of aapt (I went to https://dl-ssl.google.com/android/repository/build-tools_r17-linux.zip)
Copy the version of aapt from the zip you've just picked up into the Android/Sdk/build-tools directory as aapt-17 or whatever
Rename the original aapt to aapt-0
Alternate between the two versions of aapt (e.g. ln -sf aapt-17 aapt) and you should find you can get a good build. One version will fail in one place, the other in another, getting you past all the bumps.
I know it stinks but I was desperate!!
Here's what I did, which is enough to get a successful build. Inspired by peterthevicar's answer.
downgrade sdk to 19.1.0, you can install it from studio and then change buildToolsVersion in build.gradle
rename aapt to aapt.bin and put a shell script in its place that intercepts just the png crunch (args: s -i infile -o outfile) and does a simple cp instead. would paste it here but SO's syntax highlighting suuuucks

Titanium Mobile: Cannot build android modules in 3.3.0

Update: To clarify, I can build the process successfully. The question is about installing the example project directly on a device (though I'm thinking emulator won't work either).
I developed a module about two years ago that allowed bluetooth connectivity on Android. I believe the latest version of the Titanium SDK at the time was around 2.0. I believe the specifics of the module and the example app are irrelevant to this question. I will ask my question first, and then try and explain the steps that I have gone through to lead me to the actual question.
My question is: is something in my environment misconfigured, or is this a bug in the Titanium SDK?
If it is the latter, I am very surprised that I can't find any information about it, since it prevents android modules from installing with ant in the latest SDK.
Information about my build environment:
Mac OS 10.9.5
Titanium Studio 3.3.0.GA
Ti CLI 3.3.0
Android 19
java -version: java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode)
I am trying to build it now for the latest SDK, 3.3.0 at the time of this writing. I found that when I tried to build (install) with ant, it resulted in the following output:
[exec] [ERROR] /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/res/values-v14/themes_base.xml:133: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.
[exec] [ERROR] /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/res/values-v14/themes_base.xml:148: error: Error retrieving parent for item: No resource found that matches the given name 'android:Theme.Holo.Light.DialogWhenLarge'.
[exec] [ERROR] /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/res/values-v14/themes_base.xml:159: error: Error: No resource found that matches the given name: attr 'android:actionBarWidgetTheme'.
[exec] [ERROR] Error generating R.java from manifest
[exec] [ERROR] Build Failed.
BUILD SUCCESSFUL
Total time: 20 seconds
Not only that, but when creating a brand new mobile module project, and building straightaway with ant:
cd proj/android && ant install
The errors are the same. I should also point out that I reformatted not too long ago, so I find it unlikely that I have borked my titanium installation already. Now, from here, I've gotten into the Titanium build scripts to try and figure out what is happening. First, from the Titanium SDK 3.3.0 release notes:
Due to the addition of the appcompat library, there are a number of behavior changes to the application:
...
The target SDK must be set to API level 14 (Android 4.0.x) or higher, or you must have API level 14 or higher installed.
Now in the build output for my project:
[exec] [TRACE] Writing out AndroidManifest.xml
[exec] [DEBUG] /path/to/my/androidsdk/build-tools/17.0.0/aapt package -m -J /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/gen -M /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/AndroidManifest.xml -S /var/folders/x7/h7lgzf1n4nx7fcwr26fdrzxr0000gn/T/mTUYC62ti/testmodule/build/android/res -I /path/to/my/androidsdk/platforms/android-10/android.jar
Note that it's using the tools for android-10 and not android-14+ which is strange considering the relevant bits of my timodule.xml and build.properties:
timodule.xml:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
<tool-api-level>19</tool-api-level><!-- deprecated - see below -->
build.properties:
titanium.platform=/Users/trey/Library/Application Support/Titanium/mobilesdk/osx/3.3.0.GA/android
android.platform=/Users/trey/sdk/android-sdk-macosx/platforms/android-19
google.apis=/Users/trey/sdk/android-sdk-macosx/add-ons/addon-google_apis-google-19
android.ndk=/Users/trey/sdk/android-ndk-r9d
As it turns out, it seems to be only the deprecated element that matters, and not in timodule.xml at all. Build scripts are found at $titanium_sdk/$os/$version/android/
Observation: Setting MIN_API_LEVEL = 19 (or 14) instead of 10 in builder.py fixes the issue. So the build script is ignoring my configuration and using the default. This seems to happen in the init method of the builder class:
temp_tiapp = TiAppXML(self.project_tiappxml)
if temp_tiapp and temp_tiapp.android:
if 'tool-api-level' in temp_tiapp.android:
self.tool_api_level = int(temp_tiapp.android['tool-api-level'])
tool_api_level_explicit = True
This sets the tool_api_level that the build script uses, but in my case it never gets called because there is not tiapp.xml. So I made a tiapp.xml, and put it in my example project. It has the same configs as the timodule.xml. As part of the build process this gets copied along with the rest of the example project into a temp folder that will be used to build and run the project. self.project_tiappxml in the code above is the root of the project. And there is still no tiapp.xml there, because it was copied to the Resources directory with everything else, so tool_api_level always gets set to the default of 10. That is basically as far as I've gotten.
So, again my question: is my configuration wrong, or is this a bug. It seems to me that if it is a bug, then no one would be able to build android modules, best I can tell. So if it's my fault, what have I done to cause it, and how can I fix it.
Update: If you saw my last edit, that was wrong and I'm sorry - my selected SDK for ti was 3.2.3! Fixed that and it creates the new project fine. It also builds. However, installing the example project still results in the same error as above:
[exec] [ERROR] Error generating R.java from manifest
[exec] [ERROR] Build Failed.
I also added a line above the call to aapt, to clarify that these errors occur while trying to write AndroidManifest.xml for the example project.
Its good you are working with 3.3.0, because from this version Android module creation is relatively easy.by viewing your build.properties i think something is missing or you forgot to add completely here it should be something close to :
titanium.platform=/Users/xxxxxx/Library/Application Support/Titanium/mobilesdk/osx/3.3.0.GA/android
android.platform=/Users/xxxxxx/Documents/adt-bundle-mac/sdk/platforms/android-16
google.apis=/Users/xxxxxx/Documents/adt-bundle-mac/sdk/add-ons/addon-google_apis-google-16
android.ndk=/Users/xxxxxx/Documents/android-ndk
but what i would suggest is check the following things as they get missed often:
you should have android-ndk installed and in the path
you must have gperf installed with your system(for mac comes with xcode command line tools)
Followed by running the following command in your terminal:
ti create -p android -t module -d <WORKSPACE_DIR> -n <MODULE_NAME> -u <MODULE_URL> --id <MODULE_ID>
## Sample Command
ti create -p android -t module -d ~/Documents/Sample_Workspace/ -n calc -u http:// --id org.appcelerator.calc
If everything is alright you will have a module in your Sample_Workspace folder with name calcand the given id.
you can work on it directly by importing it to your eclipse. and once done can build it using ant over the directory.
Hope it helps. Check the module creation guide.

Environment variable in settings.gradle not working with Android Studio

I do have a multi-module project with a library project in a different root path. As illustration you can imagine something like this:
/projects_home/projects/app_root
|--app/
| |--build.gradle
|--build.gradle
|--settings.gradle
/libraries_home/libraries
|--libA
|--build.gradle
In my settings.gradle file I am able to set the absolute path to the library project utilizing the projectDir attribute. This works just fine within the console as well as with Android Studio.
But if I try to use an environment variable it stops working with Android Studio. The settings.gradle for the example above would look like this:
include ':app'
include ':libA'
project(':libA').projectDir = new File("$System.env.LIB_ROOT", '/libraries/libA')
If I build with the graddle wrapper from the console, it still works. But AS stops working with the following error msg:
Gradle 'app' project refresh failed:
Configuration with name 'default' not found.
If I unset the environment variable, the build on console fails with the same msg:
* What went wrong:
A problem occurred configuring project ':app'.
> Configuration with name 'default' not found.
Therefore I guess that AS is somehow not be able to access the environment variables set with my ~/.bashrc
Does somebody of you maybe know a way how I can make AS aware of my environment?
Android Studio does read the environment variables. You can prove it by launching Android Studio from the shell in which those env. variables being specified instead of from X-window dash board.
The reason you did not have those variables is the X-window environment you were using did not read $HOME/.bashrc which contained those variables. This makes sense because bashrc is for Bash not X.
Assuming you are using GNOME or Unity, to launch Android Studio with those environment variables being specified, just modify the .desktop file of Android Studio (e.g. ~/.local/share/applications/android-studio.desktop):
Find this line:
Exec="/home/username/tools/android/android-studio/bin/studio.sh" %f
Change it to:
Exec=env LIB_ROOT=/libraries_home "/home/username/tools/android/android-studio/bin/studio.sh" %f
Note:
This modification just prepend env LIB_ROOT=/libraries_home to the original command. You must replace username with your own user name.
Update
If you have any questions, please leave a comment instead of editing the answer directly.
On Macs, Android Studio does not read environment variables for use in Gradle apparently. I believe this is the cause for confusion in the answers here - maybe it does on Windows.
In order to get Android Studio to read environment variables, I run the application from the command line:
> /Applications/Android\ Studio.app/Contents/MacOS/studio
The other answers here offer solutions other than using environment variables. For my situation, I'm using a library I didn't write that requires the use of an environment variable, and I'd rather not edit their code so it's easier to update later.
EDIT: And, I have a dock icon to launch Android Studio this way:
OSX: Add Dock icon for dedicated Terminal command explains how.
Android Studio doesn't read environment variables, so this approach won't work. Also, using the projectDir scheme in settings.gradle will probably cause problems. Android Studio has a limitation that all of its modules need to be located underneath the project root. If you have libraries that are used in multiple projects and they can't be placed under a single project root, the best advice is to have them publish JARs or AARs to a local Maven repository that individual projects can pick up.
Despite the answer from Scott Barta is correct, I realized there is a way to solve my problem and wan't to share this in case somebody else has the same requirement.
I am now using the gradle.properties file do define and use gradle properties instead of system properties. The documentation of this feature can be fined in the user guide
The solution to my original question now looks like this:
$USER_HOME/.gradle/gradle.properties:
LIB_ROOT=/libraries_home
The settings.gradle file has to be modified to use the gradle property instead of the system property:
include ':app'
include ':libA'
project(':libA').projectDir = new File(LIB_ROOT, '/libraries/libA')
This works fine for me, headless as well as with AS.
Some more words regarding the fact that I am working with modules which are not placed underneath one project root. Till now it looks like AS is not complaining about this. But I just started working with this structure and it may be that I will run into problems later. What I like about this is the more flat representation in AS which is more like I am used to have it with Eclipse.
What is also described in the user guide, is to set system properties with the gradle.properties file. I tried this also, but I did run into the same problems with AS using environment variables.
It works for me with the following steps:
Set your variable in Windows
Reboot
reach it in gradle build: System.env.MYVARIABLE
I faced the same issue in apple laptop after the Android Studio Bumblebee update. This seems to be happening due to some permission issue with the Android Studio.
The workaround is to add missing flag:
chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv
You can check this issue tracker for more details.
You can set environment variable by appending:
-DYOUR_VARIABLE=variable_value
to ~/Library/Preferences/AndroidStudioX.X/studio.vmoptions that you can open by selecting Help -> Edit Custom VM Options... from Android Studio menu.
And then you can use it like:
System.env.YOUR_VARIABLE
in build.gradle or settings.gradle.
MAC OS Update
I confirm that I have Environmental Variables working on Mac OS Catalina
You just need to set it in the shell you are using. I was using zsh, and was trying to set ~/.bash_profile, so it wasn't working.
Example:
ZSH Profile

getting error during project creation using phonegap-2.5.0

i have installed android sdk and downloaded the latest version of phonegap 2.5.0 in fedora 18.
i am trying to create a project using the following command
./create ~/Desktop/android-project com.example.android android_project
am getting the following error.
which: no android in (/sbin:/bin:/usr/sbin:/usr/bin)
An unexpected error occurred: ANDROID_BIN="${ANDROID_BIN:=$( which android )}" exited with 1
need your valuable suggestions.
finally found the answer
After checking my .bash_profile paths, and a bunch of digging, I finally found the cause of the issue. If you open up the create file (phonegap-2.5.0/lib/android/bin/create), there is a line that lists ANDROID_BIN=”${ANDROID_BIN:=$( which android )}”. The “which android” is the cause of the issue (or at least it seemed to be in my case). If you replace this line with the full path to your android tools (as listed below) it should resolve the issue. My final line looked like this:
ANDROID_BIN=/Users/ktyacke/DEV/adt-bundle-mac-x86_64-20130219/sdk/tools/android
The message you have means the phonegap script cannot find the android binary, which is included in the SDK you downloaded. Add the binary's directory, as well as the android tool's directory, to your path defined in ~/.bash_profile or any other location you're initializing the shell's environment variables.

Categories

Resources