Android Studio - Importing libraries to a gradle project - android

I imported a gradle project from eclipse and I'd like to add some libraries to the project.
The usual approach would be to import a module in the Project Structure window.
The problem is that according to google I can't do that with a gradle project: Android Studio - Project structure is almost empty
If so, in what way that is compatible with gradle projects can I import a library?

In the project window, right click on your project name and new a directory named 'libs'. libs has the same level of build and src.
Copy your jar file to libs within file manager of OS.
Wait for seconds, the jar file will show under libs in android studio. Right click on it and click 'add as library'.
Add info to build.gradle, for example
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/NameOfYourFile.jar')
}
Now, you can use the library and build. If you get some errors, Enter project directory and run ./gradlew clean and rebuild your project.

Related

How to manually import a gradle dependency in android studio

I'm trying to contribute to an open source project from Mapzen called "On the Road". (Github link)
I would like to edit and add some code to the project. At the moment I'm including the project using gradle with the following dependency:
compile 'com.mapzen:on-the-road:1.1.1'
Someone gave me a tutorial on how to do this here, but I'm getting a gradle error;
Error:Configuration with name 'default' not found.
What I did was clone the project from github, copied the entire master folder into my apps main and renamed it to "customLibrary".
I then added include':customLibrary' to my settings.gradle and compile project(':customLibrary') to my build.gradle(Module:app) file.
How do I import this project without the errors?
As you cloned the entire master folder, inside the customLibrary there is the build.gradle file which is the top-level file of the library.
You can't do it.
Since you are including the customLibrary folder, gradle is looking for a module build.gradle.
You have to clone the library folder instead of the entire master folder.
Otherwise you can use include':customLibrary:library' only to add the module library of the entire project.

Importing project from Eclipse to Android Studio - Dependencies

On Eclipse I have one application as lib.
Then I have 10 more applications that import this lib.
When I'm programming, I do this on the lib project, so I run the application that import this lib. In the application project I just do especific developments.
The code is shared by all aplications.
How can I do this in Android Studio? Because I need to have just one code for all projects.
I've already imported my lib to Android Studio but I don't know how to link it to my application, having in mind that all changes in the lib needed to reflect the other projects (when I open it and compile again).
Thank you all!
---Edit:
My lib settings:
include ':actionbar'
include ':myMainLib'
My myMainLib Gradle:
dependencies {
compile project(':actionbar')
}
My Application settings:
include ':myApp'
include ':myLibProject:myMainLib'
project(':myLibProject:myMainLib').projectDir = new File(settingsDir, '../myLibProject/myMainLib')
My Application gradle:
dependencies {
compile project(':myLibProject:myMainLib')
compile 'com.android.support:support-v4:18.0.0'
}
Error message:
Error:(22, 0) Project with path ':myLibProject:actionbar' could not be
found in project ':myApplication'.
When I clicked to check the error, it's open the lib Gradle.
The actionbar source is in the lib's root dir.
As you have the lib and your apps on your hard drive, you can configure Gradle to resolve the dependency from the local file system.
You have to add these lines to your settings.gradle file, to have the library project available:
include 'yourlib:lib'
project(':yourlib:lib').projectDir = new File(settingsDir, '../yourlib/lib')
This code assumes that both project folders are in the same folder. You can adjust the relative reference or change it to an absolute reference if that fits better for you.
Then you can add the dependency to your app's build.gradle file:
compile project(':yourlib:lib')

Android Studio 1.0.1 add external libraries

