I needed to add a android.support.v4.widget.NestedScrollView to my project, which requires the V4 support library according to this question.
In the answer it said . . .
You need to add this line in dependencies:
compile 'com.android.support:support-v4:23.4.0'
...I assumed that referred to the build.gradle file. I have two - a top-level one and a module one. Only my top-level one had a dependencies keyword, so I put it there. But when I did I got
Error:(8, 0) Gradle DSL method not found: 'compile()'
...which is addressed in this question here as well as this question where it indicates I need to move it to the build.gradle for my module. But the module build.gradle has a completely different structure so I don't know where it goes.
My top-level build.gradle looks like this:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
compile 'com.android.support:support-v4:23.4.0'
}
}
allprojects {
repositories {
jcenter()
}
}
And my module-level one looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 16
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.assemblyguide.remote"
minSdkVersion 16
targetSdkVersion 16
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
Where do I put the compile statement (or the dependencies clause it goes into) in the module build.gradle to avoid that error, and why do the two build.gradle files look so completely different?
Bonus question: I'm new to Android Studio and I didn't have to know this stuff for Eclipse. How much of a Gradle expert should I plan to be to use Android Studio and where can I get a basic understanding of Gradle without having to get a PhD in it?
I suggest you download the full Gradle distribution, which comes with the "User Guide" PDF. Read it from top to bottom, skipping chapters that are truly not relevant to you. You'll avoid having to ask many questions.
Direct link to Gradle User Manual pdf
In your case, you should remove the "compile" line you added in the "buildscript" block. You likely need to replace your "allprojects" block with the following:
allprojects {
repositories {
jcenter()
}
dependencies {
compile 'com.android.support:support-v4:23.4.0'
}
}
You should declare the the dependencies at the end of your main project module. Usually it is the app module. Find a build.gradle file there and follow the instructions shown here
It should look like this one
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
I found an open source project I wanted to work on but I'm having trouble setting up the initial configuration. The project seems to have been written in Eclipse and I'm trying to get it to work with Android Studio. After being through a number of errors, I'm finally stuck on the following error seen in the configuration menu before I run.
AndroidManifest.XML doesn't exist or has the incorrect root tag
I found a number of answers that suggest like this that suggest I use the sync project with Gradle command but my project wasn't setup with Gradle because I'm building on someone else's project. This is my first time using Android Studio so my following attempt to fix this might not be great. I decided to try to make the project a Gradle project by adding my own build.gradle and settings.gradle files. My layout looks like this:
top level:
inside java folder:
I tried to copy working example of the build and settings Gradle files. My settings.gradle contains the following:
include ':'
My top level build.gradle contains:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
//compile project(":")
}
}
allprojects {
repositories {
mavenCentral()
}
}
My java level build.gradle contains:
apply plugin: 'android'
android {
compileSdkVersion 20
buildToolsVersion "17.0.0"
defaultConfig {
applicationId "org.pocketworkstation.pckeyboard"
minSdkVersion 8
targetSdkVersion 19
versionCode 1037
versionName "v1.37"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
jni.srcDirs = ['jni']
}
androidTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
And because I think it might be important, my project structure modules:
Trying to synchronize at the moment doesn't generate any word so I assume it's okay but that's a big assumption. Any ideas what I have to change?
Android Studio doesn't consider your project to be a Gradle-based project; you can tell this because a number of the entries you're seeing in the Project Structure dialog don't show up for Gradle-based projects (i.e. the Project, Libraries, Facets, and Artifacts) entries. Close your project and re-import it as a Gradle-based project and you should be okay.
This is my first attempt at Android Studio. I installed 0.8.0 and updated to 0.8.2. As soon as a project is created I get the error message:
Error:(1, 0) Plugin with id 'com.android.application' not found
C:\Users\Bob\AndroidStudioProjects\HelloAgain6\app\build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.bob.helloagain6"
minSdkVersion 15
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'])
}
and C:\Users\Bob\AndroidStudioProjects\HelloAgain6\build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Updated Answer (Dec. 2, 2020)
Latest Gradle: 6.5
Version check:
./gradlew -v
How to update:
Set URL: ./gradlew wrapper --gradle-version=6.5 --distribution-type=all
Update: ./gradlew wrapper
Latest Android Gradle Plugin: 4.1.0
If you add the following code snippet to the top of your build.gradle file. Gradle will update the build tools.
buildscript {
repositories {
google() // For Gradle 4.0+
maven { url 'https://maven.google.com' } // For Gradle < 4.0
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
}
}
Read more here: https://developer.android.com/studio/build/index.html and about version compatibility here: https://developer.android.com/studio/releases/gradle-plugin.html#updating-gradle and https://dl.google.com/dl/android/maven2/index.html.
Original Answer
I had this same error, you need to make sure your Gradle version is compatible with your Android Gradle Plugin.
The latest version of Gradle is 2.0 but you need to use 1.12 in order to use the Android Gradle Plugin.
This can happen if you miss adding the Top-level build file.
Just add build.gradle to top level.
It should look like this
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.xx.y'
}
}
allprojects {
repositories {
mavenCentral()
}
}
In my case, I download the project from GitHub and the Gradle file was missing. So I just create a new project with success build. Then copy-paste the Gradle missing file. And re-build the project is working for me.
Root-gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:x.x.x'
}
}
allprojects {
repositories {
jcenter()
}
}
Gradle-wrapper.properties file:
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-x.x-all.zip
In the project level build.gradle file, I have replaced this line
classpath 'com.android.tools.build:gradle:3.6.3'
with this one
classpath 'com.google.gms:google-services:4.3.3'
After adding both of those lines, and syncing, everything became fine.
Hope this will help someone.
I am writing this not as a solution meant for many, but for some people who may commit a simple mistake like specifying the wrong url for importing projects from SVN. It is intended for those guys :)
This happened to me when I imported the project from SVN -> automatic prompt by Studio to open the project -> It asked for Gradle location -> D:\Software\Android\gradle-2.5 -> Then the error.
The same project in a different SVN branch works fine with the Gradle plugin and Gradle which I have configured in Studio. I tried changing Android Gradle plugin and Gradle to get it working on the erring branch without any success.
Finally, I figured out that it was my following mistake:
I tried importing a specific Application alone instead of importing the application along with dependent library projects.
The url which I used for import initially had the Application porject's name at the end. Once I removed it, and specified the parent directory which contained both application project and its dependent project, everything went smooth :)
I found the problem after one hour struggling with this error message:
I accidentally renamed the root build.gradle to filename in builde.gradle, so Android Studio didn't recognize it anymore.
Renaming it to build.gradle resolved the issue!
I still got the error
Could not find com.android.tools.build:gradle:3.0.0.
Problem: jcenter() did not have the required libs
Solution: add google() as repo
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.0.0"
}
}
I was using IntelliJ IDEA 13.1.5 and faced with the same problem after I changed versions of Picasso and Retrofit in dependencies in build.gradle file. I tried use many solutions, but without result.
Then I cloned my project from remote git (where I pushed it before changing versions of dependencies) and it worked! After that I just closed current project and imported old project from Gradle file to IntelliJ IDEA again and it worked too! So, I think it was strange bug in intersection of IDEA, Gradle and Android plugin. I hope this information can be useful for IDEA-users or anyone else.
Go to your grade file where you can see this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And change classpath to this:
buildscript {
repositories {
jcenter()
}
dependencies {
// classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'com.android.tools.build:gradle-experimental:0.7.0-alpha1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
The other answers didn't work for me, I guess something wrong happens between ButterKnife and 3.0.0 alpha5.
However, I found that when I annotated any one sentence, either BUtterKnife or 3.0.0 alpha5, it works normally.
So, you should just avoid the duplication or conflict.
For future reference: For me, this issue was solely caused by the fact that I wasn't running Android Studio as administrator. I had the shortcut on Windows configured to always run as administrator, but after reinstalling Android Studio, the shortcut was replaced, and so it ran without administrator rights. This caused a lot of opaque errors, including the one in this question.
This issue happened when I accidently renamed the line
apply plugin: 'com.android.application'
on file app/build.gradle to some other name. So, I fixed it by changing it to what it was.
[FOR FLUTTER] go to your build Gradle then check if you have three paths
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.google.gms:google-services:4.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
I somehow removed the android tools classpath and was getting the error.
This just happened to me using Android Studio 1.3.2, however, since I had just created the project, I deleted it and created it again.
It seems that it had not been properly created by Android Studio the first time, not even the project folders where as expected.
If you run a the module task with android plugin 1.2.3 in module directory , the problem appears. Try this:
../gradlew -b ../build.gradle -c ../settings.gradle :module:xxx
Make sure your two build.gradle and settings.gradle files are in the correct directories as stated in https://developer.android.com/studio/build/index.html
Then open "as existing project" in Visual Studio
Gradle is very finicky about this.
I got this error message after making the following change in my top-level build.gradle to update to the latest version of gradle:
//classpath 'com.android.tools.build:gradle:2.3.2' old
classpath 'com.android.tools.build:gradle:2.3.3' //new
I foolishly made the change while I was connected behind a hostile workplace proxy. The proxy caused the .jar files for the new version of gradle to become corrupt. This can be verified by inspecting the jars to see if they are an unusual size or whether they can be unzipped.
In order to fix the mistake, I connected to my network at home (which is not behind a proxy) and did a refresh dependencies from the Terminal:
./gradlew --refresh-dependencies
This caused the newer version of gradle to be re-downloaded and the error no longer occurs.
Check the spelling, mine was 'com.android.aplication'
This may also happen when you have both settings.gradle and settings.gradle.kts files are present in project root directory (possibly with the same module included). You should only have one of these files.
i had similar problem and i did following things to resolve it.
i referred to https://developer.android.com/studio/build
and copy / pasted these following lines before apply plugin lines
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
}
}
module app build.gradle file
apply plugin: 'com.android.application'
model{
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig.with {
applicationId "com.iamsafe"
minSdkVersion 15
targetSdkVersion 23
}
buildTypes {
debug {
minifyEnabled = false
useProguard = true
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:23.0.2'
compile files('libs/asmack-android-8-0.8.10.jar')
compile files('libs/commons-io-2.0.1.jar')
compile files('libs/httpclient-osgi-4.2.1-sources.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpmime-4.1.2.jar')
}
project build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.10'
}
}
allprojects {
repositories {
jcenter()
}
}
In this case of issues check below code
dependencies {
classpath 'com.android.tools.build:gradle:**1.5.0**'
}
and gradle-wrapper.properties inside your project directory check below disctributionUrl:
distributionUrl=https\://services.gradle.org/distributions/gradle-2.9-all.zip
If these are not compatible with each other then you end up in this issue.
For com.android.tools.build:gradle:1.5. you need a version at least 2.8 but if you switch to a higher version like com.android.tools.build:gradle:2.1.0 then you need to update your gradle to 2.9 and above this can be done by changing distributionUrl in gradle-wrapper.properties to 2.9 or higher as below
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
If you work on Windows , you must start Android Studio name by Administrator.
It solved my problem
Just make sure you put the http and https proxy correctly when you create the app
I am trying to migrate my android app from eclipse to Android studio. (0.5.4)
The project has several dependencies. (Sherlock etc)
I exported the app to Gradle and imported it in Android studio and managed to get the project to build successfully.
It appears however that only the dependencies are built.
Adding erronous lines in the app code does not trigger compile errors.
When I view project | packages, the package for my app does not show, Only the external libraries are shown.
My root level build.gradle only contains repositories and dependencies:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
I tried manually adding an android section with the intent to make gradle look at the source files but I had problems adding the section:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
As i now get the following error
Could not find method android() for arguments xxx on root project 'zzz'.
If proceeded to add
apply plugin: 'android'
Just before the android section, but now I get
A problem occurred evaluating root project 'zzz'.
Plugin with id 'android' not found.
Can anyone shed some light as to why i cannot have an Android section at the root level.
If the problem lies somewhere else any help would also be much appreciated.
In case it is needed, here is my settings.gradle
include ':external:PullToRefresh:SmoothProgressBarLib'
include ':external:ActionBarSherlock'
include ':'
include ':external:pulltorefresh-abs'
include ':external:MyAwesomeLibrary'
include ':external:PullToRefresh:pulltorefresh'
include ':external:SherlockNavigationDrawer'
include ':external:sdk:MyAwesomeSDK'
You've included an android block inside a buildscript block in your top-level build file, but this is incorrect. Instead it should be structured like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "19.0.2"
}
dependencies {
//Your app dependencies go here
}
All this is assuming that you truly have an Android application module at your project root (meaning that at your project root directory there's a src directory that has Android sources in it). It seems to be that you're trying to set it up this way because you also have this in your settings.gradle file:
include ':'
If that's the case, then rearranging your top-level build file as indicated above should fix it.
If you don't have a module at the project root, then you should restore the top-level build file to its original condition (take out apply plugin and android), take out that include ':' line from settings.gradle, and add an include statement that points to your application module.
In your settings.gradle file, I don't see where you included your main module. You should add:
include ':mainmodule-directory'
Your project should, ideally, have two build.gradle files. One at the root level, one at the module level. In your main module, you specify that the module is an Android module by adding apply plugin: android in the module's build.gradle file. Then you specify that the entire project would need the Android plugin by using the following in the root build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}