This message was deleted.
# community-support
s
This message was deleted.
v
Did you only replace what you said, or also create by register?
a
ive tried both, same issue occurs
v
Can you maybe knit an MCVE?
a
i'll give it a go
v
Btw. you should not use a
Copy
task there, as you do not have an output directory reserved for it but there other files are in too. This will disturb up-to-date checks and also fingerprint more files than necessary and thus waste time. Better use
copy { ... }
in the task action, remove the type, and define the single input file and output file explicitly. (Not related to your problem)
a
sure thing, this is rather the legacy code im touching
hey so MCVE is a plain android project with said block pasted in
apologies i couldve done it better
Copy code
tasks.configureEach { task ->
        tasks.register("task", Copy) {
        }
    }
this is enough to trigger the same issue
v
Yeah well, for that the error is quite clear I'd say.
It tells you that your cannot use
register
there. So in that case you probably want to use
create
indeed. The task is only added when it is clear that it is going to be needed anyway, so there is not much need to try avoiding is configuration.
a
ah, same problem happens with create though
would stick with whenTaskAdded, but the reason im moving off that is because it breaks minify task as of AGP 8.1
i realised, since i am not doing task configuration, maybe this would work
Copy code
tasks.forEach { task ->

    // Rename app.aab:
    if (task.name.startsWith("bundle")) {
        def variantName = task.name.substring("bundle".length()).uncapitalize()
        def renameTaskName = "rename${task.name.capitalize()}Output"
        tasks.register(renameTaskName, Copy) {
            def path = "${buildDir}/outputs/bundle/${variantName}/"
            from(path)
            include 'app.aab'
            destinationDir = file(path)
            rename 'app.aab', "ScottishPower-${variantName}-${app_version_name}.aab"
        }
        task.finalizedBy(renameTaskName)
    }
}
v
It would not.
tasks.forEach
is a standard
forEach
. That means it again breaks task-configuration avoidance by realizing all tasks that are already registered. And additionally it also just processes tasks that are already registered the moment you call it.
a
i guess that's not great.. how else can i express my original use case?
v
Hm, what is the actual full stacktrace you got?
Can you maybe show a build
--scan
?
a
in which case? after migrating the APIs?
v
When getting the error with
configureEach
and
create
Ah, wait
It happens here too, I thought I used it like that before
a
yep
v
Then I don't remember whether there is an appropriate way. Maybe you just cannot lazily add tasks like that.
a
but whenTaskAdded crashes minify steps as of AGP 8.1, is there a third way?
v
I'm not an Android developer, so I cannot say for sure. But I just checked how I did it in the past. Use
all
instead of
configureEach
, and
register
instead of
create
. But that will as well disable task-configuration avoidance for all iterated tasks, so better restrict the task set first as far as you can if possible, for example like
tasks.withType(...).all { tasks.register(...) }
, then at least only all tasks with that type are realized.
Other than that, don't configure depending on the tasks, but depending on the source of those tasks.
I cannot say how to do it concretely as I have only little Android knowledge
a
no worries, i appreciate all the help
i think
.all
has the same problem as
whenTaskAdded
v
But for having an accompanying task for all spotbugs tasks I for example instead do
_sourceSets_.configureEach
, assuming that there are only the standard spotbugs tasks one for each source set
i think
.all
has the same problem as
whenTaskAdded
Maybe do not add a new task at all? Maybe it would be feasible instead to add a
doLast
action to the
bundle...
task that does the copying?
a
its a thought! i dont have much experience with gradle task configuration so im not sure how to do that but i have plenty of things to look for at least
v
Something along the lines of
Copy code
tasks.matching { it.name.startsWith('bundle') }.configureEach { task ->
    task.outputs.file(...)
    task.doLast {
        copy {
            ...
        }
    }
}
a
i look at all the tasks generated and have no idea if we even need all of these. which is a whole other problem
may i ask what
task.outputs.file(...)
does?
v
You produce an additional output file by doing the copy, so you should also define it as output file for the task
a
i have this
Copy code
tasks.matching { it.name.startsWith("bundle") }.configureEach {task ->
def variantName = task.name.substring("bundle".length()).uncapitalize()
    def path = "${buildDir}/outputs/bundle/${variantName}/"
    def filename = "ScottishPower-${variantName}-${app_version_name}.aab"
    task.outputs.file(path + filename)
    task.doLast {
        copy {
            from(path)
            include 'app.aab'
            destinationDir = file(path)
            rename 'app.aab', filename
        }
    }
}
seeing if it works
it works, but along the way i realised this block of code does nothing and we don't need it
👌 1
awesome