How to copy or use native libs with Android Studio

2019-04-30 05:02发布

问题:

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:

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')
    }


回答2:

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.



回答3:

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')
    }
    

  4. Click Rebuild Project under the Build menu.

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



回答4:

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


回答5:

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.