Native libraries (.so files) are not added to an a

2020-06-03 09:03发布

问题:

I'm trying to use sqlitecipher library which requires using native libraries. Their tutorial is very simple but it doesn't work for me. Every time I get the following error:

FATAL EXCEPTION: main
        java.lang.UnsatisfiedLinkError: Couldn't load stlport_shared from loader dalvik.system.PathClassLoader[dexPath=/data/app/org.example.test-2.apk,libraryPath=/data/app-lib/org.example.test-2]: findLibrary returned null
        at java.lang.Runtime.loadLibrary(Runtime.java:365)
        at java.lang.System.loadLibrary(System.java:535)
        at net.sqlcipher.database.SQLiteDatabase.loadLibs(SQLiteDatabase.java:141)
        at net.sqlcipher.database.SQLiteDatabase.loadLibs(SQLiteDatabase.java:136)

it means .so files were not attached during compilation.

all 3 files are stored in /libs/armeabi/ directory.

I assumed that the problem is that I use maven to manage dependencies (building is performed using Android Studio anyways). The next try - include those libraries into maven dependencies. According documentation it is pretty easy. I couldn't find them in a public repository so I used <scope>system</scope> with a tag. They were visible but it didn't work. Later found this link where it is said that scope "system" won't work, tried adding those libraries into local repository using

mvn install:install-file -DgroupId=net.sqlitecipher -DartifactId=stlport_shared -Dversion=1.0 -Dfile=libstlport_shared.so -Dpackaging=so -DgeneratePom=true

in the end - it didn't work.

Also I saw this topic, probably this is a solution but I don't use gradle currently. Is there a way to include .so files into the apk without moving from maven to gradle?

As I understood Android Studio uses gradle internally to build apks, so all my efforts with maven are useless until they start supporting native libraries, am I right? I have also tried building the project with Intellij IDEA Cardea (13.0), unfortunately without any success.

回答1:

As soon as the native libs are in /libs/armeabi/ all you have to do is to configure the android plugin properly. Something like this:

<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <configuration>
         <nativeLibrariesDirectory>${project.basedir}/libs</nativeLibrariesDirectory>
         ...
    </configuration>
    <extensions>true</extensions>
</plugin>


回答2:

To Question 1: Yes. I haven't had this specific problem before, but genearlly you don't need to swich to gradle. It is possible to use an ant task to add SO files to an APK. And since maven can run Ant tasks it possible.

The nativeLibsToJar that you found here is nothing else than a task that puts all the SO files into a ZIP file and then uses the extension JAR instead of ZIP. You can easily do that with the Ant task Zip (and maybe additionally rename).

To Question 2: No. Because of the first answer. There is no need for "maven to support native libraries". Just use Ant as build-system.



回答3:

You have to create jni folder for .so file include in android project using android studio.

I have attached image please follow these steps.

After creating jnilib folder you can put your all .so files in this folder.