I have just downloaded Android Studio 1.0.1 and this is the new project structure
and when i see it in the explorer there is a empty libs folder
i want to import 3 external libraries i have, which i want to use.
build.gradle has already defined this
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
but when i copy my libraries in libs folder my project still dont recognize it,
how do i make use of the libs folder and copy my libraries in this folders and use it?
any help?
Edit:
it was a simple question, how to add libraries to project in Android Studio
like we do in eclipse, for eg i have a library project(a folder, not a jar!).
in my case i have a library project 'viewPagerIndicator' (again a Library folder, not a jar!) i have to add it to my project like i use to do it with eclipse by importing project, right click->properties->android->Library->add-> select the library project
PS: i have found the answer now.
suppose my library project name is 'viewPagerIndicator'
- Copy library project on root of my Android Studio Project
- Under build.gradle(under your Android Studio Project folder) write this under dependencies
compile project(':viewPagerIndicator')
- include it in settings.gradle, write:
include ':viewPagerIndicator'
- Sync project with gradle files
this is how project structure look like now:
so sorry that admins stopping the question without understanding it.
First you have to add library project (module) in Android Studio
File -> Import Module
To add library project (module) in build path, click
File -> Project Structure
On the left hand side click on
app -> Dependencies tab -> green + button -> Module dependency
Now select the library project you already added.
For more details refer this link
In My Case to add external library project into android studio 1.1.0
step-1 : File -> Import Module -> set path for library project
step-2 : Set dependencies{} in main projects's build.gradle like,
dependencies {
compile project(':facebookSDK')
compile project(':slidingMenu')
compile project(':SkyconsLibrary')
}

How to update imported modules with code modification from the their external library project in Gradle/Android Studio

I'm developing an android app, in which I recently migrated from Eclipse to Android Studio and Gradle.
In my projected I created 5 UI Libs, and added them as modules in my project, libs I have created are pushed on my github account (publicly).
In eclipse when you add external dependency for a project marked as lib, when you update the external project's code then you clean your build, your project get these updates, but in Gradle I noticed that it creates Physical Copies, completely independent from their sources, my question is how can I change only external libs and have my project updated.
here's a snipped from my gradle's config:
dependencies {
// Libraries
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:5.0.89'
.
.
.
compile 'com.squareup.picasso:picasso:2.4.0'
// Projects
compile project(':com.shehabic.helpicon')
.
.
}
How to actually use external libraries
I've read that is not recommended but it is practical for debugging:
File > Project Structure... > PLUS sign to add module
"Create New Module" dialogs:
Select "Import .JAR/.AAR Package"
Find and select your package but copy the path before continue
Name your package
Back at "Project Structure" dialog:
The message "Gradle project sync in progress.." appears, but it takes to click OK for the build to actually start. Instead, you can continue with the dependency.
Select "app" module, go to DEPENDENCIES tab, and PLUS sign to add MODULE DEPENDENCY
Select your module
Click OK for the build to run.
That does create a copy of your package inside your android project but it also generates all necessary information and files, and you can always get rid of the copy or leave it alone.
Your module have been added to the settings.gradle file:
':app', ':module_name'
A build.gradle file for your module have been created:
configurations.maybeCreate("default")
artifacts.add("default", file('package.jar'))
And the dependency has been added to the build.gradle of your ':app':
compile project(':module_name')
Now, access the build.gradle file of your added module and paste the path you copied:
configurations.maybeCreate("default")
artifacts.add("default", file('X:\Java\Applications\YourApplication\dist\package.jar'))
Wherever you edit your package, just "Clean & Build" it.
Whenever you want your app to reflect that "update" in the package from outside your android project, just sync.
When you are done debugging, you can remove the module, the dependency and the old copy, and add the last build of your package as a copy.
You must not add the external library as a module. It will make copy of it under your project folder.
What you have to do is:
1) Delete the library folder in your current project.
2) Open the 'seeting.gradle' file and add these:
include ':your_external_library_module_name', ':perhaps_second_external_library'
project (':your_external_library_module_name').projectDir = new File('../path/to/your/external/library')
project (':perhaps_second_external_library').projectDir = new File('../path/to/your/second/external/library')
2) In your 'build.gradle' file add dependency as:
dependencies {
compile project(':your_external_library_module_name')
compile project(':perhaps_second_external_library')
}
3) Sync the project and you are done.
Do NOT add a module using Studio's "Open Module Settings".
Just add the full path of your library's AAR file in your child app's build gradle:
implementation files('/workspace/AndroidAppsBase/base_app/build/outputs/aar/base_app-debug.aar')
When you make changes to your library project and rebuild the project, a new .aar file will be created. To reflect the change in your child project just rebuild it.

Adding external library in Android studio

