How do I add a library project to Android Studio?

前端 未结 30 4049
梦谈多话
梦谈多话 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: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.

提交回复
热议问题