5

I have a native lib in the

/libs/armeabi folder called libparser.so and an associated jar file.

I changed the gradle build file to include the jar file, which seemsm to be easy (MYNEWJAR):

dependencies {
    compile files('libs/android-support-v4.jar', 'libs/MYNEWJAR.jar')
}

But when I run the app, I think it cannot find the native lib:

E/AndroidRuntime(22569): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load parser from loader dalvik.system.PathClassLoader[dexPath=/data/app/com.hybris.mobile.history-1.apk,libraryPath=/data/app-lib/com.hybris.mobile.history-1]: findLibrary returned null
E/AndroidRuntime(22569):    at java.lang.Runtime.loadLibrary(Runtime.java:365)
E/AndroidRuntime(22569):    at java.lang.System.loadLibrary(System.java:535)
E/AndroidRuntime(22569):    at com.senstation.android.pincast.Pincast.<clinit>(Pincast.java:1299)
E/AndroidRuntime(22569):    ... 17 more

Can you help me get the build file straight so it will include the native lib? This seems to be happening automatically on Eclipse, but i really want to use android studio.

Thx! Sven

1
  • Have you tried to re-import the project into Android Studio? According to your stacktrace, it has a problem with com.hybris.mobile.history-1 - which is NOT in android-support-v4.jar. Jan 10, 2014 at 19:35

5 Answers 5

6

I found this answer from user Assaf Gamliel very useful.

And just made some changes to make it even more cleaner.

You don't need to rename the .zip file to .jar, just add it with a normal compile file dependency on build.gradle script. So, you would make a foo.zip file with a structure similar to this:

foo.zip ->
|--/lib
|--|--/armeabi
|--|--|--*.so
|--|--/x86
|--|--|--*.so

put it in your libs folder and then add it to gradle using compile files('libs/foo.zip'):

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+' //gradle plugin update for Andoid Studio 0.2.+
    }
}
apply plugin: 'android'
dependencies {
    compile files('libs/android-support-v4.jar')
    compile files('libs/foo.zip') //zip file should be in Module's /libs folder (not the Project's /lib)
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }
}

while gradle does the build it just unzip the file you added preserving its structure.

2
  • updated to add the full build.gradle script, dependency should be added as a compile files dependency, where android-support-v4.jar is added, not as a classpath dependency (that's for maven)
    – Netherdan
    Jul 19, 2013 at 12:43
  • I can confirm that this workaround indeed works - and oddly enough it does not work for tar.bz2 archives - it seems they are silently ignored! Lets hope the Android Gradle plugin will handle this soon...
    – slott
    Dec 11, 2013 at 21:52
4

To use an external jar library

  1. Put the jar into the libs folder/drag the file onto libs from a file explorer
  2. Right click it and select Add as library
  3. Ensure that compile files('libs/your_jar.jar') is in your build.gradle file

    To do this, modify build.gradle which is under [projectname]Project -> [projectname] in the project pane on the left.

    For me, it was necessary to change

    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
    }
    

    to

    dependencies {
        compile 'com.android.support:support-v4:13.0.+'
        compile files('libs/universal-image-loader-1.8.5.jar')
    }
    

    enter image description here

  4. Click Rebuild Project under the Build menu.

I did this today to get the Universal Image Loader library integrated with my project.

2

Create a directory called 'jniLibs' into 'app/src/main/' and put inside all the .so

app/src/main/jniLibs/
|---- armeabi-v7a/your.so
|---- armeabi/your.so
|---- x86/your.so
1
  • 1
    This is the correct answer if you are at 0.7.2+ of the Gradle plugin. A few more details here: stackoverflow.com/a/20926595/897397. Remember to call System.loadLibrary("LIBNAME"); where your library name is libLIBNAME.so Dec 20, 2014 at 4:26
1

Hmm...I was HOPING that someone will provide a clear example, of how to make a 3rd-party JAR file accessible to Android-Studio, by showing the exact SYNTAX of what the resulting 'build.gradle' file's dependency-clause would look like, after they've added their 'foobar.jar' entry.

You know, something like:

=========

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
    compile 'org.somedevs.foobar:Foobar.jar'
}

=========

[Otherwise, I don't stand a snowball's chance in hell of guessing what the answers posted so far really mean. My INTITAL clause contained the single 'compile' line...so my GUESS would that one should add another such line!?!?]

EDIT: Yes, many THANKS, rebelious! I now, too, have it working. [Instead of the 'drag/drop' onto the 'libs' in Studio, I have more reliable results by just right-clicking on 'libs' in Studio and choose "add as library...", after copying the JAR into that location, using cmd-line.]

The correct form for the dependencies clause is the form shown below:

 dependencies {
        compile 'com.android.support:support-v4:13.0.+'
        compile files ('libs/Foobar.jar')
    }
1
  • Create a directory called 'jniLibs' into 'app/src/main/' and put inside all the .so
    – BQuadra
    Nov 24, 2014 at 11:35
0

Due to security reason, it's not possible to reference a local jar/aar file in an application project with the gradle android plugin.


For the support library, with the Android SDK Manager, you have to install the extra named Android Support Repository which will expose the support library inside a maven repository. Then you can add the support library in your project via :

dependencies {
    compile 'com.android.support:support-v4:13.0.0'
}

For external libraries, you have 2 possibilities :

  • Build an aar file and deploy it to your local maven repository, then reference it in your project like you did with the android support library.
  • Put the library sources beside your application project and create a settings.gradle at root which will define the modules. (see the docs for more info).

On my side I would prefer build aar files because it's more modular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.