Dependency from .jar file to .so from another project

I’m trying to configure Gradle so that one project of a module contains the C++ code for a JNI extension, and another project creates a Java library whose .jar file contains both the Java classes from that project and also the .so file from the JNI project. I can get the .so file included in the .jar file, but I haven’t been able to figure out how to describe the dependency for the .so file properly.

Here is build.gradle for the jni project:

plugins {
    id 'cpp-library'

library {
    targetMachines = [machines.linux.x86_64]

    tasks.withType(CppCompile).configureEach {
        compilerArgs.add '-Wall'
        compilerArgs.add '-Werror'
        compilerArgs.add '-fno-strict-aliasing'
        compilerArgs.add '-fPIC'
        compilerArgs.add '-I' + System.getenv("JAVA_HOME") + '/include'
        compilerArgs.add '-I' + System.getenv("JAVA_HOME") + '/include/linux'

and here is the build.gradle file for the lib project:

plugins {
    id 'java-library'

repositories {

dependencies {
    testImplementation 'junit:junit:4.13.2'
    // implementation project(':jni')

jar {
    from('../jni/build/lib/main/debug') {
        include '*.so'

With the implementation line commented out, the module builds and the .so file appears in the .jar file (but only after the second build), and Gradle complains about the lack of a dependency:

- Gradle detected a problem with the following location: '/users/ouster/java/jni/build/lib/main/debug'. Reason: Task ':lib:jar' uses this output of task ':jni:linkDebug' without declaring an explicit or implicit dependency...

If I uncomment the implementation line, builds fail with this message:

 > Could not resolve project :jni.
     Required by:
         project :lib
      > No matching variant of project :jni was found. The consumer was configured to find an API of a library compatible with Java 11, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally but:
          - Variant 'cppApiElements' capability grpc_homa:jni:unspecified:

I have tried several other ways of specifying the dependency, but none has worked for me. I suspect there must be an easy fix, but my newbie brain is stumped.

Any help would be much appreciated.