Why does my build takes so long is Android Studio? - android

My build takes to long in Android Studio, more than 2 minutes, sometimes 3'. I tried some methods explained in StackOverflow to accelerate the build time, but it didn't solve my problem.
My build.gradle code for app module is the following:
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
}
// Adding The GIT SHA to Crashlytics crash reporting
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.moymer"
minSdkVersion 11
targetSdkVersion 22
multiDexEnabled true
versionCode 5
versionName "2.0.0"
buildConfigField "String", "GIT_SHA", "\"${gitSha}\""
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.android.support:design:22.2.0'
compile 'com.github.nirhart:parallaxscroll:1.0'
compile 'com.android.support:multidex:1.0.0'
compile('com.crashlytics.sdk.android:crashlytics:2.4.0#aar') {
transitive = true;
}
compile 'in.srain.cube:grid-view-with-header-footer:1.0.12'
compile 'com.android.support:recyclerview-v7:23+'
compile 'com.squareup:otto:1.3.8'
compile 'com.android.support:percent:23.0.0'
compile 'com.jakewharton.timber:timber:3.1.0'
compile project(':androidffmpeglibrary')
}
My gradle.properties file is the following:
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

You have 11+ dependencies and an extra plugin. At least one (androidffmpeglibrary) would appear to involve the NDK. That is not going to be fast, particularly depending on your build hardware.
If you want a faster build, get rid of some of that junk. For example, there have been finer-grained Play Services SDK dependencies for over a year; use only the pieces that you need, rather than the "everything but the kitchen sink" play-services artifact. That, in turn, might allow you to dump multidex, as you might fall back below the 64K DEX method reference limit.
The Android Tools team is working on improving the build speed, which will show up in newer versions of the Android Plugin for Gradle. That must be in your top-level build.gradle file, as I don't see where you are including that here. Make sure you are on the latest one, and keep updating it as new editions come out.

As CommonsWare depicted, having a lot of dependencies does slow down the overall build. As of now, you can start using Android Studio 2.0 which has the instant run feature.
Instant run cuts down the build time by a big factor because it only pushes the small changes that are not. Whatever changes you do in the code, are first classified into three categories - hot swap, warm swap and cold swap. Lesser the load, warmer the swap, quicker the build-time.
Big structural changes like changing annotations, static fields etc come into cold swap. Changes in the manifest file also comes in the cold swap. You can check the full list here.
The good news is most of the small changes fall in the hot swap and these changes are quickly pushed in the running application as soon as you press the run button. The relevant activity restarts, and you can see your changes in a few seconds.
Stable version of Android Studio 2.0 is released and you can download it from this link. It has this instant run feature. Hope this helps.

Related

Instant Run takes longer than standard builds

