Gradle Could not find method compile() for arguments
Asked Answered
S

5

14

i have a hello world full screen android studio 1.5.1 app that i added a gradle/eclipse-mars subproject to. no other files were modified except for adding include ':javalib' to settings.gradle. adding a project lib dependency:

project(':app') {
    dependencies {
        compile project(':javalib') // line 23
    }
}

to the root build build file and running gradle from the command line , gets:

  • Where: Build file 'D:\AndroidStudioProjects\AndroidMain\build.gradle' line: 23

  • What went wrong: A problem occurred evaluating root project 'AndroidMain'.

    Could not find method compile() for arguments [project ':javalib'] on org.grad le.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@46 3ca82f.

adding the java plugin to the root build file did not help.

i don't think it's in the wrong place.

both the root project and the added subproject have the gradle wrapper.

any pointers will be appreciated.

thanks

edit: for clarification, the root build file is:

// 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:1.5.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        jcenter()
    }
    task hello << { task -> println "I'm $task.project.name" }
}

project(':app') {
    dependencies {
        //compile project(':javalib') // causes problems
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

and the app build file is:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "acme.androidmain"
        minSdkVersion 22
        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'])
    //compile project(':javalib') // use :javalib:jar?
    //testCompile project(':javalib')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:support-v4:23.1.1'
}

edit: the whole purpose of this exercise is to get core java code into an android project. currently the core code is a standalone gradle/eclispe project. i have a batch in the android project that does a gradle -p standaloneprojectdir/ jar and copied the jar into libs/.

i just though there would be an easier way other than to publish the jar and get it from a repository since this all done on my pc.

it would be nice to have all the code live and just do a build :(

edit: as per RaGe's suggestion, here is a virgin android studio project and a virgin gradle/eclipse project. no editing has been done to these files. you mission should you choose to accept it is to get the android app to easily access the java project classes (i.e. new Library(); in MainActivity.onCreate() will compile and run). i don't care where the java project lives. ideally, both sources would be live in both ide's.

atempting this fails also. says: > Could not find :virginjavaproject, but directory does exist.

edit: what actually worked was RaGe's pull request to the virgin android project.

Suellen answered 23/1, 2016 at 2:16 Comment(8)
Java plugin is not appropriate for an android project, but you should have the Android plugin somewhere, which adds the compile configuration. Did you create the project using Android studio?Trug
Can you show your project folder structure pleaseTrug
yes. there is one in the app build file. please see edit.Suellen
root/ has app/ and javalib/ - root and app were created by android studio, javalib is a clone of an standalone gradle/eclipse project that i added by hand.Suellen
Did you add javalib to settings.gradle?Trug
yes, settings,gradle in root/ has: include ':app' include ':javalib'Suellen
You already have a compile javalib in the app build.gradle, why repeat it in the root build.gradle?Trug
check this - #44778025Stpeter
T
4

You already have

compile project(':javalib') 

in your :app project, you don't have to also inject the dependency from your root build.gradle. If you still want to do it from the root build.gradle, the correct way to do it is:

configure(':app') {
    dependencies {
        compile project(':javalib') // causes problems - NOT ANYMORE!
    }
}

the virgin android studio head is what worked.

Trug answered 23/1, 2016 at 15:19 Comment(15)
i do have that compile in the app build file. removing it does not help.Suellen
removing the compile from he app build file, and replacing my project() { ...} with your configure() { ...} in the root build file still fails with the compile line. where is the doc for the configure?Suellen
can you show your settings.gradle please? Configure [doc is here](docs.gradle.org/current/dsl/…, groovy.lang.Closure))Trug
setting.gradle in root/ is: include ':app' include ':javalib', settings.gradle in javalib/ is: rootProject.name = 'javalib'. thanks for the doc,Suellen
there is no : in project names in settings.gradle. See examples here: docs.gradle.org/current/userguide/multi_project_builds.htmlTrug
yes, there is no ':' in the doc. but generating a new app with android studio puts: include ':app' in the setting.gradle file.Suellen
Can you try deleting settings.gradle in javalib/ ?Trug
removing the ':'s in settings.gradle does not help.Suellen
commenting out the code in settings.gradle gets: Error:(20, 0) Project with path ':app' could not be found in root project 'AndroidMain' using project(){}Suellen
using comfigure(){} it says: Error:(27, 0) Project with path ':javalib' could not be found in root project 'AndroidMain'. (with or wothout the ':'sSuellen
I meant comment out the settings.gradle in the javalib folder, sounds like you commented out the root settings.gradle ?Trug
yes, there's a coment in the file: To declare projects as part of a multi-project build use the 'include' method. both project(){} and configure() {} still get : Error:(27, 0) Gradle DSL method not found: 'compile()'Suellen
If you could upload a stripped down version of your project to github, I can try to help you out. Your code files aren't necessary, just the folder structure and all gradle files.Trug
see pull request I submitted to your github projectTrug
@etech Can you post your issue as a new question pleaseTrug
G
1

We have 2 files named "build.gradle". make sure you have copied your "compile code" in the build.gradle file that is inside "app" folder

Gratian answered 18/5, 2017 at 14:48 Comment(0)
C
0

As stated by @peter-niederwieser:

The build script is mixing up buildscript dependencies (i.e. dependencies of the build itself; typically this means Gradle plugins) with regular dependencies (i.e. dependencies of the code to be compiled/run). The latter need to go into dependencies { ... }, not into buildscript { dependencies { ... } }. Everything but the classpath dependencies are regular dependencies.

Also have a look at this: Could not find method compile() for arguments Gradle.

Chasse answered 23/1, 2016 at 5:13 Comment(1)
the dependency is not in the buildscript.Suellen
S
0

Simply paste this on build.gradle(Project:"Your prject name")

 // 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'

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

   allprojects {
repositories {
    jcenter()
   }
}

task clean(type: Delete) {
delete rootProject.buildDir
}
Salespeople answered 18/6, 2016 at 9:3 Comment(0)
G
0

I had this problem but with my own library. The way to correctly compile was:

compile project(path: ':MyLib')
Grandpa answered 21/2, 2017 at 2:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.