【问题标题】:how to add libs dependency from project to module in android studio Gradle如何在android studio Gradle中将libs依赖从项目添加到模块
【发布时间】:2016-05-03 12:49:53
【问题描述】:

我创建了一个 android 项目,它的名称是一个示例应用程序我还在示例应用程序项目中创建了一个库模块。示例应用程序依赖于其他库,因此我在示例应用程序的 libs 文件夹中添加了所需的 jar 文件。模块内部也需要相同的 jar 文件。

如何添加模块的依赖项以引用示例应用程序的 libs 文件夹? 我在下面尝试过,但每次都给出重复复制文件异常。

repositories {
    flatDir { dirs '../libs' }
}

compile fileTree(dir: '../libs', include: '*.jar')

【问题讨论】:

    标签: android android-studio build.gradle


    【解决方案1】:

    将此添加到您应用的 build.gradle:

    dependencies {
        ....
        compile project(':module-name')
    }
    

    【讨论】:

    • 嗨,杰伊,我已将 compile project(':module-name') 添加到我的应用程序 build.gradle。但我的问题是我想在我的模块中使用应用程序的库。意味着,我的应用程序和模块想要使用相同的依赖项。
    • @sathish 检查此解决方案将帮助您stackoverflow.com/questions/21170395/…
    【解决方案2】:

    在应用级 build.gradel

     dependencies {
            compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:multidex:1.0.1'
        compile 'com.android.support:support-v13:22.2.1'
        compile 'com.facebook.android:facebook-android-sdk:4.1.0'
        compile 'com.squareup.picasso:picasso:2.5.2'
    
        compile files('lib/universal-image-loader-1.9.3.jar')
    
        }
    

    【讨论】:

    • 你假设universal-image-loader-1.9.3.jar添加到lib文件夹中
    • 这是 libs,而不是 lib
    【解决方案3】:

    为了在 android studio 中添加模块依赖,请按照以下说明进行操作:

    您应该将库模块放入应用程序项目中。为了指定模块依赖关系,只需:

    Right click on Application->Open Module Settings
    Click on the '+' icon
    Select the root directory for your library module you'd like to add.
    Follow the prompts
    

    然后,该模块将显示在您的项目中。然后,您需要将其作为库依赖项添加到 Application 中。再次,在您的模块设置中:

    Select your Application module
    Select the Dependencies tab on the right
    Click the '+' icon on the bottom
    Select Module Dependency
    Select your desired library module
    

    【讨论】:

    • 嗨 Jaimin,感谢您的重播。但我无法在模块名称之前找到复选框。我想为应用程序及其模块添加公共库。
    • 那么,google 一下吧,希望 GoogleBaba 能帮到你很多..!
    【解决方案4】:

    如果您希望一个库 (*.jar / *.aar) 对整个项目可见,那么您需要使其位置对所有项目可见。

    项目的gradle文件:

    allprojects {
        repositories {
            jcenter()
            flatDir {
                // This is where the *.jar or *.aar should be located
                dirs "$rootProject.projectDir/libs"
            }
        }
    }
    

    示例

    具有以下目录结构:

    SampleApp/
    |--- app/
    |--- libs/
         \--- myLibrary.aar
    |--- myModule/
    |--- build.gradle (<-- Project's gradle file)
    \--- settings.gradle
    

    以及以下依赖关系图:

    compile - Classpath for compiling the main sources.
    |--- project :myModule
        \--- :myLibrary:
    \--- :myLibrary:
    

    然后

    SampleApp 的 build.gradle:

    dependencies {
        compile project (":myModule")
        compile (name: 'myLibrary', ext: 'aar')
        // ...
    }
    

    myModule 的 build.gradle:

    dependencies {
        compile (name: 'myLibrary', ext: 'aar')
        // ...
    }
    

    【讨论】:

      【解决方案5】:

      如果您使用的是 kotlin DSL,请将其添加到 build.gradle.kts 文件中:

      dependencies {
          ....
          implementation(project(":your-library-module"))
      }
      

      【讨论】:

        猜你喜欢
        • 2014-10-06
        • 1970-01-01
        • 1970-01-01
        • 2013-07-27
        • 1970-01-01
        • 2021-10-05
        • 1970-01-01
        • 2019-04-11
        • 2017-12-15
        相关资源
        最近更新 更多