9

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.

JoshDM
  • 4,939
  • 7
  • 43
  • 72
Ruzard
  • 1,167
  • 3
  • 16
  • 33
  • 1
    Are the files actually in the APK? You can use unzip to open the APK. – Frohnzie Aug 15 '13 at 13:19
  • that's a problem - they are not in the apk. I could copy them to assets folder and then load libs from the specific folder (actually your comment helped me to realize that, thanks) but I would like to do it in the proper way. It might in the future. – Ruzard Aug 15 '13 at 14:00
  • 1
    The NDK is not supported in AssetStudio/gradle. Have you tried creating the project with Eclipse or Ant? For some reason the .so files are not getting packaged. If you do decide to copy files from the assest folder make sure you use java.lang.Runtime.load and provide the full path. – Frohnzie Aug 15 '13 at 14:09
  • Thanks, I saw this in other questions however my situation is a little bit different (I use maven). I would change code to load libraries using full path if it was my code. I could make a fork of the library and change sources then I have to spend time in the future to adopt my changes for the updated versions. As for Eclipse - I'd rather use vim than Eclipse. No, I haven't tried building with it. I realize that the problem is in Intellij IDEA – Ruzard Aug 15 '13 at 14:18
  • @Frohnzie , I'll try to connect libraries through assets folder. Going to do that a bit later. If I succeed - let's find a way to give you the reward for your comment. It led me to the solution. – Ruzard Aug 15 '13 at 14:24
  • Seems other have found workarounds for gradle/AndroidStudio. http://stackoverflow.com/questions/16683775/include-so-library-in-apk-in-android-studio/17131418#17131418 – Frohnzie Aug 15 '13 at 15:55
  • if you check my question I've already referenced that topic – Ruzard Aug 16 '13 at 12:20
  • Well, [this bug](http://code.google.com/p/maven-android-plugin/issues/detail?id=372) seems to exist atm. There's a workaround in there to manually copy the libraries from dependencies. – Delyan Aug 22 '13 at 12:28

3 Answers3

10

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>
ben75
  • 29,217
  • 10
  • 88
  • 134
  • 1
    Thank you for your answer, it helped me. I still cannot believe that I missed the "nativeLibrariesDirectory" line, it just points to the wrong directory in my case. my workaround was to copy libraries from assets folder to a custom one (otherwise there is no path to these files) on startup and send a path to the sqlcipher so it loads copies of libraries from a custom folder (for that I needed to rewrite a calling function first). Damn, cannot believe the solution was that easy. – Ruzard Aug 22 '13 at 15:11
  • @ben75 will this enable you to replace or make changes to existing system libraries? Why I ask is so that these 3 files (libaudio.so, libaudioflinger.so, libaudiopolicy.so) can be replaced to enable voice_call recording from both ends of the call. Android currently has a problem with voice recording. Some devices(modified with custom roms) allow voice recording. But if we can find a way to replace these 3 files on any android device, that will be a huge break through as there are thousands of android users wanting to record phone calls, but simply can't. – Pierre Jan 03 '14 at 10:04
  • @Pierre I don't think it's possible to modify system libraries without rooting your device. (this post is related to native libraries "private" to an apk/user) – ben75 Jan 03 '14 at 10:37
  • @ben75 Is it possible to exclude a file in the /libs directory, by changing the configuration ? My question is here: http://stackoverflow.com/questions/30366514/avoid-adding-particular-native-library-files-so-to-the-project – naveejr May 21 '15 at 06:54
1

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.

Community
  • 1
  • 1
Kenyakorn Ketsombut
  • 2,072
  • 2
  • 26
  • 43
  • I appreciate your answer, but please take a look at ben75's answer – Ruzard Aug 22 '13 at 15:12
  • @Ruzard Yes, there it goes. My way here is also more the description of a hack. ben75 tell you the real way. One more day, that we go smarter to sleep than we woke up :) – Kenyakorn Ketsombut Aug 23 '13 at 05:18
  • @KenyakornKetsombut I agree with your comments, ant seems fine for packaging , people keep telling me to switch to gradle, I don't really want to. Ant is fine with arm64-v8a abi?? I thought maybe for that need to repackage using aapt2.... – Sixjac Mar 11 '22 at 13:32
  • @KenyakornKetsombut to your Question 1 answer. What does zipping to .jar have to do with getting native libs into the apk? They normally just stay in libs/{abifolder} within the apk. It seems using Ant via commandline does not add native libs by default. – Sixjac Mar 13 '22 at 08:51
  • @KenyakornKetsombut so in theory you say i can add: to my build.xml and it will all my native lib dir to the apk – Sixjac Mar 13 '22 at 09:32
1

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.enter image description here

Ramkailash
  • 1,852
  • 1
  • 23
  • 19