Manage Gradle project dependency with configurations

Dwayne Duane

New member
Jan 21, 2023
1
0
0
Visit site
In my Android project, I have a folder whose content is generated by a gradle task, and I need to package this folder as assets for the .apk I build. Initially, this is how I am setting up my gradle build file:

```
android {
...
sourceSets {
main {
assets {
srcDir generatedAssetsDir
}
}
}
...
}
...
afterEvaluate { evaluated ->
evaluated.tasks.getByPath("packageReleaseAssets").dependsOn evaluated.tasks.getByPath('taskThatGeneratesTheAssetsDir')
}
```

Now I am trying to avoid using `afterEvaluate`, so I followed this example and created two gradle projects, one acting as producer and one as consumer. The relevant portions of the producer project’s build.gradle looks like:

```
abstract class MyTaskClass extends DefaultTask {
@outputDirectory
abstract DirectoryProperty getOutputDir()

@TaskAction
def run() {
def src = new File(getProject().getRootDir().toString()+'/in/src.txt')
def dst = new File(getOutputDir().get().getAsFile().toString()+'/dst.txt')
dst << src.text
}
}

tasks.register("myTask", MyTaskClass) {
outputDir = new File(getProject().getRootDir().toString()+'/out')
}

configurations {
prepAssets {
canBeConsumed = true
canBeResolved = false
extendsFrom implementation, runtimeOnly
}
}

artifacts {
prepAssets(myTask.outputDir) {
builtBy(myTask)
}
}
```
While the build.gradle file of the consumer looks like:
```
configurations {
assetsConsumer {
canBeConsumed = false
canBeResolved = true
}
}

android {
...
sourceSets {
main {
assets {
srcDir configurations.assetsConsumer
}
}
}
....
}

dependencies {
assetsConsumer project(path: ":mylibrary", configuration: 'prepAssets')
...
}
```
The idea is that when myTask inside the producer runs, it should create a text file inside <Project_Root>/out/ folder. This folder is then specified as the Android assets folder in the consumer project.
However, when I try to perform a gradle sync, I get this strange error:

```
Could not resolve all dependencies for configuration ':app:assetsConsumer'.
Could not create task ':app:generateDebugLintModel'.
Could not resolve all dependencies for configuration ':app:assetsConsumer'.
Cannot query the value of this property because it has no value available.
The value of this property is derived from:
- task ':app:generateDebugLintModel' property 'outputDirectory'
```

Any idea what I am doing wrong with Gradle configurations?
In my Android project, I have a folder whose content is generated by a gradle task, and I need to package this folder as assets for the .apk I build. Initially, this is how I am setting up my gradle build file:

```
android {
...
sourceSets {
main {
assets {
srcDir generatedAssetsDir
}
}
}
...
}
...
afterEvaluate { evaluated ->
evaluated.tasks.getByPath("packageReleaseAssets").dependsOn evaluated.tasks.getByPath('taskThatGeneratesTheAssetsDir')
}
```

Now I am trying to avoid using `afterEvaluate`, so I followed this example and created two gradle projects, one acting as producer and one as consumer. The relevant portions of the producer project’s build.gradle looks like:

```
abstract class MyTaskClass extends DefaultTask {
@outputDirectory
abstract DirectoryProperty getOutputDir()

@TaskAction
def run() {
def src = new File(getProject().getRootDir().toString()+'/in/src.txt')
def dst = new File(getOutputDir().get().getAsFile().toString()+'/dst.txt')
dst << src.text
}
}

tasks.register("myTask", MyTaskClass) {
outputDir = new File(getProject().getRootDir().toString()+'/out')
}

configurations {
prepAssets {
canBeConsumed = true
canBeResolved = false
extendsFrom implementation, runtimeOnly
}
}

artifacts {
prepAssets(myTask.outputDir) {
builtBy(myTask)
}
}
```
While the build.gradle file of the consumer looks like:
```
configurations {
assetsConsumer {
canBeConsumed = false
canBeResolved = true
}
}

android {
...
sourceSets {
main {
assets {
srcDir configurations.assetsConsumer
}
}
}
....
}

dependencies {
assetsConsumer project(path: ":mylibrary", configuration: 'prepAssets')
...
}
```
The idea is that when myTask inside the producer runs, it should create a text file inside <Project_Root>/out/ folder. This folder is then specified as the Android assets folder in the consumer project.
However, when I try to perform a gradle sync, I get this strange error:

```
Could not resolve all dependencies for configuration ':app:assetsConsumer'.
Could not create task ':app:generateDebugLintModel'.
Could not resolve all dependencies for configuration ':app:assetsConsumer'.
Cannot query the value of this property because it has no value available.
The value of this property is derived from:
- task ':app:generateDebugLintModel' property 'outputDirectory'
```

Any idea what I am doing wrong with Gradle configurations?
 

Forum statistics

Threads
945,123
Messages
6,924,289
Members
3,159,672
Latest member
teneika lee