Instant Run takes longer than standard builds - android

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

Related

Android Studio not generating APK

I have a strange problem that just started with an existing Android Studio 2.3.3 project.
The build process is not automatically generating an APK, in this case app-debug.apk Log shows build is successful, but no APK anywhere in the project directory tree. Manually selecting Build / Build APK works fine, however.
This is a problem when I change the source and click run. The build process is successful, but then get "Error Installing APK" into the emulator (because the APK does not exist).
This problem only happens on the one project, and I was playing around with Espresso earlier. My guess is I changed a setting somewhere, I just cannot figure out which one. What do I need to change to have the APK generated automatically again? build.gradle included.
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 16
targetSdkVersion 25
versionCode 2
versionName "0.0.2"
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file("/home/user/keystore/keystore.jks")
storePassword "xxxxxxxxxxxxxxxxxx"
keyAlias "xxxxxxxxxxxxxxxxx"
keyPassword "xxxxxxxxxxxxxxxxxxx"
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.android.gms:play-services-ads:10.2.4'
compile 'com.google.android.gms:play-services-location:10.2.4'
testCompile 'junit:junit:4.12'
}
Try below solution -
At right side of Android studio you have a gradle option click on that and then click the refresh icon and then check build.
More details follow below link -
apk not generating
My Android Studio does it all the time so I came up with a workaround. Instead of going to Build > Build APK. I simply run the app.
After running the app if you go to build > output > apk in your project directory, you will find your APK over there. Android Studio rarely throws an error when you run the app.
EDIT
This APK does NOT work on physical devices I dont know what the reason is. It runs on emulators though.
Something is not right with the key, maybe it is not on the path... A studio does not report anything then (simply does not build bundle). Click on the folder icon in the key alias field it will be an error if the key is not on the path (fastest check).
Due to many dependencies in Build.gradle android studio takes too much time and then said Request time out. So have a look into Build.gradle.
Do these steps.
First Method: try to minimize the dependencies
Second Method: Add MultiDEX implementation 'androidx.multidex:multidex:2.0.1' this is for androidX.
Third Method: Make class public class ApplicationDelegate extends MultiDexApplication { } then add in Android Manifest File like this in Application tag
<application
android:name=".ApplicationDelegate"
android:allowBackup="false"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">

Why is the release build variant always debuggable?

Whether I create the "release" APK by:
Using Generate Signed APK in Android Studio
Select the Release build variant and use Tools -> Build APK
Run the assembleRelease task
... the APK produced always has debuggable=true which I've confirmed by trying to upload them to Google Play, which says:
"Upload failed. You uploaded a debuggable APK. For security reasons you need to disable debugging before it can be published in Google Play."
The (only) manifest has no debuggable attribute specified. Gradle specifies debuggable=false for release, and true for debug, see below.
What am I missing? Where is the debuggable state coming from, and why is the debuggable=false in the release build type declaration being ignored? I do not want to add debuggable=false to the manifest and to have to keep manually enabling/disabling it.
app/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "com.myapp.android"
minSdkVersion 14
targetSdkVersion 26
versionCode 5
versionName 5
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile rootProject.file("keystore.jks")
if (storeFile.exists()) {
def config = new Properties()
config.load(new FileInputStream(rootProject.file("keystore.passwords")))
storePassword config.KeystorePassword
keyAlias config.KeyAlias
keyPassword config.KeyPassword
}
}
}
buildTypes {
release {
debuggable false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
debug {
debuggable true
applicationIdSuffix ".debug"
}
}
dataBinding {
enabled = true
}
lintOptions {
disable 'RtlHardcoded'
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
// Copy release APK to project root
task copyReleaseApk(type: Copy) {
from 'build/outputs/apk'
into '..'
include '**/*release.apk'
}
afterEvaluate {
if (tasks.findByPath("packageRelease") == null) {tasks.create("packageRelease")}
tasks.findByPath("packageRelease").finalizedBy(copyReleaseApk)
}
}
ext {
// Single place to specify the support library version
supportLibraryVersion = '26.0.0-beta2'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.google.code.findbugs'
exclude module: 'espresso-idling-resource'
exclude group: "javax.inject"
})
implementation 'com.android.support.test.espresso:espresso-contrib:2.2.2'
// Dagger dependency injection
implementation 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
implementation 'com.google.dagger:dagger-android:2.10'
implementation 'com.google.dagger:dagger-android-support:2.10'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.10'
implementation "com.android.support:appcompat-v7:$supportLibraryVersion"
implementation "com.android.support:design:$supportLibraryVersion"
implementation "com.android.support.constraint:constraint-layout:1.0.2"
implementation "com.jakewharton.timber:timber:4.5.1"
implementation "com.squareup.phrase:phrase:1.1.0"
implementation "com.squareup.retrofit2:retrofit:2.2.0"
implementation "com.squareup.retrofit2:converter-gson:2.2.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.7.0"
implementation 'net.danlew:android.joda:2.9.9'
testImplementation 'junit:junit:4.12'
implementation 'com.google.firebase:firebase-crash:11.0.0'
androidTestImplementation 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
Update 1: I tried adding debuggable=false to the manifest and it makes no difference, the APK produced still cannot be uploaded to Google Play.
Update 2: I loaded the APKs back into Android Studio using the APK Analyzer that makes it easy to see the manifest, and they all include.... debuggable=true. Where is it coming from?
Update 3: assembleRelease produces a debuggable APK on both my local machine AND on the CI server (BuddyBuild).
Update 4: A clean rebuild (including deleting the build folders) and restarting Android Studio with its caches cleared makes no difference.
Update 5: It seems reasonable to assume that the debuggable=true state could be coming from one of the dependencies, but if that is the case which, and how can that be overridden?
As the project is targeting API 26 and using 3.0.0-alpha4 of the android gradle plugin, 26.0.0-beta2 build tools, and gradle 4.0-rc1 I thought I should check that the issue does not relate to an issue with these pre-release tools. So I reverted to API 25 and the stable releases of gradle 3.3, gradle plugin 2.3.3 and build tools 25.0.3. This was a little tedious as I had to downgrade all the Java 8 syntax from the source to Java 7. But having done that, the build process now works as expected and produces release APK artifacts that do not contain the debuggable="true" flag and can be uploaded to Google Play. 👍
I'm not clear specifically where the cause is but I've logged this in the Android tools bug tracker as it seems possible it is a bug:
https://issuetracker.google.com/issues/62899843
UPDATE: The response from the tools team is that this is expected behaviour because the app targets API 26 and that is in preview. I thought as the 26 APIs were final that APKs could be built against it and released to Google Play but clearly not.
This is because you probably have incremental builds, by default all incremental builds are assumed debuggable.
Under the General Notes of the SDK Tools Revision 8, it states:
Support for a true debug build. Developers no longer need to add the
android:debuggable attribute to the tag in the manifest
— the build tools add the attribute automatically. In Eclipse/ADT, all
incremental builds are assumed to be debug builds, so the tools insert
android:debuggable="true". When exporting a signed release build, the
tools do not add the attribute. In Ant, a ant debug command
automatically inserts the android:debuggable="true" attribute, while
ant release does not. If android:debuggable="true" is manually set,
then ant release will actually do a debug build, rather than a release
build.

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

