Skip to content

Instantly share code, notes, and snippets.

@timmattison
Created March 24, 2022 14:57
Show Gist options
  • Save timmattison/f4e1dbb165607c168e8b3f0ca2296d33 to your computer and use it in GitHub Desktop.
Save timmattison/f4e1dbb165607c168e8b3f0ca2296d33 to your computer and use it in GitHub Desktop.
(Partial) Gradle Kotlin DSL code to copy AWS CRT JNI libs into the resources directory so they can be picked up by native image's resource-config.json
tasks.register("copyJniLibs") {
// This filter is used to find the binaries in the CRT JAR. Add additional extensions or conditions as needed.
val binaryFilter: (File) -> Boolean =
{ it.name.endsWith(".so") || it.name.endsWith(".dylib") || it.name.endsWith(".dll") }
// Find the JAR file for aws-crt and get a File object that represents it
val awsCrtJar = configurations.getByName("default")
.resolvedConfiguration
.resolvedArtifacts
.filter { it.moduleVersion.id.toString().contains("software.amazon.awssdk.crt") }
.filter { it.moduleVersion.id.toString().contains("aws-crt") }
.map { it.file }
.first()
val resourcesDirectory = layout.buildDirectory.dir("resources").get()
val resourcesPath = resourcesDirectory.asFile.toPath()
// Let Gradle know what files we're going to be outputting so that it can correctly optimize the build process
// See https://discuss.gradle.org/t/gradle-7-0-seems-to-take-an-overzealous-approach-to-inter-task-dependencies/39656/2 for details on why we need this
// NOTE: We resolve the path on our own here so there's a chance that this code and the copy code could resolve to
// different paths.
outputs.files(zipTree(awsCrtJar).files
.filter { binaryFilter(it) }
.map { resourcesPath.resolve(it.name) }
)
doLast {
copy {
// Find all the binaries in the AWS CRT JAR and copy them into the resources directory
from({ zipTree(awsCrtJar) }) {
include { binaryFilter(it.file) }
// If this isn't included the resources directory ends up with a lot of empty directories
includeEmptyDirs = false
}
into(resourcesDirectory)
}
}
}
tasks.build {
// Make sure the build task depends on this task so the binaries get copied before nativeCompile runs
dependsOn("copyJniLibs")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment