Whenever I have to add certain library from the internet to my Android project, I add them inside the dependencies in the app level gradle script and it downloads the library for me. Is it possible to download these library files so that I can use them in other projects as well without downloading the whole library and dependency files again?
Just go to Maven central and download the libraries.
For example, here is Volley. Just click the download JAR button.
I would strongly recommend sticking with Gradle / Maven, though, to keep consistency with versions and appropriately handle additional dependencies for the libraries you want to download. They are called package managers for a reason, and they do their job well.
The libraries are actually downloaded to disk only once and shared between projects, they aren't downloaded for every new project.
Put library's jar file inside libs folder.
Add this line in module level build.gradle (if not present):
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Other libraries
}
Find the gradle dependency for your library and put it in dependencies of your build.gradle file. For Example,
dependencies {
// other dependencies
compile 'com.google.code.gson:gson:2.6.2'
}
and then build the gradle file.
I'd just run into an issue where a coworker's AS instance wasn't syncing with the server. In that case, we simply had to manually invoke the "Sync Project with Gradle File". For whatever reason, his AS instance wasn't doing that automatically.
Related
I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project.
Note: I'm using maven dependencies.
My question: Are people using custom scripts to generate this folder? I hardly think that this is generated once and then manually maintained when there is a newer version available.
Please enlighten me!
With Android Studio AND Gradle, there is no need to use libs folder (except for old .jar library).
In fact you can develop Android app whitout Android Studio as in your build.gradle there is already a apply plugin: 'com.android.application'
Gralde is using Maven or jCenter via gradle dependencies to import libraries. Gradle and Android Gradle plugin will automaticly download the libs as you sayed in a build/ folder. It is not static and can be clean with the Clean projet on Android Studio. Also, Android Studio will add a warning when a new library version is available automaticly in your build.gradle.
Dont miss the old libs folder used to import .jar library
I currently am not using the "libs" folder for my third party dependencies (it seems they are added automatically to build/intermediates/pre-dexed/) but noticed that it may help static code analysis so I would like to add it to the project. Note: I'm using maven dependencies.
Don't confuse the libs folder with build/intermediates/pre-dexed/ folder.
Currently the gradle plugin for Android manages the build process and create these "internal" and intermediates folders.
My question: Are people using custom scripts to generate this folder?
I hardly think that this is generated once
You don't have to create this folder. The Gradle plugin for Android manage it for your. Also it will be deleted and recreated when your run a gradlew clean command.
and then manually maintained when there is a newer version available.
No. Your dependencies are defined in your build.gradle file.
When you define a new version, Gradle downloads the new dependency and updates the intermediate folders.
You can define your dependencies in many ways:
dependencies{
//You can create a folder and put jar files inside. You can use your favorite name, usually it is libs.
compile fileTree(dir: 'libs', include: ['*.jar'])
//The support libraries dependencies are in a local maven managed by SDK
compile 'com.android.support:appcompat-v7:23.0.0'
// A Maven dependency
compile 'com.squareup.picasso:picasso:2.5.2'
//A local library
compile project(':mylibrary')
//An aar file. It requires to define a repository.
//repositories{
// flatDir{
// dirs 'libs'
// }
//}
compile(name:'nameOfYourAARFileWithoutExtension', ext:'aar')
}
I'm using Square's Wire library for my Android app, using Android Studio with Gradle.
I originally added the wire-runtime-1.2.0.jar into a libs folder in my module, and added the dependency to Gradle like this in my build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
That worked fine.
I'm new to Gradle and Android Studio, but based on the way I'm depending on the Google Support and Play Services libraries, I thought I might be able to remove the wire-runtime-1.2.0.jar library from my repository and just declare a dependency like this (the line is from the Maven repository):
dependencies {
compile 'com.squareup.wire:wire:1.0.0'
}
But if I do that then I hit this error:
Gradle: package com.squareup.wire does not exist
Is there a way to set up this dependency without importing the JAR file directly? Or does that only work for libraries that you can install through the SDK Manager?
Some packages, like com.squareup.wire, have multiple artifacts in Maven Central. You need to choose the right one for your needs. In this case, the equivalent of wire-runtime-1.2.0.jar is the wire-runtime artifact, not the wire artifact.
Here's what your dependencies section should look like:
dependencies {
compile 'com.squareup.wire:wire-runtime:1.2.0'
}
my problem is simple:
in the web there are numerous ways and tutorials on how to include jars in an android application through eclipse, android studio etc.
I want to do it manually; so, I want to know which files do I have to edit to bind my app
with the specific jars.
in the web there are numerous ways and tutorials on how to include jars in an android application through eclipse, android studio
That is because the use of third-party libraries is tied to the build system being used to build the app.
I want to do it manually
It is unclear what "manually" means in this context.
If you mean that you are using Ant, just put the JAR(s) in your project's libs/ directory, and you are done. Note that this will work with Eclipse as well.
If you mean that you are using Gradle, you will need something like this in your build.gradle file:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
Or, if the JARs can be found in a Maven or Ivy repository, you can reference those as well, by defining the repository in the repositories block and then simply specifying the artifact in the compile directive:
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.guava:guava:11.0.2'
}
We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:
Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".
Is there a general rule to add third party source or jar files into an existing android studio project? Thanks
I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to build.gradle file:
Specify that you want to use maven central repository for your dependency
repositories {jcenter()}
Add compile dependency to gson 2.6.2
dependencies {compile 'com.google.code.gson:gson:2.6.2'}
Android Studio as well as your CI server should easily build your project now. And you can continue app development.
I prefer to use central repository for dependencies management because:
easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
lighter repository and easier dependency upgrade
Examples:
Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars
I should notice:
Not many libraries are in maven central and you should make some effort to use them such way in your project
You could much easier get to "class already added" error during dexing with central repository approach
You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
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
This series of steps was taken from Android Studio: Add jar as library? and is not my original answer. I am posting them here, again, because your question was the third in search results on Google when looking up this same topic. Hence, copying.
All credits to the one who wrote the steps.
Download & Copy Your .jar file in libs folder then adding one line to build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
compile 'com.google.code.gson:gson:2.3.4' ----------> I added this one
}
Do not forget to click "Sync now"
I´m using Android Studio 1.1.0
Download and copy your jar to libs folder then add the following to your app.gradle file and SYNC.
dependencies {
compile 'com.google.code.gson:gson:{version_you_need}'
}
repositories{
flatDir{
dirs 'libs'
}
}
First of all, I know how to add a local library to the build.gradle file, it was discussed in several questions here already (which are all basically the same), see here, here and here. But you have to hardcode the paths in the compile files('/path/to/lib.jar') statements in the build.gradle file, which isn't nice, not redistributable, etc, IF you use a library not within the project's folder structure. I prefer to maintain this library for all my projects in the same place (so it is always up to date for all projects etc.). So I would like to know how to add a library, which is not available via Maven, to an Android-Studio project using gradle, in a sane way, given that the library is added as a global library in AS's preferences.
What I have done so far:
I use Google's new Android-Studio, which uses gradle for the build management, to build an Xposed framework module. For that, I have to include an external library, XposedLibrary, which I downloaded from the respective Github repository to keep it up-to-date.
It contains the jar XposedLibrary/XposedBridgeApi.jar, which I added in AS as a global library (Ctrl+Shift+Alt+S -> Global Libraries -> green plus to add the folder XposedLibrary). The compilation failed, complaining that it doesn't know the imported classes. So I had to manually add the library to the build.gradle file, adding the respective line in the dependencies like so:
dependencies {
compile files('libs/android-support-v4.jar')
compile files('/home/sebastian/dev/android/XposedMods/XposedLibrary/XposedBridgeApi.jar')
}
I tried out to just add compile files('XposedBridgeApi.jar') or compile files('XposedLibrary/XposedBridgeApi.jar') but this didn't work.
So, what is a nice way to add an AS global library to the dependencies without using full paths? (I don't like the idea of symlinking to the jar file from within the lib/ folder ;) )
when referencing a file via
files("relative/path/to/a.jar")
the relative path is evaluated relative to the buildscript this snippet is in. so when your build.gradle file is located in let's say '/a/project/build.gradle' then the jar should be in '/a/project/relative/path/to/a.jar'. In a multiproject gradle build you can put the the jar in a folder relative to the root - project and reference it in all subprojects via
rootProject.files("relative/to/root/a.jar")
hope that helps,
cheers,
René
This post describes how to get XposedBridgeApi.jar working with Gradle in Android Sudio: http://forum.xda-developers.com/showpost.php?p=41904291&postcount=1570
I think here is the proper way:
Import Xposed in Android Studio
Edit the /app/build.gradle like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
provided fileTree(dir: 'deps', include: ['*.jar'])
}
The best way is to use "provided files('src/XposedBridgeApi-54.jar')" as the lib isn't allowed to be included in the module, because the XposedBridge is already installed on the phone.
With Android Studio, you have to first understand that the IDE uses the same model for a project that your command line build (gradle) uses. That is why the Project Structure dialog has a pop up that says edits here will have no effect. So adding a global library will also have no effect.
The correct way to fix such issues is to edit your gradle build scripts so that the command line gradle build works properly. Then you should just have to click on "Tools | Android | Sync Project with Gradle files" menu item to refresh the project structure in the IDE.
Finally, if your dependencies are not going to be in Maven Central, then you'd have to create a local maven repository. Read the thread here: https://groups.google.com/d/msg/adt-dev/eCvbCCZwZjs/vGfg-4vNy9MJ for background.