Actually the main error is "java.exe finished with non-zero exit value 1". First i tell you every problem which i faced after installing studio:
Three days ago, i just installed android studio & I created new project.
1) First it throw the error "Plugin is too old, please update to more recent version", after searching on google i changed
classpath : com.android.tools.build:gradle:2.0.0-alpha2
to
classpath : com.android.tools.build:gradle:2.0.0-alpha8
Current Error solved.
2) After that it was asking for gradle 2.10, i updated this one also & set the path.
Current Error solved.
3) When i ran my application i got one more error "app-debug-unaligned.apk, specified for property 'input file' does not exist".
I searched on internet, i got one solution on stackoverflow. So as answer on stackoverflow i go to "Build" & i selected build apk.
Current error solved.
4) But after that again i got one error
"To run dex in process, the Gradle daemon needs a larger heap. It currently has 910 MB.
For faster builds, increase the maximum heap size for the Gradle daemon to more than 1G.
java.exe finished with non-zero exit value 1".
I have been searching on stackoverflow for last three days, i applied each and every answer one by one but i'm not able to solve the error. Please save my life, i am really tired of this problem. I show you image what error is coming exactly
My build.gradle file
apply `plugin: com.android.application`
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "java.danish.org.myapplication"
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.1.1'
compile 'com.android.support:design:23.1.1'
}
I updated everything SDK platforms & SDk Tools.
Please tell me what i am doing wrong here.
Issue
In gradle plugin version 2.0.0-alpha7 and -alpha8 Dex runs inside gradle build process as opposed to a separate process.
Option a)
Change gradle plugin version to 2.0.0-alpha9 where in-process Dex is disabled by default.
classpath 'com.android.tools.build:gradle:2.0.0-alpha9'
Option b)
Disable in-process dex in your app module build.gradle:
android {
// ...
dexOptions {
dexInProcess = false
}
}
Option c)
Increase memory available to gradle process.
Create or update gradle.properties file in your project root directory:
# Default value: -Xmx10248m -XX:MaxPermSize=256m
org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=512m
And update your app module build.gradle file:
dexOptions {
preDexLibraries true
javaMaxHeapSize "3g"
incremental true
dexInProcess = true
}
These values are experimental and work for my setup. I use 3 GB for dex and 4 GB for gradle (3 + 1 GB).
Note
If you have any issues update to alpha9 anyway.
I found the solution.
Changes
1)
dexOptions {
javaMaxHeapSize "4g"
}
2)
lintOptions {
checkReleaseBuilds false
abortOnError false
}
This is my new build.gradle and everything is working fine now.
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.0 rc4"
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.aquasoft.guesp"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile 'com.android.support:recyclerview-v7:23.3.0'
compile 'com.squareup.picasso:picasso:2.5.0'
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.android.support:design:23.4.0'
compile 'com.stripe:stripe-android:+'
compile 'com.roomorama:caldroid:3.0.1'
compile 'com.android.support:cardview-v7:23.3.+'
}
try this gradle params
defaultConfig {
...
// Enabling multidex support.
multiDexEnabled true
}

