簡體   English   中英

如何在android studio Gradle中將libs依賴項從項目添加到模塊

[英]how to add libs dependency from project to module in android studio Gradle

我創建了一個 android 項目,它的名字是一個示例應用程序我還在示例應用程序項目中創建了一個庫模塊。 示例應用程序依賴於其他庫,因此我在示例應用程序的 libs 文件夾中添加了所需的 jar 文件。 模塊內部也需要相同的 jar 文件。

如何為模塊添加依賴項以引用示例應用程序的 libs 文件夾? 我在下面嘗試過這個,但每次都給出一個重復的副本文件異常。

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

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

將此添加到您的應用程序的 build.gradle 中:

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

為了在 android studio 中添加 Module 依賴項,請按照以下說明操作:

您應該將您的庫模塊放在應用程序項目中。 要指定模塊依賴項,只需:

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

在應用級別 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')

    }

如果您希望庫 (*.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')
    // ...
}

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM