Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy or use native libs with Android Studio Gradle build?

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

like image 848
Sven Haiges Avatar asked Jul 08 '13 08:07

Sven Haiges


People also ask

Where is the libs folder in Android Studio?

How to find the libs folder in Android Studio? If you are unable to find the libs folder in Android studio then open your android project in “Project” mode If the project is already opened in the “Android” mode. Then go to Your Project Name > app > libs and right-click on it and paste the downloaded JAR files.

How do I manually sync Gradle files?

Open your gradle. properties file in Android Studio. Restart Android Studio for your changes to take effect. Click Sync Project with Gradle Files to sync your project.

How import AAR to Gradle?

Add your AAR or JAR as a dependencyNavigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Jar Dependency in the dropdown. In the Add Jar/Aar Dependency dialog, first enter the path to your . aar or .


2 Answers

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.

like image 70
Netherdan Avatar answered Sep 26 '22 00:09

Netherdan


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.

like image 45
rcbevans Avatar answered Sep 27 '22 00:09

rcbevans