Workaround to link a shared library in debug mode with Android Studio (v2+)

Long ago, a Google Code ticket was opened as the Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library:
DESCRIPTION:
Gradle plugin does not propagate whether you are doing a
debug build or a release build to a dependent android library.
VERSION INFORMATION:
gradle plugin: 0.3 gradle: 1.4
This is a problem, especially when the release configuration of the library prevents the edition of the code or the debug process to occur normally (red highlighting of native methods as the IDE does not find any longer while they link normally, broken Java step-by-step debugging...).
This was quite thoroughly discussed on Google Code and here on Stack Overflow, and it happened that Kane O'Riley proposed a really interesting (and clean) workaround:
Put this in your app:
dependencies {
debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
releaseCompile project(path: ':custom_lib', configuration: "libraryRelease")
}
and in your library's build.gradle add:
defaultPublishConfig 'release'
publishNonDefault true
productFlavors {
library {
}
}
I gave a try, but I have the following message at Gradle sync time:
Error:Configuration with name 'libraryDebug' not found.
I tried with just "debug" and "release" (which are the build config names I use in my lib) but the result is the same.
Any idea to make this interesting workaround working? (I'm running Android Studio 2.0 beta 2)
APPENDIX:
my lib's build.gradle file (including the ugly workaround I'm currently using):
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
// THIS IS WHERE I INSERT THE THREE STATEMENTS PERTAINING
// TO THE LIB FROM Kane O'Riley'S WORKAROUND ABOVE
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
/*
release {
// UNCOMMENT BEFORE MAKING AN ACTUAL RELEASE !!! (duh...)
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-fvisibility=hidden -g0 -DSHLUBLU_ACTUAL_RELEASE -O3"
}
buildConfigField "boolean", "actualRelease", "true"
debuggable false
jniDebuggable false
minifyEnabled false
}
*/
release {
// COMMENT THIS WHOLE BLOCK BEFORE MAKING AN ACTUAL RELEASE !!!
// (this is a copy of the "debug" config below... did I say 'duh' ?)
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-g"
}
buildConfigField "boolean", "actualRelease", "false"
debuggable true
jniDebuggable true
minifyEnabled false
}
debug {
// IF ONLY THIS ONE WAS USED WHEN DEBUGGING!
// (Only the IDE uses that, but the release build is actually linked,
// see https://code.google.com/p/android/issues/detail?id=52962 )
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-g"
}
buildConfigField "boolean", "actualRelease", "false"
debuggable true
jniDebuggable true
minifyEnabled false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
my project's build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
versionCode 27
versionName "1.4"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
debuggable false
jniDebuggable false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-shlublu.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':mylib')
// THE LINE ABOVE IS WHAT I REPLACE BY Kane O'Riley's WORKAROUND
// DESCRIBED AT THE BEGINNING OF THIS POST
}
my Gradle config:
For this to work, the twobuild.gradle files must not be modified at the same time before syncing once for all. I had to follow the following steps:
Step 1: modify the lib's build.gradle, exactly as Kane said:
// "android" section:
defaultPublishConfig 'release'
publishNonDefault true
productFlavors {
library {
/* This strange empty flavour is actually needed
for the step 3 to be successful */
}
}
Step 2: clean/rebuild
Step 3: modify the app's build.gradle, also as Kane said:
dependencies {
debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
releaseCompile project(path: ':custom_lib', configuration: "libraryRelease")
}
Step 4: Gradle sync.
So it was just a matter of changing the library first, and then cleaning before modifying the app.
I checked the APK produced by the debug and release build modes, and each of them contains the proper variant of the lib, unlike before applying this workaround, so it does work (thanks Kane !).
Shlublu's solution didn't work for me, no matter what steps I used.
After much digging, found out that one can define available configurations (at least using experimental gradle), and this in turn avoided having to clean/rebuild in hope of fixing all errors.
Using latest gradle with AS2.1, I was able to solve this issue with a very simple dependency content:
Lib's build.gradle (uses experimental gradle 0.7.0):
model {
android {
...
productFlavors {
create('all') {
...
}
}
publishNonDefault true
}
}
configurations {
allDebug
allRelease
}
App's build.gradle (uses standard gradle 2.1.0):
debugCompile project(path: ':lib', configuration: 'allDebug')
releaseCompile project(path: ':lib', configuration: 'allRelease')
I'm pretty sure the productFlavors in the lib's gradle is not needed if using debug/release configurations only, like this:
Lib's build.gradle (uses experimental gradle 0.7.0):
model {
...
}
configurations {
debug
release
}
App's build.gradle (uses standard gradle 2.1.0):
debugCompile project(path: ':lib', configuration: 'debug')
releaseCompile project(path: ':lib', configuration: 'release')

Why does my build takes so long is Android Studio?

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.

Categories

Resources