It’s been some time now that Instant Run is not working anymore, that is, it’s slower than standard builds.
There’s some material in the AOSP bug tracker, but I could not work this out. I’m sure some of you will help.
Without Instant Run
Builds take about 1.30 min.
With Instant Run
I run the app for the first time, then apply some minor change. For example, change int a = 1 to int a = 2 inside a method, or alter a string.
Then click run again. What happens:
A seemingly full build takes place (calling assembleDebug on all my modules). Takes about 1.30 min.
A second round goes on, calling just app:assembleDebug. It takes about another minute:
16:27:10 Executing tasks: [:app:assembleDebug]
16:28:16 Gradle build finished in 1m 5s 264ms
16:28:22 Instant Run applied code changes and restarted the app.
During the first round, no output whatsoever to Event Log or Gradle Console. During second round, the Gradle Console outputs lots of stuff and ends with
BUILD SUCCESSFUL
Total time: 51.385 secs
PREDEX CACHE HITS: 0
PREDEX CACHE MISSES: 45
Stopped 0 compiler daemon(s).
What can I do?
I have these in my grade.properties file:
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.daemon=true
Nothing dex related in build.gradle files. Not using Jack. Android Studio 2.2.3.
gradle.properties file
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.daemon=true
ANDROID_BUILD_MIN_SDK_VERSION=15
ANDROID_BUILD_TARGET_SDK_VERSION=25
ANDROID_BUILD_SDK_VERSION=25
ANDROID_BUILD_TOOLS_VERSION=25.0.2
Project level build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
Main module build.gradle
(I have other two ‘library’ modules also)
apply plugin: 'com.android.application'
Properties signProp = new Properties()
signProp.load(new FileInputStream(file(project.property("SIGN_PROPS"))))
Properties authProp = new Properties()
authProp.load(new FileInputStream(file(project.property("AUTH_KEYS_PROPS"))))
def PARSE_APPLICATION_ID = '"' + authProp['parseApplicationId'] + '"';
def PARSE_CLIENT_KEY = '"' + authProp['parseClientKey'] + '"'
def PARSE_SERVER_URL = '"' + authProp['parseServerURL'] + '"'
def GOOGLE_PLAY_API_KEY = '"' + authProp['googlePlayApiKey'] + '"'
android {
signingConfigs {
release_config {
keyAlias signProp['keyAlias']
keyPassword signProp['keyPassword']
storeFile file(signProp['storeFilePath'])
storePassword signProp['storePassword']
}
}
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
versionCode 21
versionName "1.3.3"
buildConfigField 'String', 'PARSE_APPLICATION_ID', PARSE_APPLICATION_ID
buildConfigField 'String', 'PARSE_CLIENT_KEY', PARSE_CLIENT_KEY
buildConfigField 'String', 'PARSE_SERVER_URL', PARSE_SERVER_URL
buildConfigField 'String', 'GOOGLE_PLAY_API_KEY', GOOGLE_PLAY_API_KEY
signingConfig signingConfigs.release_config
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
debug {
minifyEnabled false
shrinkResources false
}
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:preference-v14:24.2.1'
compile 'com.google.maps.android:android-maps-utils:0.4.3'
compile 'com.google.android.gms:play-services-maps:9.2.1'
compile 'com.google.android.gms:play-services-appinvite:9.2.1'
compile 'com.google.android.gms:play-services-location:9.2.1'
compile 'com.google.android.gms:play-services-appindexing:9.2.1'
compile ('com.facebook.android:facebook-android-sdk:4.16.1') {
exclude module: 'bolts-tasks'
}
compile 'com.parse.bolts:bolts-tasks:1.4.0'
compile 'com.squareup.okhttp3:okhttp:3.2.0'
compile 'com.parse:parse-android:1.13.1'
compile ('com.parse:parseui-widget-android:0.0.1') {
exclude module: 'parse-android'
}
compile project(':dateTimePicker')
compile project(':commons')
}
apply plugin: 'com.google.gms.google-services'
http://tools.android.com/recent/androidstudio223isnowavailable
Update to the latest version of Android Studio and then increase the memory profile in (studio.vmoptions) based on your system memory
-Xms256m
-Xmx1280m
-XX:MaxPermSize=350m
Enable Instant Run
Open the Settings or Preferences dialog.
Navigate to Build, Execution, Deployment > Instant Run.
check the box next to Enable Instant Run.
Limitations of Instant Run.
Deploying to multiple devices,
Multidexing your app,
Using third-party plugins,
Pushing changes to multi-process apps,
Launching your app in a Work profile.
If you use multidex support (multiDexEnabled true) for your app, minSdkVersion must be 21 or higher. Otherwise, android studio disables Instant Run.
Android plugin for gradle version must be 2.1.0 or higher
Enable Offline Work android studio to speed up:
Go to file >> settings or simply press ctrl + alt + s
file >> settings >> Gradle. please check Offline work under Global Gradle setting
Improve Gradle Performance
After opening android studio, open gradle.properties file from right side of android studio and add the following two lines.
org.gradle.jvmargs=-Xmx1536m
org.gradle.daemon=true
org.gradle.parallel=true
Configure Build Variants of debug in buildTypes
debug {
minifyEnabled false
applicationIdSuffix ".debug"
}
After changes, you should restart your Android Studio.
Note: Build time depend on your processor and RAM.
I noticed in (i7-3610qm with 8gb ram) when you compile 4 android studio project at same time then slow respond on window. after build process finish pc works normal.
Maintain bulit versions and target version as same

Having already installed build-tools but fail to find build tools revision 24.0.1

Here i got a problem, I have already installed build tool in revision 24.0.1, here is the screen snapshot from my terminal:
And in fact I think I wrote it well in the build.gradle filebuildToolsVersion "24.0.1", but while sync the project it still appear such error:
fail to find build tool revision 24.0.1 install such build tools
Another question is about the file place order in android studio. Below is the imported project hierarchy and the manifest file is under the main folder and i can not find any usage file under android perspective.
Such a project hierarchy I have never seen, maybe there is somebody who could demonstrate such answer to me. Thank you.
Update: Here is the build.gradle file picture
You are doing it in the wrong way. There are two types of build.gradle files. The first one is for your project and the second type for module which can be more than one. In your case you are mixing both.
The project's build.gradle should like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and the module build.gradle should look something like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.something"
minSdkVersion 14
targetSdkVersion 24
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:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.android.support:cardview-v7:24.1.1'
compile 'com.android.support:recyclerview-v7:24.1.1'
}
Regarding your second question, the hierarchy you see is called Project view.
By default, Android Studio displays your project files in the Android view. This view does not reflect the actual file hierarchy on disk, but is organized by modules and file types to simplify navigation between key source files of your project, hiding certain files or directories that are not commonly used.
(...) To see the actual file structure of the project including all files hidden from the Android view, select Project from the dropdown at the top of the Project window.
Source: https://developer.android.com/studio/projects/index.html

Add AppCompat dependency to Gradle in Android Studio

