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.

Community
  • 1
  • 1
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
  • 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? – RaGe Jan 23 '16 at 06:52
  • Can you show your project folder structure please – RaGe Jan 23 '16 at 06:58
  • yes. there is one in the app build file. please see edit. – Ray Tayek Jan 23 '16 at 07:02
  • 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. – Ray Tayek Jan 23 '16 at 07:10
  • Did you add javalib to settings.gradle? – RaGe Jan 23 '16 at 08:12
  • yes, settings,gradle in root/ has: include ':app' include ':javalib' – Ray Tayek Jan 23 '16 at 09:37
  • You already have a compile javalib in the app build.gradle, why repeat it in the root build.gradle? – RaGe Jan 23 '16 at 12:50
  • check this - https://stackoverflow.com/questions/44777525/how-to-solve-could-not-find-method-compile-for-arguments-file-collection/68421448#68421448 – Derrick Jul 19 '21 at 13:46

5 Answers5

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.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90
RaGe
  • 22,696
  • 11
  • 72
  • 104
  • i do have that compile in the app build file. removing it does not help. – Ray Tayek Jan 23 '16 at 22:46
  • 1
    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? – Ray Tayek Jan 23 '16 at 22:57
  • can you show your settings.gradle please? Configure [doc is here](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:configure(java.lang.Iterable, groovy.lang.Closure)) – RaGe Jan 23 '16 at 23:43
  • setting.gradle in root/ is: include ':app' include ':javalib', settings.gradle in javalib/ is: rootProject.name = 'javalib'. thanks for the doc, – Ray Tayek Jan 24 '16 at 03:40
  • there is no `:` in project names in settings.gradle. See examples here: https://docs.gradle.org/current/userguide/multi_project_builds.html – RaGe Jan 24 '16 at 06:26
  • yes, there is no ':' in the doc. but generating a new app with android studio puts: include ':app' in the setting.gradle file. – Ray Tayek Jan 24 '16 at 06:39
  • Can you try deleting settings.gradle in javalib/ ? – RaGe Jan 24 '16 at 06:48
  • removing the ':'s in settings.gradle does not help. – Ray Tayek Jan 24 '16 at 06:51
  • 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(){} – Ray Tayek Jan 24 '16 at 06:53
  • using comfigure(){} it says: Error:(27, 0) Project with path ':javalib' could not be found in root project 'AndroidMain'. (with or wothout the ':'s – Ray Tayek Jan 24 '16 at 06:54
  • I meant comment out the settings.gradle in the javalib folder, sounds like you commented out the root settings.gradle ? – RaGe Jan 24 '16 at 06:57
  • 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()' – Ray Tayek Jan 24 '16 at 07:13
  • 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. – RaGe Jan 24 '16 at 07:27
  • see pull request I submitted to your github project – RaGe Jan 25 '16 at 15:46
  • @etech Can you post your issue as a new question please – RaGe Apr 20 '16 at 21:51
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

Vrushali Raut
  • 1,130
  • 1
  • 10
  • 20
Mahdi
  • 11
  • 4
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.

Community
  • 1
  • 1
TejjD
  • 2,571
  • 1
  • 17
  • 37
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
}
0

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

compile project(path: ':MyLib')
Ari
  • 1
  • 2