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:
- Put the Gson jar (in my case,
gson-2.2.4.jar
) into thelibs
folder - Right click it and hit 'Add as library'
Ensure that
compile files('libs/gson-2.2.4.jar')
is in yourbuild.gradle
file (orcompile fileTree(dir: 'libs', include: '*.jar')
if you are using many jar files)Edit : Use
implementation files('libs/gson-2.2.4.jar')
(orimplementation fileTree(dir: 'libs', include: '*.jar')
) in Android Studio 3.0+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
How to Test If a Fragment View Is Visible to the User
Android Appcompat-V7:21.0.0 Change Material Checkbox Colors
How to Get Each Device's Ip Address in Wi-Fi Direct Scenario
How to Execute Something Just Once Per Application Start
Is Google's Android Opengl Tutorial Teaching Incorrect Linear Algebra
Volley Out of Memory Error, Weird Allocation Attempt
Quality Selector for Exoplayer 2
Android: Free Cropping of Image
How to Remove the Top and Bottom Space on Textview of Android
Update Text of Notification, Not Entire Notification
Android Exporting to CSV and Sending as Email Attachment
How to Get Free and Total Size of Each Storagevolume
Com.Google.Android.Gsf Package Couldn't Be Found
Android Studio 2.0 - Pause/White Screen on App First Run