Android Studio and Gradle - build fails - android

I am building a small library project along wit a sample project to illustrate the use. I can't manage to run the sample in Android Studio. I have created the project from scratch. I am experienced with Eclipse but it's my first try at Android Studio & Gradle.
The error given:
Gradle: Execution failed for task ':demo:dexDebug'.
Running C:\DevTools\Android\android-studio\sdk\build-tools\android-4.2.2\dx.bat
failed. See output
I have the following folder structure:
- demo
- build
- libs
- android-support-v4.jar
- src
- main
- java
- res
- build.gradle
- library
- build
- libs
- android-support-v4.jar
- src
- main
- java
- res
- build.gradle
- build.gradle
- settings.gradle
Build.gradle at project root:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
Settings.gradle at project root:
include ':library', ':demo'
Build.gradle for the library module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Build.gradle for the sample module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile project(':library')
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}

Specifying compile files('libs/android-support-v4.jar') means that every library includes support v4. What you want to do is just specify that every library depends on it:
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
This will allow gradle to detect all dependencies and include this only once.
Note: You have to first use the SDK Manager and download and install two Maven repositories: "Android Support Repository" and "Google Repository".

I found the problem:
I removed that line from the sample gradle file.
compile files('libs/android-support-v4.jar')
However, I have no idea why this does not work (if I have 2 or 3 external libraries that all depend on the support library, how are we supposed to do, without touching their gradle files?

You should navigate to your libs folder in the IDE, right click on the jar and select to add the library to the project, it still needs to establish the dependency even though the jar appears to be there. Also look at your gradle built script to make sure the dependency appears there. If that still doesnt work just run a gradle clean on the project. Intellij documentation will give you more details on what clean does. see:
stackoverflow gradle build

This error could be encountered while migrating from Groovy to kotlin DSL as well and here are the steps to get rid of it:
If you are still in the process of migrating please complete the migration of gradle files first, use kts syntax and then sync gradle files.
Use this dependency inside your build.gradle(app level):
implementation("androidx.legacy:legacy-support-v4:1.0.0")
Remove id("kotlin-android-extensions") from plugins block inside build.gradle.kts (app level).
That's it! 3rd Point solved the issue for me but trying all the points should definitely fix the issue.

In my Case replace this line
classpath "com.android.tools.build:gradle:7.0.2"

Related

Cannot change dependencies of configuration (after enabling instant run)

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'
}
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')
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.
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.
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
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.
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
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.
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'

Android gradle build Error:(9, 0) Gradle DSL method not found: 'compile()'.

I am getting the following build error when I try and sync my project:
Error:(9, 0) Gradle DSL method not found: 'compile()'
Possible causes:The project 'AlexTest' may be using a version of Gradle that does not contain the method.
The build file may be missing a Gradle plugin.
link: Apply Gradle plugin
I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.
Here is the build.gradle file for AlexTest (the project directory):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
compile 'com.google.android.gms:play-services:6.1.11'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
I think that was the gradle file it was having trouble with. But I'm not sure what method it is referring to.
Also here is the gradle-wrapper.properties which it also referred to:
#Mon Nov 10 01:06:12 PST 2014
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.1-all.zip
perhaps the gradle version in the distributionUrl needs to match the one in the dependency?
I also have a build.gradle file in the app directory itself - 1 level lower, though I don't think that is what it was referring to, but here it is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.snappiesticker.alextest"
minSdkVersion 16
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:6.1.+'
}
I have tried applying every single gradle plugin they link me to in that link on the bottom, yet same issue, so I conclude that the first error is the cause.
Correct.
Here is the build.gradle file for AlexTest (the project directory):
You will notice that this file contains a code comment:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Remove the compile 'com.google.android.gms:play-services:6.1.11' line from that file. Leave the compile 'com.google.android.gms:play-services:6.1.+' that you have in the other build.gradle file.
The dependencies closure in buildscript is for Gradle plugins only. The top-level dependencies closure, found in the module's build.gradle file, is for application dependencies.
Saw reports that the problem occurred for other reasons, but this solved for me.
If you carry out any changes in the Project Structure (Android Studio) and press OK, Gradle will be synchronized, but this type of synchronization, if the dependency block has something like:
This block will stay this way after synchronization:
One way to solve the problem is to insert a line break to stay as it was before the synchronization or put a semi-colon between the two statements.
I hope it helps.
Just add foolwoing statement in your dependencies
apply plugin: 'jetty'
Hi everyone for me it was a "couple days consuming job" to make my app run in Android Studio (I migrated from Eclipse and had this problem too ) . Finally I found that very simple way of it .
Create libs folder under src/main/java/ it is App/java/libs in left pane .
Copy and paste all your external jars into here.
Goto left pane and right click on your App then click Open Module Settings
Then Project Structure window will appear .
Then move to Dependencies tab .
Final Step : Add all your jars located in App/java/libs (You will find them in src/main/java/libs) one by one .
That is all Enjoy it.
Declare dependencies in Module's build.gradle file, not in AlexTest's build.gradle file

Add Android Better Pickers as a gradle dependency

