【问题标题】:Android Studio - Adding SDK versions and dependencies to all module [duplicate]Android Studio - 将 SDK 版本和依赖项添加到所有模块 [重复]
【发布时间】:2017-11-06 07:42:49
【问题描述】:

在我当前工作的项目中包含很多模块和依赖项。那么有没有办法让所有这些版本和依赖项保持通用并在所有模块中重用。我知道我们可以在根 build.gradle 文件中定义公共依赖关系,但是关于 compileSdkVersions 和排除组之类的东西。 我的依赖有时包括排除组之类的。

  androidTestCompile ('com.android.support.test:rules:1.0.1'){
    exclude group: 'com.android.support', module: 'support-annotations'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude module: 'recyclerview-v7'
}

我们如何处理这种情况? 一旦我们添加了根 build.gradle,有没有办法将它们全部添加到应用程序模块中,而无需指定如下所示的单个模块。

compile deps.cardview
compile deps.design
compile deps.supportv4
compile deps.animation
compile deps.pagination
compile deps.shimmerlayout
compile deps.enhanced_card
compile deps.swipeanim
compile deps.appcompact

【问题讨论】:

  • 使用排除组添加依赖项怎么样。

标签: android android-studio gradle


【解决方案1】:

对于像 compileSdkVersion、buildTypes 和 compileOptions 这样的东西,我在根 gradle 文件中这样定义:

ext.android_settings_for_module = {
    compileSdkVersion COMPIlE_SDK_VERSION.toInteger()
    buildToolsVersion BUILD_TOOLS_VERSION

    defaultConfig {
        minSdkVersion 
        targetSdkVersion 
        versionCode 
        versionName 
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
    }
    compileOptions compile_options
    lintOptions lint_options
    testOptions test_options
}

ext.lint_options = {
    //butterKnife
    disable 'InvalidPackage'
}

ext.compile_options = {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

然后,在您的模块文件中,您可以使用:

android android_settings_for_module

依赖关系非常相似。在根 gradle 文件中定义一个字段:

ext.common_libs = [

]

然后在模块级的gradle文件中使用:

dependencies {
    compile common_libs
}

【讨论】:

  • 使用排除组添加依赖项怎么样。
【解决方案2】:

用于共享通用 SDK 版本和依赖项。您可以在 library 模块中定义 shared gradle dependencies,如果 app 模块将 library 作为依赖项,则无需指定所有内容两次。更进一步,您可以创建一个需要共享 gradle 依赖项的 'common' 模块,并让 app 和 library 模块都需要 common 模块。

看看:

// Top-level build file where you can add configuration options common to 
all sub-projects/modules.
buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.2.3'
}
}

// Load dependencies
apply from: 'dependencies.gradle'

这里是 dependencies.gradle:适用于应用程序中的所有模块和 sdk 版本。

ext {
//Version
supportLibrary = '22.2.1'

//Support Libraries dependencies
supportDependencies = [
        design           :         "com.android.support:design:${supportLibrary}",
        recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
        cardView         :         "com.android.support:cardview-v7:${supportLibrary}",
        appCompat        :         "com.android.support:appcompat-v7:${supportLibrary}",
        supportAnnotation:         "com.android.support:support-annotations:${supportLibrary}",
]
}

编码愉快!!

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 2019-04-11
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    相关资源
    最近更新 更多