How to use a library project in android studio
Asked Answered
A

4

26

I have been trying to add this library project to my existing project in Android Studio. It's the first time I am going to use a library project and I am having tough time. I have looked around for many ways to do this from tutorials and posts around but couldn't get it done.

Annual answered 4/12, 2013 at 14:11 Comment(0)
H
30

I'm not sure if it's already possible to add a library project via the IDE (-> without any problems). I do this by configuring the gradle files of my project like this:

  • create a folder in your root project directory named libs
  • copy the folder datetimepicker-library to libs
  • add this library in your settings.gradle with the following command:

    include ':libs:datetimepicker-library'
    
  • go to your build.gradle file of your AppProject and add the following line to your dependencies:

    implementation project(':libs:datetimepicker-library')
    
  • at least you have to sync your gradle files: Tools -> Android -> Sync Project with Gradle Files

Please try this. If you get errors please post the log file.

Hijacker answered 6/12, 2013 at 7:5 Comment(4)
I believe this is the correct answer, but I couldn't get my build.gradle file to accept the 'compile project' line. Using "compile fileTree(dir: 'libs', include: ['datetimepicker-library'])" however, worked for me.Petitionary
This last comment worked for me. With the original compile project... IDE complained of an undefined 'default' something. Anyway: the library code doesn't appear on AS, so I guess I should open another Android Studio window to edit it.Rapallo
Not working for me with Mapbox. Error:(13) No resource identifier found for attribute 'accessToken' in package 'com.example.myapp'Notch
I followed these steps you mentioned and getting the error Error:Project :app declares a dependency from configuration 'compile' to configuration 'default' which is not declared in the descriptor for project :libs:recognito. I am trying to include the lib - github.com/amaurycrickx/recognitoFlat
A
8

I did it this way ,

  1. go to project Structure from File menu
  2. Select modules from the left pane
  3. press on `+'
  4. complete the new module wizard steps Make sure to make the module package name same as the module you want to add
  5. again open Project structure as in step 1
  6. select your project from the module list and on the right select dependencies
  7. Click on the + icon from right
  8. select module dependency and then select the newly added module.
  9. copy files of the library project to the new directory created as module
  10. Done

Step 1:

step 1

Step 2:

Step 2!

Step 3:

Step 3

Step 4:

**Step 4:**

....

Step 6 & 7:

enter image description here

Step 8: enter image description here

Annual answered 5/3, 2014 at 11:51 Comment(1)
Any suggestion for adding github.com/saik0/UnifiedPreference ? I tried to follow this, but AS 0.5.1 kept complaining about a failed syncCoeternal
D
0

The other answers make it seem more difficult than it usually is. Just add a single compile line to your dependencies section of the app's build.gradle file.

In this case it is

dependencies {
    // ...
    compile 'com.github.flavienlaurent.datetimepicker:library:0.0.2'
}

Gradle prompted me to sync and after I did so the library was automatically downloaded into my project.

Dispart answered 25/1, 2017 at 10:23 Comment(0)
D
0

The problem with solutions listed above is that you will end up with a clone of the library project in the app using the library. This complicates making updates to the library because you need to remove and re-import the module when you make changes.

The solution I found can use the library project directly from outside folder and it does not clone the original library files.

The solution is easy...

In settings.gradle add the following lines:

include ':commonwidgets'
project (':commonwidgets').projectDir = new File(settingsDir, '../SharedWidgets/commonwidgets')

and in the build.gradle dependency section add:

implementation project(path: ':commonwidgets')

Note: commonwidgets is the name of my library, you should replace the name and path according to your library name and path.

I hope this helps someone...

I really would have liked to have the library import from GitHub bit I refuse to pay $9/month for jetpack for private libraries.

Driskell answered 16/2, 2019 at 2:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.