Instant Run takes longer than standard builds
Asked Answered
L

3

13

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'
Leger answered 27/12, 2016 at 15:32 Comment(15)
maybe cuz of this bug code.google.com/p/android/issues/detail?id=206698 ?, I don't know what build you haveStethoscope
@Dasser yeah, who knows? Seems just a list of people complaining, more than a bug report.Leger
You have read the limitations of instant run and ensured your project doesn't victim to one of the listed pitfalls, yeah?Reine
@Reine I have now, no pitfalls. It used to work, also. Something changed in the last few updates, but I had no time to trace the issue as soon as I saw it.Leger
@Leger is this for a specific project or any project?Watt
@Charuka I work only on a project, I can create a empty project but I doubt that would be significant.Leger
@Leger Once you have time try that,I know it does not solve your problem but if that works fine maybe we can bit more focus on dependencies and memory allocation as wellWatt
Please check https://mcmap.net/q/102482/-no-changes-to-deploy-android-studio-2-1 and https://mcmap.net/q/100813/-instant-run-in-android-studio-2-0-how-to-turn-offBaruch
@Charuka it works well with a ‘empty’ project.Leger
@Amit I found no solution there.Leger
@Leger then its time to open your gradle here lets have a look!Watt
i know, i just wanted to tell you that, instant run still has bugs.Baruch
@Charuka edited question.Leger
@Leger I think this is a memory leak in the Gradle daemon , did you manually add this org.gradle.daemon=true remove it and seeWatt
@Charuka I experience the same behaviour even without using the daemon.Leger
Z
1

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
Zasuwa answered 4/1, 2017 at 7:13 Comment(1)
Already using 2.2.3, and already setting some of these parameters in the build file, as you can see in the question. If you expand on this, I can try tweaking them, but I have tried before.Leger
E
1

Enable Instant Run

  1. Open the Settings or Preferences dialog.
  2. Navigate to Build, Execution, Deployment > Instant Run.
  3. 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.

Endothelioma answered 5/1, 2017 at 5:3 Comment(2)
@Leger Please check project level build.gradle ===== task clean(type: Delete) { delete rootProject.buildDir }Endothelioma
Do not place Android context classes in static fields; this is a memory leak and also breaks Instant RunEndothelioma
E
-1

Maintain bulit versions and target version as same

Eupatorium answered 3/1, 2017 at 11:43 Comment(3)
please add details .Its a bounty questionJiva
I am, build version is 25, target version is 25 and build tools version is 25.0.2 in all my modules.Leger
compileSdkVersion 23, buildToolsVersion "23.0.3", minSdkVersion 11, targetSdkVersion 23, By maintaining all this i resolved the issueEupatorium

© 2022 - 2024 — McMap. All rights reserved.