How do I set LD_LIBRARY_PATH using gradle?

I want to use a library with a C++ project and I need to set the LD_LIBRARY_PATH to get it to compile and execute fine (ie, do export LD_LIBRARY_PATH=path through gradle). The code compilation is done using gradle. I am trying to set the LD_LIBRARY_PATH in build.gradle as below but it doesn’t seem to work:

task exportLDLibPath() {
    exec {
        environment 'LD_LIBRARY_PATH', '/path/to/lib'
    project.check.dependsOn it

What am I missing here? I need to path to be set so that the unit tests can run.

Besides that your task does nothing at execution time, but always at configuration time and that always, even if the task will not run, it starts an executable with that environment variable set, but that has no effect whatsoever to the rest of the Gradle run, Gradle is not a shell command processor. Besides that the exec should also not work at all but throw an exception as you didn’t specify an executable to execute.

If you want to set an environment variable on the test task, then configure the test task to set that environment variable.

Thanks for your inputs. I am beginner and still getting a hang of things. So just adding environment 'LD_LIBRARY_PATH', '/path/to/lib' inside another task (say the test task) is sufficient?

If that task supports environment variables, yes.
You cannot just do it in any task.
But the Test task has this support, yes: Test - Gradle DSL Version 7.6

My recommendation is, that you stop using the Groovy DSL and use the Kotlin DSL instead. You get type-safe build scripts with that, better error messages if you screw it, and an amazingly better IDE support if you use a good IDE like IntelliJ.

Thanks for the help, setting it in the test task helped.