How to setup NDK libs path in Gradle?

2019-07-29 02:56发布

问题:

I have two projects A and B.

A is a main project, that use B thought dependency A->B

NDK libs located in two projects :

--------------    --------------
|      A      |   |     B      | 
--------------    --------------
|  JNI libs   |   |  JNI libs  |
--------------    --------------


A
|-jniLibs
     |-armeabi-v7a
     |-x86

B
|-jniLibs
     |-armeabi-v7a
     |-x86

And I try to include JNI into both projects :

Piece of build.gradle from A :

 ...
 sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['jniLibs']
        }

    }

    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a'
            universalApk true
        }
    }

    productFlavors {

        x86 {
            ndk {
                abiFilters "x86", ""
            }
        }

        arm7 {
            ndk {
                abiFilters "armeabi-v7a", ""
            }
        }

    }
    ...

Piece of build.gradle from B :

 ...
 sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['jniLibs']
        }

    }

    splits {
        abi {
            enable true
            reset()
            include 'x86', 'armeabi-v7a', 'mips'
            universalApk true
        }
    }

    productFlavors {

        x86 {
            ndk {
                abiFilters "x86", ""
            }
        }

        arm7 {
            ndk {
                abiFilters "armeabi-v7a", ""
            }
        }

    }
    ...

And when I try to launch my App I've got an error about missing implementation of method from JNI lib. You can say that jni lib doesn't contains the method, but if I put all .so files to A project and try use the lib from B project - I doesn't have any error.

So the question is : how to locate directly JNI lib path in gradle?

回答1:

Finally I solved my problem. Read attentively.

The mistake was in path of Java class that worked with JNI.

There is no gradle mistakes. Mistake was in package name. I move my class that contains native methods to another package.

When you change package name(move package) for class that contains native methods, you should change method signature in native code. If you doesn't have ability to change native code you should keep package name for this class.