如何为所有子项目/模块应用依赖项Gradle Android Studio

时间:2015-06-06 12:45:27

标签: android gradle android-gradle maven-dependency-plugin

我是gradle脚本的新手,只有基本知识 我的项目中有多个模块。这是截图 enter image description here

在我的项目build.gradle文件中,我有基本配置 据我所知,这个文件是用于全局项目配置。

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven { url "http://dl.bintray.com/populov/maven" }
        jcenter()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }

    }
}

我的所有模块build.grade文件都有不同的依赖关系。例如。

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleDependency
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.3'
    compile 'com.etsy.android.grid:library:1.0.5'
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'
    compile 'com.fasterxml.jackson.core:jackson-core:2.5.3'
    compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.3'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.github.ksoichiro:android-observablescrollview:1.5.1'
    compile 'com.viewpagerindicator:library:2.4.1@aar'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
    compile('com.mikepenz.materialdrawer:library:2.9.7@aar') {
        transitive = true
    }
    compile project(':horizontalrecyclerview')
    compile project(':ultimaterecyclerview')
    compile 'org.lucasr.twowayview:core:1.0.0-SNAPSHOT@aar'
    compile 'org.lucasr.twowayview:layouts:1.0.0-SNAPSHOT@aar'
    compile 'com.yqritc:recyclerview-flexibledivider:1.2.4'
    compile 'de.greenrobot:eventbus:2.4.0'
    compile 'com.malinskiy:superrecyclerview:1.1.0'
    compile 'com.android.support:support-v4:22.2.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:recyclerview-v7:22.1.1'
}

有什么问题?我的一些模块需要相同的库依赖项,或者所有模块都需要相同的库。当我向每个模块build.gradle文件添加依赖项时,我会在项目构建时遇到下一个错误。

Error:Execution failed for task ':app:processDebugResources'.
> Error: more than one library with package name 'com.mikepenz.iconics'
  You can temporarily disable this error with android.enforceUniquePackageName=false
  However, this is temporary and will be enforced in 1.0

我该如何解决这个问题?将依赖项添加到根build.gradle文件或其他解决方案中 或者只是按照建议并设置

android.enforceUniquePackageName=false

1 个答案:

答案 0 :(得分:0)

有多个存档使用相同的包(通过在其中包含类或在AndroidManifest.xml中。我不确定)。这是档案开发人员犯的错误。

如果您将以下任务添加到构建脚本并执行:lib-that-has-error:extractCompileDependencies,您将在build/compileDependencies中找到提取的依赖项。

解决此问题:

  • 首先确保使用最新版本的依赖项。也许这个问题已经被报道并修复了。
  • 如果最新版本的问题仍然存在,请检查哪些提取的依赖项包含相同的包。请这些依赖项的开发人员解决问题。
def compileDependenciesBuildDir = new File(buildDir, 'compileDependencies')

task copyCompileDependencies(type: Copy) {
    from configurations.compile
    into compileDependenciesBuildDir
}

task extractCompileDependencies() {
    inputs.files(copyCompileDependencies)

    doLast {
        compileDependenciesBuildDir.listFiles(new FileFilter() {
            @Override
            boolean accept(File pathname) {
                return pathname.isFile()
            }
        }).each {
            def unzippedDirectory = new File(it.parent, it.name + '-unzipped')
            ant.unzip(src: it, dest: unzippedDirectory, overwrite: 'true')
            def classesJarFile = new File(unzippedDirectory, 'classes.jar')
            if (classesJarFile.exists()) {
                ant.unzip(src: classesJarFile, dest: new File(unzippedDirectory, 'classes'), overwrite: 'true')
            }
        }
    }
}