I want to add external library https://github.com/foursquare/foursquare-android-oauth to my Android application (I use Android Studio, the instructions provided by lib author for Eclipse didn't work for Android Studio).
I've tried to do it with maven, so in File->Project Structure->Dependencies I've added com.foursquare:foursquare-android-nativeoauth-lib:1.0.0 but Gradle Sync fails:
Error:Failed to find: com.foursquare:foursquare-android-nativeoauth-lib:1.0.0
When I try to build my app (without fixing above error becaus I don't know how) I get:
Error:A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugCompile'.
> Could not resolve com.foursquare:foursquare-android-nativeoauth-lib:1.0.0.
Required by:
ForSquaresOnly:app:unspecified
> Could not parse POM http://jcenter.bintray.com/com/foursquare/foursquare-android-nativeoauth-lib/1.0.0/foursquare-android-nativeoauth-lib-1.0.0.pom
> Could not find any version that matches com.foursquare:parent:1.0.0.
Any other way to import this lib? I can simply copy-paste source code into my source or create JAR out of it?
BTW: if you run into problems see this question (I had this issue after importing): Manifest merger failed : uses-sdk:minSdkVersion 14
Try one of these approaches:
Approach 1)
1- Choose project view
2- Copy your JAR file in app -> lib folder
3- Right click on your JAR file and choose add as library
4- Check it in build.gradle
Approach 2)
1- File -> New -> New Module
2- Import .JAR/.AAR Package
3- Browse your JAR File
4- Finish
5- File -> Project Structure -> Dependencies
6- You should click on + button and then click on Module Dependency
7- You will see your library here
8- choose your library and click ok
9- Then, you will see that your library is added.
For first two approaches, you need a JAR file. You can search http://search.maven.org/ to find JAR files that are related to Android. For example, this is the search result for jdom in this link
Approach 3) Android is using http://jcenter.bintray.com/ as remote library. For example, this is the search result for jdom in the link.
To add a library in this approach, please follow these steps:
1- File -> Project Structure -> Dependencies
2- Click on + button and choose library dependency
3- find your library and select it, then click OK.
Try this:
File > Project Structure > Dependencies Tab > Add module dependency (scope = compile)
Where the module dependency is the project library Android folder.
To reference an external lib project without copy, just do this:
- Insert this 2 lines on setting.gradle:
include ':your-lib-name'
project(':your-lib-name').projectDir = new File('/path-to-your-lib/your-lib-name)
Insert this line on on dependencies part of build.gradle file:
compile project(':your-lib-name')
Sync project
There are two simplest ways if one does not work please try the other one.
Add dependency of the library inside dependency inside build.gradle file of the library you are using, and paste your library in External Libraries.
OR
Just Go to your libs folder inside app folder and paste all your .jar e.g Library files there, Now the trick here is that now go inside settings.gradle file now add this line include ':app:libs' after include ':app' it will definitely work.
Any other way to import this lib? I can simply copy-paste source code
into my source or create JAR out of it?
Complete Steps for importing a library in Android Studio 1.1
Goto File -> Import Module.
Source Directory -> Browse the project path.
Specify the Module Name
Open build.gradle (Module:app) file
Add the following line with your module name
compile project(':internal_project_name')
Taken from: how to add library in Android Studio
I had also faced this problem. Those time I followed some steps like:
File > New > Import module > select your library_project. Then include 'library_project' will be added in settings.gradle file.
File > Project Structure > App > Dependencies Tab > select library_project. If library_project not displaying then, Click on + button then select your library_project.
Clean and build your project. The following lines will be added in your app module build.gradle (hint: this is not the one where classpath is defined).
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library_project')
}
If these lines are not present, you must add them manually and clean and rebuild your project again (Ctrl + F9).
A folder named library_project will be created in your app folder.
If any icon or task merging error is created, go to AndroidManifest file and add <application tools:replace="icon,label,theme">
A late answer, although I thought of giving an in-depth answer to this question. This method is suitable for Android Studio 1.0.0 and above.
STEPS
First switch your folder structure from Android to Project.
Now search for the libs folder inside app - build folder.
Once you have pasted the .jar file inside libs folder. Right click on the jar file and at end click on Add as library. This will take care of adding compile files('libs/library_name.jar') in build.gradle [You don't have to manually enter this in your build file].
Now you can start using the library in your project.
For the simplest way just follow these steps
Go to File -> New -> Import Module -> choose library or project folder
Add library to include section in settings.gradle file and sync the project (After that you can see new folder with library name is added in project structure)
include ':mylibraryName'
Go to File -> Project Structure -> app -> dependency tab -> click on plus button
Select module dependency -> select library (your library name should appear there) and put scope (compile or implementation)
Add this line in build.gradle in app level module in dependency section
implementation project(':mylibraryName')
Three ways in android studio for adding a external library.
if you want to add libarary project dependency in your project :
A. In file menu click new and choose import module choose your library project path and click ok, library project automatically add in your android studio project .
B. Now open your main module(like app) gradle file and add project dependency in dependency section dependencies {
compile project(':library project name')
if you want to add jar file :
A. add jar file in libs folder.
B. And Add dependency
compile fileTree(dir: 'libs', include: '*.jar') // add all jar file from libs folder, if you want to add particular jar from libs add below dependency.
compile files('libs/abc.jar')
Add Dependency from url (recommended). like
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
Android studio doesn't use Maven, it use Gradle, you can open your build.gradle the route should be: /app/build.gradle and add the Gradle dependency that shows on the repo:
The build.gradle has a dependencies section:
dependencies {
//here add your dependency
}
The repo says that you need to add this dependency:
compile 'com.foursquare:foursquare-android-oauth:1.0.3'
only add that line to your dependencies on buil.gradle, save the file, and android will rebuild the project, that's all
If the library you need is on GitHub then adding it to Android Studio is easy with JitPack.
Step 1. Add the jitpack repository to build.gradle:
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
Step 2. Add the GitHub repository as a dependency:
dependencies {
// ...
compile 'com.github.Username:LibraryRepo:ReleaseTag'
}
JitPack acts as a maven repository and can be used much like Maven Central. The nice thing is that the maintainers don't have to upload the library. Behind the scenes JitPack will check out the code from GitHub and compile it. Therefore for this to work there needs to be a working build file in the git repository.
There is also a guide on how to publish an Android library.
Turn any github project into a single line gradle implementation with this website
https://jitpack.io/
Example, I needed this project:
https://github.com/mik3y/usb-serial-for-android
All I did was paste this into my gradle file:
implementation 'com.github.mik3y:usb-serial-for-android:master-SNAPSHOT'
There are some changes in new gradle 4.1
instead of compile we should use implementation
implementation 'com.android.support:appcompat-v7:26.0.0'
1.Goto File -> New -> Import Module
2.Source Directory -> Browse the project path.
3.Specify the Module Name – it is used for internal project reference.
Open build.gradle (Module:app) file.
implementation project(':library')
I had the same problem. This happened because of core library dependency. I was using javax.* . This is what i did to fix
In File->Project Structure->Dependencies I added this as as provided file, not a compile. Then re build the project.
This problem started after upgrade of android studio. But I think it happens when you try to edit you build files manually.
Adding library in Android studio 2.1
Just Go to project -> then it has some android,package ,test ,project view
Just change it to Project View
under the app->lib folder you can directly copy paste the lib and do android synchronize it.
That's it
1)just get your lib from here http://search.maven.org/
2)create a libs folder in app directory
3)paste ur library there
4)right click on ur library and click "Add as Library"
5)thats all u need to do!
I hope this will definitely gonna help you!!!!
Could not find any version that matches com.foursquare:parent:1.0.0.
Means that library developer made a mistake in .pom file and specified a wrong one.
I would recommend use the standard approach. You can find .pom file in Maven Repository and use that version or ask developer to take a loot at this problem(via github issue tracker)
compile 'com.foursquare:foursquare-android-oauth:1.1.1'
SIMPLE STEPS TO ADD ANY LIBRARY IN ANDROID STUDIO:
Copy the file in question from any folder in your computer (eg. C:/documents/xyz.jar"), then go to Android Studio and right-click the Project Library folder and select paste.
Now, right click on the newly added Library file and select the option "add as Library"
D.O.N.E

Categories

Resources