2016-01-23 76 views
9

我有一個hello world全屏android studio 1.5.1應用程序,我添加了一個gradle/eclipse-mars子項目。沒有其他文件被修改,除了在settings.gradle中添加':javalib'。加入project lib dependencyGradle無法找到方法編譯()的參數

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

在命令行根構建構建文件並運行gradle這個,得到:

  • 其中: 構建文件「d:\ AndroidStudioProjects \ AndroidMain \的build.gradle 'line:23

  • 出錯了: 評估根項目「AndroidMain」時出現問題。

    Could not find method compile() for arguments [project ':javalib'] on org.grad le.api.interna[email protected]46 3ca82f.

adding the java plugin到根構建文件並沒有幫助。我不認爲它在wrong place

根項目和添加的子項目都有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: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 
} 

和應用程序構建文件是:

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

編輯:這個練習的整個目的是將核心java代碼放入android項目中。目前核心代碼是獨立的gradle/eclispe項目。我在Android項目中有一個批處理,它執行gradle -p standaloneprojectdir/jar並將jar複製到libs /中。

我只是雖然會有一個更簡單的方法,而不是發佈該jar,並從存儲庫中獲取它,因爲這一切都在我的電腦上完成。

這將是不錯的所有代碼直播和只是做一個構建:(

編輯:按RaGe的建議,這裏是一個virgin android studio projectvirgin gradle/eclipse project沒有編輯已經做了這些文件。你應該選擇接受它,是讓android應用程序輕鬆訪問java項目類(即新的Library();在MainActivity.onCreate()中將編譯並運行)。我不關心java項目在哪裏理想情況下,兩個來源都將是live in both。

試圖this也失敗。說:>找不到:virginjavaproject,但目錄確實存在。

編輯:實際工作的是RaGe對virgin android project的拉動請求。

+0

Java插件是不適合的Android項目,但是你應該有Android插件的地方,這增加編譯配置。您是否使用Android Studio創建項目? – RaGe

+0

你可以顯示你的項目文件夾結構請 – RaGe

+0

是的。應用程序構建文件中有一個。請參閱編輯。 –

回答

3

你已經有

compile project(':javalib') 

:app項目,你不必也從根的build.gradle注入的依賴。如果你仍然想從根build.gradle做,做正確的做法是:

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

virgin android studio頭是什麼工作。

+0

我確實在應用程序構建文件中進行了編譯。刪除它並沒有幫助。 –

+1

從應用程序構建文件中刪除編譯,並且用根編譯文件中的configure(){...}將我的項目(){...}替換爲編譯行仍然失敗。配置的文檔在哪裏? –

+0

你可以顯示你的settings.gradle請嗎?配置[doc在這裏](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:configure(java.lang.Iterable,groovy.lang .closure)) – RaGe

0

如前所述通過@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.

也看看這個:Could not find method compile() for arguments Gradle

+1

依賴項不在buildscript中。 –

0

簡單地粘貼上的build.gradle(項目: 「你的名字prject」)

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

我有這個問題,但我自己的圖書館。正確編譯的方法是:

compile project(path: ':MyLib') 
1

我們有2個名爲「build.gradle」的文件。 確保你在的build.gradle文件裏面複製你的「編譯代碼」 「應用程序」文件夾

相關問題