How to Add External Jar Libraries to an Android Project from the Command Line

How to add external jar libraries to an android project from the command line

Jay K's answer was right at the time of writing, but now the SDK has changed, and here is the new way to do it:

Add the following line to ant.properties:

jar.libs.dir=lib

Tested, works where external.libs.dir does not work.

That changed in December 2010 and then again in October 2011.

Android Studio - Importing external Library/Jar

So,

Steps to follow in order to import a JAR sucesfully to your project using Android Studio 0.1.1:

  • Download the library.jar file and copy it to your /libs/ folder inside your application project.
  • Open the build.gradle file and edit your dependencies to include the new .jar file:

compile files('libs/android-support-v4.jar', 'libs/GoogleAdMobAdsSdk-6.4.1.jar')

  • File -> Close Project
  • Open a command prompt on your project's root location, i.e 'C:\Users\Username\AndroidStudioProjects\MyApplicationProject\'
  • On the command prompt, type gradlew clean, wait till it's done.
  • Reopen your application project in Android Studio.
  • Test run your application and it should work succesfully.

Adding external .jar to androidstudio project

Change your dependencies like that

dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'org.beanshell:bsh:2.0b4'
}

You can now remove manually downloaded dependencies in libs directory.

What you did was adding libraries to Android Studio project only. You should always add them to Gradle build files as only this is interpreted by Android Build Tools.

There is also new version of build tools 18.0.1, you can install them and change version in you build.gradle. As far as I know they can handle aar dependencies better.

Adding external library in Android studio

Try this:

File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)

Where the module dependency is the project library Android folder.

Android Studio: Add jar as library?

I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I finally cracked it – here are the steps I took:

  1. Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
  2. Right click it and hit 'Add as library'
  3. Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file (or compile fileTree(dir: 'libs', include: '*.jar') if you are using many jar files)

    Edit : Use implementation files('libs/gson-2.2.4.jar') (or implementation fileTree(dir: 'libs', include: '*.jar')) in Android Studio 3.0+

  4. Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system

After I did the above four, it started working fine. I think the 'Add as library' step was the one I'd previously missed, and it didn't work until I cleaned it either.

[Edit - added the build.gradle step which is also necessary as others have pointed out]

Can't import an external .jar library into my Android project on Eclipse

Adding the JAR to your project's build path yourself as if it were a plain Java project will not work.




You add libraries to an Android project by copying the JAR to the libs folder at the root of your project (if the folder does not exist, then create it); ADT will then automatically add it to your build path and set up your project to bring the JAR into your APK.




You can learn more here http://tools.android.com/recent/dealingwithdependenciesinandroidprojects

How can I add a JAR in my gradle project?

just add

compile fileTree(dir: 'libs', include: '*.jar')

to your dependencies in the build.gradle then all the jars in the libs folder will be included.



Related Topics



Leave a reply



Submit