Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 552
Ruzard Avatar asked Aug 11 '13 15:08

Ruzard


People also ask

What is .so file in Android?

The SO file stands for Shared Library. You compile all C++ code into the.SO file when you write it in C or C++. The SO file is a shared object library that may be dynamically loaded during Android runtime. Library files are larger, often ranging from 2MB to 10MB in size.

What are Android native libraries?

Android applications can contain compiled, native libraries. Native libraries are code that the developer wrote and then compiled for a specific computer architecture. Most often, this means code that is written in C or C++.


2 Answers

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>
like image 159
ben75 Avatar answered Oct 17 '22 08:10

ben75


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.

like image 36
Kenyakorn Ketsombut Avatar answered Oct 17 '22 07:10

Kenyakorn Ketsombut