Home > Software design >  How do I include a Gradle dependency based on architecture?
How do I include a Gradle dependency based on architecture?

Time:04-12

I recently got a new M1 Macbook and started seeing this stacktrace in Netty on application startup. The recommended solution is to add this dependency:

runtimeOnly "io.netty:netty-resolver-dns-native-macos:4.1.75.Final:osx-aarch_64"

Since this is an ARM64 specific dependency, it doesn't seem right to just add it to the build considering others on my team have x86 workstations and it will ultimately be deployed to x86 based instances. I only really need this dependency when executing the run task, so how do I optionally include it when it is executed on ARM64 machines?

It seems like a custom Gradle configuration would be required, but I can't figure out how to automatically include it based on architecture.

CodePudding user response:

in your build.gradle you can define

ext.osArch = System.getProperty("os.arch") //return `x86_64` or ..
ext.osNameSP = System.getProperty("os.name") //return `Mac OS X` or ..
ext.osName = "Mac OS X" == osNameSP ? "osx" : "none"
ext.osClass = "${osName}_$osArch"

//construct the string for dependency
ext.depString = "io.netty:netty-resolver-dns-native-macos:4.1.75.Final"
ext.depStringFinal = "osx_x86_64" == osClass ? "$depString:$osClass" : depString
println "$osClass | $depString | $depStringFinal"

//apply in dependency
dependencies {
    implementation "$depStringFinal"
}
  • Related