Gradle Could not find method compile() for arguments

荒凉一梦 提交于 2019-12-09 14:52:46

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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
}



回答5:


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

compile project(path: ':MyLib')


来源:https://stackoverflow.com/questions/34958886/gradle-could-not-find-method-compile-for-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!