• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Manage Gradle project dependencies with configurations

dwayneduane

Lurker
Jan 21, 2023
1
0
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?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones