reactormonk
04/03/2024, 12:15 PMprintln(tasks.asMap.keys)
^ running this one inside a function called on toplevel, gives me []
🤨spand
04/03/2024, 12:18 PMreactormonk
04/03/2024, 12:18 PMVampire
04/03/2024, 12:33 PMtasks.names
which is lazy-safe.
But even that most probably will not work as you intend, as you only get the tasks that are already registered at the time you call it and not any tasks that get registered after that.
What is the actual use-case you try to solve?reactormonk
04/03/2024, 12:49 PMreactormonk
04/03/2024, 12:50 PMval signApk = tasks.register("signApk", Exec::class.java) {
group = "build"
description = "Signs the APK using a yubikey"
doFirst {
var pass: CharArray? = null
SwingUtilities.invokeAndWait {
val dialog = javax.swing.JDialog()
dialog.apply {
isModal = true
title = "Enter password"
isAlwaysOnTop = true
isResizable = false
defaultCloseOperation = javax.swing.WindowConstants.DISPOSE_ON_CLOSE
val vbox = javax.swing.Box(javax.swing.BoxLayout.Y_AXIS)
vbox.add(javax.swing.JLabel("Please enter key passphrase:"))
val input = javax.swing.JPasswordField(20)
vbox.add(input)
val button = javax.swing.JButton("OK")
button.addActionListener {
pass = input.password
dialog.dispose()
}
vbox.add(button)
add(vbox)
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
if (pass == null) {
throw InvalidUserDataException("You must enter a password to proceed.")
}
val taskName = when (val variantName = project.properties["signApkVariant"] as? String) {
"release" -> "assembleRelease"
"debug" -> "assembleDebug"
else -> throw IllegalArgumentException("Invalid build variant: $variantName")
}
val apkPath = project.tasks.getByName(taskName)
.outputs.files.singleFile.absolutePath
commandLine(
"apksigner",
"-J-add-opens=jdk.crypto.cryptoki/sun.security.pkcs11=ALL-UNNAMED",
"sign",
"--provider-class sun.security.pkcs11.SunPKCS11",
"--provider-arg",
"~/dev/reach/repo/pkcs11_java.cfg",
"--ks",
"NONE",
"--ks-type",
"PKCS11",
apkPath
)
}
}
fun configureSignApkForVariant(variant: String) {
tasks.named(":app:assemble${variant.capitalize()}") {
finalizedBy(signApk)
signApk.configure {
project.extensions.extraProperties.set("signApkVariant", variant)
}
}
}
Vampire
04/03/2024, 12:57 PMproject
at task execution phase is deprecated (or forbidden already, don't remember) and if you ever hope to use configuration cache cannot be used.
Also changing the output of another task is very bad practice, this disturbs up-to-date checks, caching, and correctness.
To mitigate the "task no found", you could do tasks.named { it == ":app:assemble${variant.capitalize()}" }.configureEach { ... }
but the other parts stay valid.
Better for example add the signing as doLast
action to the actual task that creates the apk, so that it is part of the task execution.
For doing things like getting password only once, use a shared build service that determines and provides the value.reactormonk
04/03/2024, 1:01 PMfindByName
, which gives me a Task
directoyl.reactormonk
04/03/2024, 1:03 PMVampire
04/03/2024, 3:30 PMfindByName
again completely disables task-configuration avoidance and also only gets the tasks already registered at the time you call it.
I could also add another independent task and configure intellij to manually run it via run configurations, would that be cleaner?If the task modifies the output of another task, that would not be any better, but have the exact same problems.
What's the best way to change the singing?Other than what I already recommended above?
reactormonk
04/03/2024, 4:53 PMBetter for example add the signing as doLast action to the actual task that creates the apk, so that it is part of the task execution.This part? The task is defined in the library, can I modify it afterwards?
spand
04/03/2024, 5:25 PMVampire
04/03/2024, 5:40 PMreactormonk
04/03/2024, 5:42 PMVampire
04/03/2024, 5:42 PMVampire
04/03/2024, 5:43 PMVampire
04/03/2024, 5:44 PMreactormonk
04/03/2024, 5:44 PMVampire
04/03/2024, 5:46 PMreactormonk
04/03/2024, 5:47 PMnamed
as well.Vampire
04/03/2024, 5:47 PMVampire
04/03/2024, 5:48 PMVampire
04/03/2024, 5:48 PMreactormonk
04/03/2024, 5:48 PMnamed
is fine, and use doLast
instead to attach the additional signing?Vampire
04/03/2024, 5:49 PMnamed
, yes.reactormonk
04/03/2024, 5:50 PMreactormonk
04/03/2024, 5:51 PMVampire
04/03/2024, 5:52 PMmatching
insteadVampire
04/03/2024, 5:53 PMconfigureEach
after itreactormonk
04/03/2024, 5:53 PMreactormonk
04/03/2024, 5:56 PMcommandLine()
available O.oreactormonk
04/03/2024, 5:58 PMAbstractExecTask
, which the default builder one is... not?Vampire
04/03/2024, 6:01 PMexec { ... }
insteadreactormonk
04/03/2024, 6:03 PMVampire
04/03/2024, 6:14 PMExecOperations
and call exec { ... }
on that instead of calling it on project
implicitly.