How to Increase the Memory that's available to gradle - android

In the official post detailing the features of the stable release of Android Studio 2.1, I came across this:
We are also speeding up build times by using in-process dex, which converts class files to dex files within the Gradle daemon process. This avoids the costly processing operation of creating separate dex processes. To use this feature, you will need to increase the amount of memory available to the Gradle daemon to at least 2GB (1 GB is the default).
Please, how can I increase the memory that's available to Gradle?

In Android Studio 2.1 open your gradle.properties file. Find the line
\# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Uncomment the line. Gradle was faster after I did this.

In your app's build.gradle file, add the following:
android{
//....
dexOptions {
javaMaxHeapSize "2g"
}
}

I have not try this but it should works:
In graddle.build file:
apply plugin: 'java'
compileJava {
//raise heap
options.fork = 'true'
options.forkOptions.with {
memoryMaximumSize = '2048m'
}
}
If it does not work in Windows try to add it to a new file gradle.properties next to gradle.build
References: http://www.gubatron.com/blog/2014/07/29/solved-gradle-how-to-increase-the-java-compilers-available-heap-memory/
I hope it helps you

Related

Building gradle takes so long (8 minutes to 20 minuntes)

Is it normal that building Gradle of my android project takes at least 8 minutes and something goes up to 20 minutes?
I am running on Windows 10 with 8 Gb RAM, 1.7 Ghz Intel core i3 processor.
If not, kindly advice how to speed up the building time.
Following the steps will make it 10 times faster and reduce build time 90%
First create a file named gradle.properties in the following directory:
/home/<username>/.gradle/ (Linux)
/Users/<username>/.gradle/ (Mac)
C:\Users\<username>\.gradle (Windows)
Add this line to the file:
org.gradle.daemon=true
org.gradle.parallel=true
You can add following code in gradle.properties . this file will be under your project folder.
org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.configureondemand=true
And add following code in you app build.gradle file
lintOptions {
disable 'InvalidPackage'
}
dexOptions {
incremental true
maxProcessCount 4
javaMaxHeapSize "4g"
}
tasks.whenTaskAdded { task ->
if (task.name.equals("lint")) {
task.enabled = false
}
Delete the files in build/android-profile
(It contains .rawproto and .json files)
After doing this you may see monstrous speed increases. One of the reasons for the slowdown may be Lint checking these files during the build. The folder had grown to 2GB on my computer and contained over 7000 files. That can't be good. I guess an alternative approach could be to configure Lint to ignore this folder, but I haven't investigated any further.
Warning: I am not completely sure whether these files have any value, so you can check for yourself. You definitely don't need them in order to build the project, that's for sure (you don't check them in GIT anyway).

Gradle sync failed (android studio 2.2)

the event log say
5:11:08 PM Gradle sync started
5:11:35 PM Gradle sync failed: Process 'command '/usr/local/android-studio/jre/bin/java'' finished with non-zero exit value 2
Consult IDE log for more details (Help | Show Log)
delete .gradle file (fail)
restart android studio/pc (fail)
offline work (fail)
use local gradle distro (fail)
reinstall (fail)
update android studio (fail)
for more information : this is the first time im using android studio.
edit : sorry for my formatting
I did this in Android studio 2.3:
Download new version of gradle from http://services.gradle.org/distributions/ and extract it. Open your project go to file>settings choose build, execution, deployment select gradle choose 'use local gradle distribution' and set the path to the updated gradle. Import project again (make sure you're connected to the internet).
I have found the answer for me on askubuntu, but since I found this thread first, I'll post it here for others:
The problem is there in the error report:
/usr/local/android-studio/jre/bin/java
It tries to find a java runtime in its own folder, while you probably have java installed elsewhere. Simply go to File -> Other Settings -> Default Project Structure, and set the JDK folder to the root folder of your JDK. (e.g. C:\Program Files\jdk1.8.0_111)
I just wish it was more clear on what its problem was...
Try below codes in your app level build.gradle file
multiDexEnabled = true
dexOptions {
preDexLibraries = false
incremental true
javaMaxHeapSize "4g"
}
This issue is quite possibly due to exceeding the 65K methods dex limit imposed by Android. This problem can be solved either by cleaning the project, and removing some unused libraries and methods from dependencies in build.gradle, OR by adding multidex support.
So, If you have to keep libraries and methods, then you can enable multi dex support by declaring it in the gradle config.
defaultConfig {
// Enabling multidex support.
multiDexEnabled true
}
Also it is possible that two or more liabraries conflict (same library but different versions). Check your app build.gradle in dependencies block.
Here are possibility of mulitple reasone for Gradle Sync Failed.
1) You have same library or jar file included several places and some of them conflicting with each other.
2) Check if you have 2 classes with same name.
Solutions
1) Remove unnecessary complie library code in build.gradle
dependencies { compile 'com.google.android.gms:play-services:7.5.0' } which was causing over 65k methods, so removed it,gradle sync, cleaned project, and then ran again and then this error stopped. I needed just maps and gcm so i put these lines and synced project
compile 'com.google.android.gms:play-services-gcm:7.5.0' compile 'com.google.android.gms:play-services-location:7.5.0'
2) Add below code to your build.gradle file.It may manage duplication of libraries
packagingOptions { exclude 'META-INF/NOTICE' exclude 'META-INF/notice.txt' exclude 'META-INF/NOTICE.txt' }
defaultConfig { ... ... multiDexEnabled true }
For Reference Java finished with non-zero exit value 2 - Android Gradle

