2

I am new to writing gradle tasks.

I have added a .gradle file into root of my project in this gradle file there are some tasks, my question is how to run these tasks via Terminal in android studio?

gradle-task

When i run a task with Terminal in android studio it has return following error:

Task 'testTask' not found in root project 'TestProject'.
Edalat Feizi
  • 1,371
  • 20
  • 32

2 Answers2

6

We must add any custom gradle scripts to build.gradle using apply from
For example:

apply from: "$rootDir/test.gradle"
DeKaNszn
  • 2,720
  • 3
  • 26
  • 26
3

Just to explain a bit, following @DeKaNszn answer:

Depending on where is the script situated, you will need to extend your build.gradle with it.

If the script is in the root, yes including

apply from: "$rootDir/test.gradle"

is going to extend you the script you have applied it to, then if your test.gradle is in the root and you want to access the task defined in that script from your project, or any subproject, you will need to apply it in the build.gradle in this project, because otherwise gradle will not read it automatically and configure the task.

build.gradle is read automatically, therefore extending it with your script is going to configure the task you want to use and have it visible for Gradle.


There is a kind of a convention, to put your scripts in the gradle directory, and keep build.gradle and settings.gradle only in the root, but is just a preference.

But if you would like to follow this idea you will need to give it a path where to apply from, like above, and always use Gradle variables pointing to directories. $rootDir, $buildDir, etc.

LazerBanana
  • 6,865
  • 3
  • 28
  • 47