How to use .so files in Android

Program-Me-Rev Source

I am tring to add a C++ library into my Android project, but I keep getting the error:

Could not get unknown property 'jniLibs' for source set 'main' of type org.gradle.api.internal.tasks.DefaultSourceSet.  

The project attempts to use a .so library of SQLite ( amalgamation).

How can I include the .so lib in my project and link it successfully to be able to call its functions?

Thank you all in advance.

My build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 26
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    buildToolsVersion '27.0.3'
    externalNativeBuild {
        cmake {
            path 'CMakeLists.txt'
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs = ['src/main/jniLibs']
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:26.+'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs']
    }
}
javaandroidc++csqlite

Answers

answered 5 months ago xingjiu #1

  1. remove config in build.gradle. This path is the default value of jniLibs, no customer config is needed.

    sourceSets { main { jniLibs.srcDirs = ['src/main/jniLibs'] } }

or try with this config

sourceSets {
    main {
        jniLibs.srcDir(['src/main/jniLibs'])
    }
}
  1. copy the so file into app/src/main/jniLibs.
  2. add the JNI java interface jar into your dependency.
  3. System.LoadLibrary(libName) // for example you put xxx.so into jniLibs folder. here would be System.LoadLibrary("xxx")
  4. Use the native method in JNI java class to call so method.

comments powered by Disqus