Verdant newbie here. I'm making a fairly simple project that needs to be compatible with Android versions older than Lollipop, but still want to have the app look nicer by using AppCompat.
Unfortunately, I do not know how to work (or even find) the gradle - I tried to run a gradle task: compile "com.android.support:appcompat-v7:21.0.+"
But it returned an error saying that "Task 'compile' is ambiguous...." in my project.
Could anyone tell me how to add this? I've searched all over Google, but everything goes way over my head.
Thanks!
Have you been able to locate Gradle file?
Edit your gradle like this :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.xyz.xyz"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.00.00"
}
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:support-v4:23.3.0'
}
Update your question and post full Gradle code.
Add the appcomapt library(com.android.support:appcompat-v7:21.0.+") in dependancy section of the build.gradle file
Or second way to add library is by searching on maven repo.this can be done by traversing file->project structure->app>dependancy->click (+) ->add Library dependancy
Task 'compile' is ambiguous.
This issue is not the Compile Issue. but this was an issue on the task itself.
if you are not using a command like "gradle compileDebug" then one of the android configurations are executing the task and it is not working well with your build.gradle.
Here is a link that seems to relate specifically to your issue. Click Here

Execution failed for task 'app:prepareDebugAndroidTestDependencies'

I'm trying to build UI tests for my Android application in Android Studio. The problem is that I can't run them. I get the message: Execution failed for task 'app:prepareDebugAndroidTestDependencies' which I couldn't find what it's about. When I run my application, it starts perfectly, but when i try to run tests it shows the mentioned message. My build.gradle is lsited below:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
applicationId "ba.etf.pkks.eldaralmin.libraryapp"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
maven {
url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:support-v4:22.0.0'
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+'
// Supports Android 4.0.3 and later (API level 15)
compile 'com.embarkmobile:zxing-android-minimal:2.0.0#aar'
// Supports Android 2.1 and later (API level 7), but not optimal for later Android versions.
// If you only plan on supporting Android 4.0.3 and up, you don't need to include this.
compile 'com.embarkmobile:zxing-android-legacy:2.0.0#aar'
// Convenience library to launch the scanning and encoding Activities.
// It automatically picks the best scanning library from the above two, depending on the
// Android version and what is available.
compile 'com.embarkmobile:zxing-android-integration:2.0.0#aar'
// Version 3.0.x of zxing core contains some code that is not compatible on Android 2.2 and earlier.
// This mostly affects encoding, but you should test if you plan to support these versions.
// Older versions e.g. 2.2 may also work if you need support for older Android versions.
compile 'com.google.zxing:core:3.0.1'
compile 'com.nononsenseapps:filepicker:2.1'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
compile 'com.google.code.gson:gson:1.7.2'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
}
My run/debug configurations for tests are in the image below:
http://pokit.org/get/?5b2509c64e5049b1b168c99b95313d5e.jpg
Can anyone help me to figure out where the problem is?
In your apps build.gradle add the following within android { }:
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
}
But why would lead to such?
When instrumentation tests are run, both the main APK and test APK share the same classpath. Gradle build will fail if the main APK and the test APK use the same library (e.g. Guava) but in different versions. If gradle didn't catch that, your app could behave differently during tests and during normal run (including crashing in one of the cases).
To make the build succeed, just make sure both APKs use the same version. If the error is about an indirect dependency (a library you didn't mention in your build.gradle), just add a dependency for the newer version to the configuration ("compile" or "androidTestCompile") that needs it. You can also use Gradle's resolution strategy mechanism. You can inspect the dependency tree by running ./gradlew :app:dependencies and ./gradlew :app:androidDependencies.

Compilation errors with Gradle, Android Studio and Butterknife

I've imported Eclipse Android project to Android Studio and it fails to compile with following errors:
Error:(7, 8) error: duplicate class: com.company.fragment.PrimaryFragment$$ViewInjector
I've tried adding apt plugin:
apply plugin: 'android'
apply plugin: 'apt'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
disable 'InvalidPackage'
}
packagingOptions {
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
}
dependencies {
compile project(':library')
compile 'com.android.support:support-v4:+'
apt "com.jakewharton:butterknife:5.1.1"
compile 'com.jakewharton:butterknife:5.1.1'
}
I think that this has something to do with Butterknife. But when I make a clean project with it, everything is ok.
If you still have the target/build folders from eclipse in your build directory it might be a good idea to delete those.
Also Cleaning project under Project -> Clean might help.
Also if you are running maven or gradle from command line, it would be wise to run:
maven clean
or
gradle clean
You don't need both the apt and the compile dependency, only the compile dependency.
By having it twice, the annotation processor is running twice (with two different output directories) which means the generated classes are created twice. The error you are seeing is because there are two identical classes being generated for your PrimaryFragment.
The apt scope (and plugin) are only needed for libraries which separate their runtimes from their annotation processors (sometimes called compilers). An example of this would be Dagger which has a dagger and dagger-compiler.

Categories

Resources