54

I just enabled instant run in my android studio project. (Followed the instructions here)

My project contains git submodules and somehow these do not compile anymore.

This is the error i get:

Error:(8, 0) Cannot change dependencies of configuration ':libraries:my_library:classpath' after it has been resolved.

Any idea what could be wrong there ?

Top level build.gradle:

buildscript {
repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-alpha1'
    classpath 'com.novoda:bintray-release:0.2.7'
    classpath 'io.fabric.tools:gradle:1.+'
}}

Module build.gradle:

apply plugin: 'android'
apply plugin: 'io.fabric'

android {

    defaultConfig {
       versionCode 4850
       versionName '4850'
       compileSdkVersion 23
       buildToolsVersion '23.0.1'
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/MANIFEST.MF'
        exclude 'META-INF/NOTICE'
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    useLibrary 'org.apache.http.legacy'
}

repositories {
    mavenCentral()
    jcenter()
}


dependencies {
    [skip]
    compile project(':libraries:my_library:sdk')
}

Library build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
    }

    lintOptions {
        abortOnError false
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'

    testCompile 'junit:junit:4.12'

}
mbonnin
  • 6,893
  • 3
  • 39
  • 55
  • Can you include your `build.gradle` files? – nhaarman Nov 23 '15 at 19:28
  • try removing fabric and see if that's what creates the mess, also use the same buildToolsVersion for all modules – Gabor Dec 09 '15 at 21:15
  • 1
    [Seems you wouldn't need `mavenCentral()` when you already have `jcenter()`.](http://stackoverflow.com/questions/24852219/android-buildscript-repositories-jcenter-vs-mavencentral) – frogatto Jan 19 '16 at 15:54
  • @HiI'mFrogatto exactly... that's what i mentioned in my answer a while ago. – Viral Patel Jan 19 '16 at 16:04
  • @passsy Do the gradle files posted above match your situation exactly? From some of your comments below, it seems like, while you have the same error message, your project structure (with two top level build.gradle files) is different from the question above. You could update this question with your exact situation, but really, you should post your code in a whole new question. – RaGe Jan 19 '16 at 20:51
  • It's the exact same structure. See above. It includes compile project(':libraries:my_library:sdk'). The my_library includes a top level build.gradle which breaks the build process. – passsy Jan 20 '16 at 12:39
  • @passsy Was trying to create a minimal example to recreate error, unable to reproduce. Can you please take a look at https://github.com/foragerr/android-multi-project-sample This project builds fine, what am I missing? – RaGe Jan 21 '16 at 04:28
  • your sample compiles fine for me. I already tried to find a difference but haven't found one in 4h. – passsy Jan 21 '16 at 15:12
  • I tried adding my subproject to your sample -> fails. I moved all my code code in your sample project -> works. Adding the new subproject to my project -> fails. I will continue testing tomorrow – passsy Jan 21 '16 at 15:20
  • @passsy If you can possibly add a project that fails to github, I can look as well. Please do a @ mention if you reply here, didn't see your messages until now. – RaGe Jan 22 '16 at 23:27
  • Personally, I avoid pre-release versions of Android Studio (at least, after 1.0 shipped). So, I will not touch Instant Run until Android Studio ships on the release channel. – CommonsWare Jan 25 '16 at 07:08
  • @passsy I even tried adding your HoloCicularProgressBar project under my github sample project, builds fine. – RaGe Jan 25 '16 at 16:23

8 Answers8

34

gradle reads and executes all build.gradle files in all folders of the included modules. As the error shows, it also tries to execute the root build script of :libraries:my_library.

You have to change your settings.gradle and include the library project by setting its 'projectDir':

include ':app'

// Give your library project any module name, i.e. ':sdk'
include ':sdk'
// Then set the project path of the library module
project(':sdk').projectDir = new File('libraries/my_library/sdk')

With this settings.gradle you can reference the library project as gradle dependency with:

compile project(':sdk')
thaussma
  • 9,756
  • 5
  • 44
  • 46
30

I had the same problem. I resolved it by removing the classpath in the submodule Top-level build.gradle file.

dependencies { 
     // classpath 'com.android.tools.build:gradle:1.0.0'
}

I'm not sure if it's the best thing to do, but it worked for me.

o.akrout
  • 399
  • 5
  • 13
  • this worked for me, too. but it is not ideal as we should not change submodule's build.gradle. – jiawen Apr 09 '16 at 16:17
29

I had the same problem. I compared it to the (working) sample project by @RaGe and found the minor difference.

The sub project folder has to start with a Upper case letter.

Here is the change I did on @RaGes sample to break it and get it working again.

Broken structure:

android-multi-project-sample
    + .gralde
    + .idea
    + app
    + build
    + gradle
    + myApplication2
    - .gitignore
    - android-multi-project-sample.iml
    - build.gradle
    - gradle.properties
    - gradlew
    - gradlew.bat
    - local.properties
    - settings.gradle

results in the following error:

Error:(8, 0) Cannot change dependencies of configuration ':myApplication2:classpath' after it has been resolved.

Working structure (with upper case sub project)

android-multi-project-sample
    + .gralde
    + .idea
    + app
    + build
    + gradle
    + MyApplication2     // upper case!!!!!!
    - .gitignore
    - android-multi-project-sample.iml
    - build.gradle
    - gradle.properties
    - gradlew
    - gradlew.bat
    - local.properties
    - settings.gradle

also the top level settings.gradle has to be changed:

+ include ':app', ':MyApplication2:mylibrary'
- include ':app', ':myApplication2:mylibrary'

and app/build.gradle has to change this

+ compile project(':MyApplication2:mylibrary')
- compile project(':myApplication2:mylibrary')

Everything compiles

Be careful! Git is not case sensitive by default. Use

git mv -f myApplication2 temp
git mv -f temp MyApplication2

to rename the folder.

RaGe
  • 22,696
  • 11
  • 72
  • 104
Stephan
  • 15,704
  • 7
  • 48
  • 63
  • Woah! great catch! Thank you. If it were upto me I would award the bounty to you before it expires. Now atleast we can try to look into why this happens. I'm guessing it is a android plugin bug. – RaGe Jan 26 '16 at 13:05
  • I have to ask, what made you change the case of the project name? – RaGe Jan 26 '16 at 13:05
  • 2
    Thanks! works great. I can't believe I wasted two days because of a single lower case letter. – passsy Jan 26 '16 at 13:09
  • @RaGe I always used small case letter for the project name. It doesn't work anymore with the wizard but I'm sure I did it for all projects in the past. – passsy Jan 26 '16 at 13:10
  • Well, if only your holo project has a small letter name, we'd have caught it sooner :) – RaGe Jan 26 '16 at 13:24
  • 1
    Also apparently, only one level of sub-directories is allowed too`:MyApplication2:mylibrary`will work but not `:Subdirectory:MyApplication2:mylibrary`. You can reproduce with https://github.com/martinbonnin/TestInstantRun. It would be great to have some explanation as to why we have these limitations but I'll mark the question as answered. Thanks all for your help. – mbonnin Jan 27 '16 at 20:02
  • @mbonnin I just ran into the same issue with Android Studio/Gradle plug 2.0 stable. I have a library project in the form `:libraries:MyLibrary:lib`. Is this what's causing the issue? Are you aware of a related issue already on http://b.android.com? – ashughes Apr 07 '16 at 23:29
  • @ashughes can you try removing the `libraries` directory and have `MyLibrary` at the root of your project ? I had to do that. – mbonnin Apr 07 '16 at 23:37
  • @mbonnin :-/ I would prefer *not* to do that. I found the related issue: http://b.android.com/197604 – ashughes Apr 07 '16 at 23:39
  • 5
    Er, please use who's answer? – 8bittree Sep 20 '16 at 15:38
2

According to official documentation on instant run.

What happened behind the scenes is that we have updated your project’s build.Gradle file to use the latest version of the Android Gradle plug-in, which is required for Instant Run to work. We also update your Gradle wrapper version to 2.8, and attempt to update the build tools version in all your modules to the latest (23.0.2). This isn't required for Instant Run, but it will use a new faster version of dex, which helps both instant run and a full build be a bit faster.

A Snippet of Application\build.gradle is shown below:

buildscript {
   repositories {
       jcenter()
   }

   dependencies {
       classpath 'com.android.tools.build:gradle:2.0.0-alpha1'
   }
}

Known Issues Using Instant Run

Using Instant Run with Reflection

Reflection could show unexpected things, for example:

  • Classes are all made public
  • Many other things are also made public

Limitations with Performance Profiling

We suggest temporarily disabling Instant Run while profiling your debug application.

There is a very small performance impact when using Instant Run, and a slightly larger impact when methods are overridden.

Increases in App Methods

Instant Run adds some methods–140 plus three times the number of classes in your app and its local dependencies. If the app was previously just below the dex limit, enabling Instant Run may push your app over the dex limit. Learn how to fix this by Optimizing Multi dex Development Builds.

Other Known Issues

  • Intermittent issues may occur where the IDE loses connection with the app which will trigger a full rebuild.
  • Third party Gradle plugin compatibility has not yet been tested, especially those that have not been updated to use the new transforms API.
  • Data-binding is currently broken in this build (capability to be restored).

so if you are facing this issue then you can turn off you instant run

go to Settings → Build, Execution, Deployment → Instant Run and uncheck Enable Instant Run… .

Better understanding of instant run go here

vishal jangid
  • 2,967
  • 16
  • 22
  • 3
    It's not about instant run. It's more about not being able to update to `com.android.tools.build:gradle:2.0.0-alpha6` because a submodule includes a complete project with top level `build.gradle` and multiple modules (lib and sample). Including the lib with compile project(':otherproject:lib') fails because the new gradle build plugin reads the top level build.gradle of otherproject. The classpath will set and cauess the error "Cannot change dependencies of configuration..." -> unable to build. Disabling instant run doesn't solve the problem – passsy Jan 19 '16 at 16:31
  • @passsy try to rebuild your complete project after turn off instant run. – vishal jangid Jan 19 '16 at 16:41
2

Take your dependencies out of your top level build gradle. As it is you are creating a classpath with your top level gradle and then attempting to overwrite it with your other build.gradles

From:

buildscript {
repositories {
    mavenCentral()
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.0.0-alpha6'
    classpath 'com.novoda:bintray-release:0.2.7'
    classpath 'io.fabric.tools:gradle:1.+'
}}

To: Note I did not add that commented line, Android-Studio does this automatically

buildscript {
    repositories {
        jcenter()
    }   
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha6'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

You should be able to add any needed Maven repositories into your separate app gradles, as they should be specific and the jcenter would cover many of these, as @AndroidMechanic, and @Hi I'm Frogatto have been trying to say in previous answers and comments. Have a look at read here Bintray - JCenter

The other thing is, I do not understand why you are managing your libraries build gradle within your project as part of your project. You should be referencing your library from your project, with the app build.gradle. You are treating the library gradle as the app gradle.

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.android.support:appcompat-v7:23.1.0'

    testCompile 'junit:junit:4.12'

}

Make these changes, then see what duplicates and you can manage that from there.

Also, I recommend manually syncing project with gradle files when changes are made. I would not rely on instant anything, it's important to make changes step wise and take stock of what's occurring, particularly when it won't compile. That's my opinion only and one way to program in android.

If instant run creates havoc with a particular project, I would disable it for that project. It is enabled by default and I've had no issues with it. The build mess may be the result of unclear gradles in your project to begin with.

Also:

In gradle wrapper properties, grade 2.10 is required for classpath 'com.android.tools.build:gradle:2.0.0-alpha6':

distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

See here for latest updates Android Tools Project Site

Or you can install a previous version of Android Studio and use the previous working version of your project.

If you have multiple git files, I suggest you remove the redundant ones, keep only the ones you are using for version control.

Community
  • 1
  • 1
  • This does not address the problem raised by either the original poster or passsy. The problem is that they tried to include a whole android project with a root and a lib module under a different android project. This results in two "root level" build.gradle files. I concede that this is a silly way to organize projects, but it clearly worked for them before upgrading to alpha6. Hence the question. All the things about repos and dependencies are good advice, but irrelevant to the issue at hand. – RaGe Jan 23 '16 at 18:16
  • Also, dependencies in the buildscript section are perfectly acceptable, just usually not applicable. In this case the dependencies are clearly gradle libraries for the buildscript itself (bintray release for example) and not android code dependencies. So they are placed correctly in the buildscript section and not app dependencies in the question, – RaGe Jan 23 '16 at 18:18
  • @ReGe correctly analyzed why this answer does not solve this problem. The structure may be "wrong" but it's used in many projects. Even with the "wrong" structure I should be able to include a folder containing a module without triggering the build.gradle of the parent folder. – passsy Jan 25 '16 at 14:34
0

classpath 'com.android.tools.build:gradle:2.0.0-alpha1'

try to change it to

 classpath 'com.android.tools.build:gradle:2.0.0-alpha6'

alpha1 seems obsolete since today (?) and is not compiling any more. Also you'll have to upgrade your gradle to latest 2.10 to work with alpha6

0

Two things you can try

Change your plugin for "android"

With the new gradle tools you need to specify the correct plugin for your module gradle file as well as your library gradle file. If you look closely, your library gradle file is correct:'

apply plugin: 'com.android.library'

Change your module gradle plugin:

apply plugin: "android" -> apply plugin: 'com.android.application'

org.apache classes are now depcrated

This could also be a possible reason as to why your application isn't compiling anymore. Remove this:

useLibrary 'org.apache.http.legacy'

See Deprecated List.

TejjD
  • 2,571
  • 1
  • 17
  • 37
0

The library project's build.gradle seems to cause the configuration error (because of some obscure reason). For me it was enough to also add the library project (which is a git submodule) to settings.gradle instead of only adding the library's project module.

Instead of: include ':libraries:my_library:sdk'

try including both the library subproject and the subproject's module: include ':libraries:my_library' include ':libraries:my_library:sdk'

sfera
  • 1,138
  • 1
  • 15
  • 21