I first added the android support library and tested that I could use it. However when I add Android Better Pickers in this maven repository I get the following error:
Gradle 'bumble' project refresh failed: Could not find
com.google.android:support-v4:18. Required by: myapp:app:unspecified
com.doomonafireball.betterpickers:library:1.4.2
This is how I set up my dependencies in the build.gradle located in MyProject -> app.
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.doomonafireball.betterpickers:library:1.4.2'
}
Android Better Pickers has the following in it's build.gradle and is packaged as an aar.
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.nineoldandroids:library:2.4.0'
}
Anyone know of a solution?
EDIT
Android better pickers now has gradle support since v 1.5! Now it is really easy adding it as a library, just follow there guide and don't forget to do a clean AND gradle sync after you change your build.gradle. Parts of the answers to this question still applies for none gradle projects I however.
First Make sure your are pointing to right sdk in File >Project Structure >Android SDK
In order to use Support Jar you have to install Android Support Repository from SDK Manager. SDK manager icon is available in Android Studio tool bar.
Things you should know for knowledge :
1.There is no need to add any dependency in your main module, if that is already added in any one of your library module already. So remove support dependency from your main module.
Make it something like this :
dependencies {
compile 'com.doomonafireball.betterpickers:library:1.4.2'
}
2.There is some issue going on in dependency management in android studio (0.4.2) which is fixed for Android Studio (0.4.3) but till the release check this as well For any dependency related issues.
Import Google Play Services library in Android Studio
EDIT :
I have checked the github repository there is no gradle dependency for date picker.
So do the following
Download Repository from github
Copy the library directory inside root of your project or make a directory and keep all your libraries inside that.The below configuration is for direct in root and I have renamed "library "to datepickerlibrary
Modify the build.gradle comes with library
build.gradle inside data picker library module
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion '19.0.0'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
apply plugin: 'maven'
apply plugin: 'signing'
version = "1.4.0"
isReleaseVersion = !version.endsWith("SNAPSHOT")
group = "com.doomonafireball.betterpickers"
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.nineoldandroids:library:2.4.0'
}
Dependency in your main module's build.gradle should be like :
dependencies {
compile project(':datepickerlibrary') //if it is inside some sub directory you can give path like ':libraries:datepickerlibrary' depends on you
}
Add this line inside settings.gradle which is located in root of your Project directory:
include ':datepickerlibrary'
After these all checks, Do sync your project with gradle.
It worked for me, let me know if any issue comes.
ORIGINAL ASKER'S NOTES
I ended up doing something similar to the above and it worked great! What I did as a summary:
Downloaded and manually added the library as a dependency in Android Studio
Removed the following from the library's build.gradle (got a sonytype class not found exception or similar)
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
repository(url: sonatypeRepo) {
authentication(userName: sonatypeUsername,
password: sonatypePassword)
}
Updated the library's SDK version to match the one I had installed.

dependencies tab not appear

I tried to add the support library v4 in a project with android studio, I read how to add the support library
Add support library to Android Studio project
but the tab dependencies not appear. I click on the (only) module of my project, but do not appear. I tried with a blank project nothing appear.
EDIT
Project structure dialog
project
EDIT 2
The build.grade file as generated by Android Studio
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 16
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
}
dependencies {
}
First I recommend you to upgrade the AS to 0.4.0, lot of bugs are fixed in this.
Then check for the dependencies in build.gradle of your MyApplication module for support library.
dependencies {
compile 'com.android.support:support-v4:18.0.+'
}
If not added add the mentioned dependency.
After adding the line sync project with Gradle Files using the option available in toolbar.
Make sure you are not getting any error. If sync happens successfully check the Module again.
You should see screen like this, In my case support v7 also added, you can ignore that.

How do I use a library with my Android project using Gradle?

I'm only just starting with Android development so I'm sure this is incredibly basic and a dumb question, but I'm getting the following error when I try and include a library to use in my code with Gradle:
Gradle 'MyApp' project refresh failed: You are using an old, unsupported version of Gradle. Please use version 1.6 or greater.
The library I'm trying to use is ION. All I've tried so far is adding the following lines into the build.gradle file.
dependencies {
compile 'com.koushikdutta.ion:ion:1.1.5'
}
Which I guessed at based on this section of Koush's GitHub account, but what else do I need to do? Still download the jar and put it somewhere? I have looked at other questions/searched for guides on the basics of Gradle, but bizarrely I can't find a solid, definitive answer.
And how do I upgrade Gradle? Can it be done from within Android studio? I'm using Android Studio 0.2.13 on Windows 7 64bit:
SOLUTION: - Make sure you're putting the dependencies in the correct build.gradle file! Doh! It should be in the one just above your src folder, not the root!
Make sure that your buildscript contains the classpath with "gradle:0.5.+". Afterwards, navigate to the root directory of your project (one directory level above the build.gradle file) and (in the command line) run "./gradlew clean"
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 18
}
sourceSets {
main {
manifest.srcFile 'MyApp\src\main\AndroidManifest.xml'
}
}
}
dependencies {
compile "com.koushikdutta.ion:ion:1.1.5"
}

Categories

Resources