Gradle「Could not find method compile() for arguments」の原因と直し方
Could not find method compile() は、compile を implementation に書き換えれば直る。
compile と runtime の設定は Gradle 7 で削除されたので、古い build.gradle をそのまま使うと出る。
公開: 更新:
gradle build … * What went wrong: A problem occurred evaluating root project 'app'. > Could not find method compile() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
gradle build … BUILD SUCCESSFUL in 852ms 1 actionable task: 1 executed Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.8.2/userguide/configuration_cache_enabling.html
2026-10-31 に gradle:jdk21(Python 3.14.4 / Git 2.53.0) で実際に打って取った出力。検証環境の詳細
要約
古い手順書のとおりに dependencies を書くと、ビルドが設定の読み込みで止まる。
Gradle 9.8.1 で compile 'org.apache.commons:commons-lang3:3.17.0' と書いた build.gradle を実行すると、次のように出た。
FAILURE: Build failed with an exception.
* Where:
Build file '/home/user/app/build.gradle' line: 8
* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method compile() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.Could not find method compile() for arguments は、compile という依存の設定(configuration)が存在しないという意味だ。
Groovy DSL では dependencies の中の compile '…' がメソッド呼び出しとして解釈されるので、設定が無いと「メソッドが見つからない」という形のエラーになる。line: の行番号が、書き換える行を指している。
Upgrading from Gradle 6.x to 7.0(新しいタブで開く) にあるとおり、compile と runtime の設定は Gradle 7 で削除された。
代わりの implementation と api は Gradle 3.4 からある。
実行例
gradle:jdk21 のコンテナ(Gradle 9.8.2)で compile を書いた build.gradle をビルドすると、Could not find method compile() で終了コード 1 になる。implementation と testImplementation に書き換えた版は、BUILD SUCCESSFUL で終了コード 0 になる。
$ gradle --version
------------------------------------------------------------
Gradle 9.8.2
------------------------------------------------------------
Build time: 2026-10-19 16:29:26 UTC
Revision: 8de7bcf76e785507ad92f0741ab9235a430c95e6
Kotlin: 2.4.10
Groovy: 4.0.33
Ant: Apache Ant(TM) version 1.10.17 compiled on April 6 2026
Launcher JVM: 21.0.13 (Eclipse Adoptium 21.0.13+9-LTS)
Daemon JVM: /opt/java/openjdk (no Daemon JVM specified, using current Java home)
OS: Linux 6.6.87.2-microsoft-standard-WSL2 amd64$ gradle build
Starting a Gradle Daemon (subsequent builds will be faster)
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/tmp.39bzByYPTd/build.gradle' line: 10
* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method compile() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
BUILD FAILED in 4s
$ echo $?
1$ gradle build
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/tmp.39bzByYPTd/build.gradle' line: 10
* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method runtime() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
BUILD FAILED in 493ms
$ echo $?
1$ gradle build
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/tmp.39bzByYPTd/build.gradle' line: 6
* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method implementation() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
BUILD FAILED in 464ms
$ echo $?
1$ gradle build
FAILURE: Build failed with an exception.
* Where:
Build file '/tmp/tmp.39bzByYPTd/build.gradle' line: 10
* What went wrong:
A problem occurred evaluating root project 'app'.
> Could not find method api() for arguments [org.apache.commons:commons-lang3:3.17.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights from a Build Scan (powered by Develocity).
> Get more help at https://help.gradle.org.
BUILD FAILED in 462ms
$ echo $?
1$ gradle build
> Task :compileJava NO-SOURCE
> Task :processResources NO-SOURCE
> Task :classes UP-TO-DATE
> Task :jar
> Task :assemble
> Task :compileTestJava NO-SOURCE
> Task :processTestResources NO-SOURCE
> Task :testClasses UP-TO-DATE
> Task :test NO-SOURCE
> Task :check UP-TO-DATE
> Task :build
BUILD SUCCESSFUL in 852ms
1 actionable task: 1 executed
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.8.2/userguide/configuration_cache_enabling.html
$ echo $?
0— 2026-10-31 時点の出力
検証環境Python 3.14.4 / Git 2.53.0 / 2026-10-31 検証
- 検証日
- 実行環境
gradle:jdk21Ubuntu 26.04.1 LTS- バージョン
- Python 3.14.4
- Git 2.53.0
この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。
解決策
1. 新しい設定名に書き換える
Gradle の移行ガイドにある対応表どおりに書き換える。
| 削除された設定 | 書き換え先 |
|---|---|
compile | implementation(ライブラリの API に出るものは api) |
runtime | runtimeOnly |
testCompile | testImplementation |
testRuntime | testRuntimeOnly |
plugins {
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.commons:commons-lang3:3.17.0'
testImplementation 'junit:junit:4.13.2'
}Gradle 9.8.1 でこの build.gradle に書き換えると、gradle build が通り、compileClasspath に commons-lang3:3.17.0 が入った。
2. プラグインを適用する
implementation が見つからないなら、plugins ブロックに java を足す。
ほかのプロジェクトから使われるライブラリで、依存を利用側のコンパイルにも見せたいときは java-library を適用して api を使う(The Java Library Plugin(新しいタブで開く))。
plugins {
id 'java-library'
}
dependencies {
api 'org.apache.commons:commons-lang3:3.17.0'
}3. Kotlin DSL も同じ名前に直す
build.gradle.kts でも、設定の名前は同じだ。
dependencies {
implementation("org.apache.commons:commons-lang3:3.17.0")
testImplementation("junit:junit:4.13.2")
}よくある原因
- Gradle 6 以前の書き方が残っている。
compile・testCompile・runtime・testRuntimeは、どれも Gradle 7 で消えた。
Gradle 9.8.1 ではruntimeもCould not find method runtime()になった。 javaプラグインを適用していない。implementationやtestImplementationは、javaなどのプラグインが追加する設定である。
プラグインの無いbuild.gradleにimplementationを書くと、Could not find method implementation()になった。apiをjavaプラグインで使っている。apiはjava-libraryプラグインにしか無い。javaプラグインのままapiを書くとCould not find method api()になり、java-libraryに変えると通った。- Kotlin DSL で古い書き方をしている。
build.gradle.ktsでは同じ原因が、スクリプトのコンパイルエラーUnresolved reference 'compile'として出る。