How do I add a library project to Android Studio?

前端 未结 30 3900
梦谈多话
梦谈多话 2020-11-21 04:24

How do I add a library project (such as Sherlock ABS) to Android Studio?

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

相关标签:
30条回答
  • 2020-11-21 05:01

    Android Studio 3.0

    Just add the library name to the dependencies block of your app's build.gradle file.

    dependencies {
        // ...
        implementation 'com.example:some-library:1.0.0'
    }
    

    Note that you should use implementation rather than compile now. This is new with Android Studio 3.0. See this Q&A for an explanation of the difference.

    0 讨论(0)
  • 2020-11-21 05:03

    I also encountered the same problem then I did following things.

    1. I import the library project into my AndroidStudio IDE as a module using menu File -> Import module menus

    2. Then I went to my main module in which I want the library project as a dependent project

    3. Right click on the main module (in my case its name is app) -> open module setting -> go into dependencies tab -> click on + button (you will get it on right side of window) -> click on module dependency -> select your library project from list

    Apply the changes and click the OK button.

    It worked for me. I hope it will help others too.

    0 讨论(0)
  • 2020-11-21 05:04

    Editing library dependencies through the GUI is not advisable as that doesn't write those changes to your build.gradle file. So your project will not build from the command-line. We should edit the build.gradle file directly as follows.

    For instance, given to following structure:

    MyProject/

    • app/
    • libraries/
      • lib1/
      • lib2/

    We can identify three projects. Gradle will reference them with the following names:

    1. :app
    2. :libraries:lib1
    3. :libraries:lib2

    The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

    dependencies {
        compile project(':libraries:lib1')
    }
    
    0 讨论(0)
  • 2020-11-21 05:05

    If you need access to the resources of a library project (as you do with ABS) ensure that you add the library project/module as a "Module Dependency" instead of a "Library".

    0 讨论(0)
  • 2020-11-21 05:05

    In Android Studio, go to inside app folder, and open build.gradle file. Here you will see dependencies{}. Inside it you can add the library project and synchronise. Now after synchronising the library it will be added to your project, and you can use its functions and classes in your project.

    0 讨论(0)
  • 2020-11-21 05:06

    I would consider Dependencies, Android Libraries and Multi-project setup necessary reading. Please take a few minutes to do so.

    Particularly, in the case of a non-jar library project, read the following snippet from above source:

    Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project.

    For instance, given to following structure:

    MyProject/
     + app/
     + libraries/
        + lib1/
        + lib2/
    

    We can identify 3 projects. Gradle will reference them with the following name:

    :app
    :libraries:lib1
    :libraries:lib2
    

    Each projects will have its own build.gradle declaring how it gets built. Additionally, there will be a file called settings.gradle at the root declaring the projects. This gives the following structure:

    MyProject/
     | settings.gradle
     + app/
        | build.gradle
     + libraries/
        + lib1/
           | build.gradle
        + lib2/
           | build.gradle
    

    The content of settings.gradle is very simple:

    include ':app', ':libraries:lib1', ':libraries:lib2'
    

    This defines which folder is actually a Gradle project.

    The :app project is likely to depend on the libraries, and this is done by declaring the following dependencies:

    dependencies {
        compile project(':libraries:lib1')
    }
    

    Kindly note that there was little or no use of Android Studio GUI to make this happen.

    I am currently using git submodules to link the nested library to the actual library git repo to avoid a dependency mess.

    0 讨论(0)
提交回复
热议问题