To run dex in process, the Gradle daemon needs a larger heap. It currently has approximately 910 MB

I am getting this Gradle error every time I run my app. The error is:
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
Here is my build.gradle (Module:app) file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "mobileapp.tech2dsk"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
}
And here is my build.gradle(Project) file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and here is my gradle.properties file :
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
Update from June 25, 2017
At Google IO 2017 there was some updates about this topic. It's not recommended anymore to set the flag on the dexOptions, so if you have something like the next, you can delete it.
dexOptions {
javaMaxHeapSize "2g"
}
Source: Speeding Up Your Android Gradle Builds
Original answer:
Android Studio 2.1 enables Dex In Process which shares the VM with Gradle to improve build times. Due to this, it's necessary to increase the size of the Gradle daemon to incorporate those Dex processes.
At least it's necessary 2 gigs of memory, but you can try with three if you can afford it.
gradle.properties.
org.gradle.jvmargs=-Xmx3072m
By default the Java max heap size is one gig, but in case you have modified it on the build.gradle file...
build.gradle (App)
dexOptions {
javaMaxHeapSize "2g"
}
...you have to resize the Gradle daemon size accordingly (has to be bigger to fit the heap).
Resources
Official blog post by Reto Meier
Official Video in Android
Developers
DexOptions docs.
Notes:
Please realize above numbers varies from machine to machine.
Add in your Build.gradle
android {
dexOptions {
javaMaxHeapSize "4g"
}
}
More info - Android Gradle: What is javaMaxHeapSize "4g"?
Simply leave that on your gradle.properties:
org.gradle.jvmargs=-Xmx2048m
You can look at that article as reference.
I know it's too late for the answer but hope it will helpful to someone
I have tried lot of thing and search lot but nothing works for me include following and build.gradle (App) also dosen't work in my case
android {
dexOptions {
javaMaxHeapSize "4g"
}
}
Also You Can Try Add on your gradle.properties:
org.gradle.jvmargs=-Xmx2048m
In my case the problem is cpu utilization there is lot of process running while i build apk i just kill some processes like genymotion,skype,safari etc and than invalidate caches/restart it works for me.
Modify or create a file named gradle.properties in the following directory.
For Mac,
/Users/USER_NAME/.gradle/gradle.properties
For Linux,
/home/USER_NAME/.gradle/gradle.properties
For Windows,
C:\Users\USER_NAME.gradle\gradle.properties
and apply the above changes. Finally it should look like,
org.gradle.daemon=true
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
org.gradle.parallel=true
org.gradle.configureondemand=true
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Please refer this link
https://tausiq.wordpress.com/2015/02/24/android-speed-up-gradle-build-process-in-android-studio/
You can go ahead and change build.gradle like that (like Veeresh Charantimath suggested):
android {
dexOptions {
javaMaxHeapSize "2g"
}
}
And then you might have to mess around with MultiDex stuff (you will need it for big android projects). But your problem may be simpler. There is some chance that you compile and include in your project some not needed libraries. I faced this issue on my tiny project. Here is what I did to fix it:
I removed this from build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
And I manually removed my build folder from application root. Problem solved.
Is it an error or a warning?
Sometimes this is only a warning and I can manage to build it successfully:
:transformClassesWithDexForDebug
To run dex in process, the Gradle daemon needs a larger heap.
It currently has approximately 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB.
To do this set org.gradle.jvmargs=-Xmx2048M in the project gradle.properties.
For more information see https://docs.gradle.org/current/userguide/build_environment.html
:mergeDebugJniLibFolders UP-TO-DATE
:transformNative_libsWithMergeJniLibsForDebug
:processDebugJavaRes UP-TO-DATE
:transformResourcesWithMergeJavaResForDebug
:validateDebugSigning
:packageDebug
:zipalignDebug
:assembleDebug
:cdvBuildDebug
BUILD SUCCESSFUL
Total time: 3 mins 44.991 secs
Built the following apk(s):
(...)
So the problem that may prevent a successful build may be another one...
In WINDOW'S open the gradle.properties from ~\Users\UserName\.gradle as shown below: for more info enter link description here
and make sure to set its value to minimum to 4608M as shown below for faster builds
org.gradle.jvmargs=-Xmx4608M
Once you do that then you can see the day and night difference in speed building your app...
I'm pretty sure javaMaxHeapSize and org.gradle.jvmargs are mutually exclusive.
javaMaxHeapSize is the old way of doing it, with dex run outside of the process. This will be an argument to the dex process.
org.gradle.jvmargs is the new way of doing it, with dex inside the gradle process. This is the max memory for the whole process (gradle, including dex)
What the warning says is that org.gradle.jvmargs should be >= javaMaxHeapSize (what you used previously for dex) + 512M (an estimation of what's needed for gradle own needs).
So if you want to use dex in process (which is faster), you shouldn't care about javaMaxHeapSize. Just set org.gradle.jvmargs to a correct value for your project/machine.
There appears to be a bug:
https://code.google.com/p/android/issues/detail?id=208214
Using com.android.tools.build:gradle:2.1.2 I set org.gradle.jvmargs=-Xmx3415M with dexOptions.javaMaxHeapSize "2g" and the error message went away.
Please check at first gradle.propertises at your project root directory.
If it is absence, please create a file as like this , then add org.gradle.jvmargs=-Xmx1536m
otherwise if already there , please increase the size.
I have faced same problem , where i have solved by this way.
Example, in gradle.properties of your project root.
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m
Try this
defaultConfig {
...
// Enabling multidex support.
multiDexEnabled true
}

how to speed up android studio build time in my project

I have 5 modules in my project, it takes me 2 mins to build everytime, is there any way to speed up android studio build time?
You can try to add this in your build.gradle file inside the android closure
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
It will allocate large heap size for your dex process which usually takes more time in build.
You can change your your heap size according to RAM you have in your system like "2048M" for 2GB allocation.
Another idea might be using a faster repository. If your build is using mavenCentral() try to replace it with jcenter().
For me, adding the following two properties to the "gradle.properties" file in the project root improved performance considerably.
org.gradle.parallel=true
org.gradle.daemon=true
"org.gradle.parallel=true" does parallel processing of the modules. You may receive a message that the feature is experimental but it has worked for me without any problems.
"org.gradle.daemon=true" will keep a dedicated Gradle JVM running so that it is not re-started each time you do a build. This first build takes as long but subsequent builds are much quicker.
Hope this helps.
Yup you can speed up android studio build time.
1. You need to clean your project before building your project.
2. Close all other projects on which you are not working before build.
3. Allow auto build option.
According to this document https://developer.android.com/studio/build/multidex.html#dev-build
If you are using MultiDexApplication you can set minSdkVersion 21 for your develop productFlavors to mitigate the longer build times for multidex output.
productFlavors {
dev {
// Enable pre-dexing to produce an APK that can be tested on
// Android 5.0+ without the time-consuming DEX build processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the production version.
minSdkVersion 14
}
}

How to solve java.lang.OutOfMemoryError: GC overhead limit exceeded error in android studio

I am using android studio 1.0 RC for 64 bit linux.
When I run my application I am getting
"java.lang.OutOfMemoryError: GC overhead limit exceeded"
When I searched on how to solve this error I got solutions like add:
-XX:-UseGCOverheadLimit to studio.vmoptions or studio64.vmoptions
-Xmx2000m to studio.vmoptions or studio64.vmoptions etc.
These did not work for me.
Please help. Thanks in advance
I solved this issue by adding
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
to the android closure in build.gradle file.
Found this answer in
OutOfMemoryError: GC overhead limit exceeded
Add this to your "gradle.properties" file:
org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError
Also, read this article. You might be able to make the building a bit faster, by adding a combination of those:
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true
EDIT: an updated answer based on my experience:
On Android Studio, choose Help -> Edit custom VM options , and then set the max memory the IDE is allowed to use. For example, if you want 5GB, use this:
-Xmx5g
Save the file, close all windows of the IDE (make sure it has no processes) and then restart the IDE.
EDIT: Android Studio now has RAM configuration in its settings. Go to "help"->"change memory settings".
Find the Memory Settings
Cmd + Shift + A on Mac (Ctrl + Shift + A on Windows) or click on Help
Type "Memory Settings"
under Preferences/ Settings and increase the IDE Heap Size and/ or the Daemon Heap Size to your satisfaction
I had the same issue too - mine was for a different reason. I was working on backing up some files and accidentally dropped a big file in the resource folder. It was close to 40MB.
Once this file was removed, the error was gone.
Add this line in your build.gradle
dexOptions
{
incremental false
javaMaxHeapSize "2048M"
preDexLibraries = false
}
I also have this problem
my solution is :
Just modify the gradle.properties in the root of your project :
org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m
the default jvmargs is 256 when you compile a lot of channel apk then will cause this problem !
For Android projects you use gradle plugin, at root build.gradle.kts:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:${gradle_plugin_version}")
}
}
Upgrade gradle_plugin_version to the latest one.
The reason for that is the latest Gradle version should be used with the latest Gradle plugin version.
For example, for Gradle 6.3 you should use gradle-plugin 4.0.1:
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:4.0.1")
}
}
If you are using IntelllijIdea, From the main menu, select Help | Change Memory Settings.
Set the necessary amount of memory that you want to allocate and click Save and Restart.
This action changes the value of the -Xmx option used by the JVM and restarts IntelliJ IDEA with the new setting.

Categories

Resources