如何在build.gradle文件中包含其他配置文件

时间:2015-07-10 08:22:11

标签: android gradle android-gradle build.gradle

是否有能力在build.gradle文件中包含文件?
我想将几个项目中的相同配置分成一个文件,而不是将它包含在build.gradle中 例如,现在我有这样的文件:

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

android {
    compileSdkVersion 15
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 15
        targetSdkVersion 22
    }

    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            println outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                def manifestParser = new com.android.builder.core.DefaultManifestParser()
                def version = manifestParser.getVersionName(android.sourceSets.main.manifest.srcFile)
                def fileName = outputFile.name.replace('.apk', "-${version}.apk")
                println fileName
                output.outputFile = new File(outputFile.parent, fileName)
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }

    lintOptions {
        abortOnError false
    }

    //Signing app
    if(project.hasProperty("debugSigningPropertiesPath") && project.hasProperty("releaseSigningPropertiesPath")) {

        File debugPropsFile = new File(System.getenv('HOME') +  "/" + project.property("debugSigningPropertiesPath"))
        File releasePropsFile = new File(System.getenv('HOME') +  "/" + project.property("releaseSigningPropertiesPath"))

        if(debugPropsFile.exists() && releasePropsFile.exists()) {
            Properties debugProps = new Properties()
            debugProps.load(new FileInputStream(debugPropsFile))

            Properties releaseProps = new Properties()
            releaseProps.load(new FileInputStream(releasePropsFile))

            signingConfigs {
                debug {
                    storeFile file(debugPropsFile.getParent() + "/" + debugProps['keystore'])
                    storePassword debugProps['keystore.password']
                    keyAlias debugProps['keyAlias']
                    keyPassword debugProps['keyPassword']
                }
                release {
                    storeFile file(releasePropsFile.getParent() + "/" + releaseProps['keystore'])
                    storePassword releaseProps['keystore.password']
                    keyAlias releaseProps['keyAlias']
                    keyPassword releaseProps['keyPassword']
                }
            }
            buildTypes {
                debug {
                    signingConfig signingConfigs.debug
                }
                release {
                    signingConfig signingConfigs.release
                }
            }
        }
    }

}

我想简化这样的事情

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

android {
    compileSdkVersion 15
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 15
        targetSdkVersion 22
    }

    include 'applicationVariants.gradle'

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    packagingOptions {
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'
    }

    lintOptions {
        abortOnError false
    }

    include 'signing.gradle'

}

2 个答案:

答案 0 :(得分:3)

您可以包含外部构建脚本。查看official guide

只需使用:

apply from: 'signing.gradle'

答案 1 :(得分:1)

我已经完成了这样的事情。

在我的libraries.gradle

ext { 
//Android
targetSdkVersion = 22;
compileSdkVersion = 22;
buildToolsVersion = '22.0.1'
.... 
dagger2Version = '2.0'
butterknifeVersion = '6.1.0'
appCompatVersion = '22.2.0'
designVersion = '22.2.0'
recyclerViewVersion = '22.2.0'=

libraries = [
        supportAnnotations: "com.android.support:support-annotations:${androidSupportAnnotationsVersion}",
        googlePlayServices: "com.google.android.gms:play-services:${googlePlayServicesVersion}",
        recyclerView         : "com.android.support:recyclerview-v7:${recyclerViewVersion}",
        picasso              : "com.squareup.picasso:picasso:${picassoVersion}",
        cardView             : "com.android.support:cardview-v7:${cardViewVersion}",
        appCompat            : "com.android.support:appcompat-v7:${appCompatVersion}",
        design               : "com.android.support:design:${designVersion}",
        findBugs             : "com.google.code.findbugs:jsr305:${findbugsVersion}",
        gson                 : "com.google.code.gson:gson:${gsonVersion}",
        flow                 : "com.squareup.flow:flow:${flowVersion}",
        butterknife          : "com.jakewharton:butterknife:${butterknifeVersion}",
        rxjava               : "io.reactivex:rxjava:${rxjavaVersion}",
        rxandroid            : "io.reactivex:rxandroid:${rxandroidVersion}",
        androidSupport       : "com.android.support:support-v13:${androidSupportVersion}",
        androidSupportV4: "com.android.support:support-v4:${androidSupportVersion}",
        javaxInject          : "javax.inject:javax.inject:${javaxInjectVersion}",
        retrofit             : "com.squareup.retrofit:retrofit:${retrofitVersion}",codec:${commonsCodecVersion}","com.facebook.stetho:stetho:${stethoVersion}",
        apache               : "org.apache.commons:commons-lang3:${apacheVersion}",
        libPhoneNumber      : "com.googlecode.libphonenumber:libphonenumber:$libPhoneNumber",
        dagger2           : "com.google.dagger:dagger:${dagger2Version}",
        dagger2Compiler   : "com.google.dagger:dagger-compiler:${dagger2Version}",
        javaxAnnotations  : "javax.annotation:javax.annotation-api:${javaxAnnotationVersion}"
]
}

在我的app.gradle

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':logic')
compile project(':local-resources')
compile project(':api')
compile rootProject.ext.libraries.appCompat
compile libraries.design
compile rootProject.ext.libraries.recyclerView
compile rootProject.ext.libraries.butterknife
compile rootProject.ext.libraries.dagger2
compile libraries.rxandroid
compile libraries.stetho
compile libraries.cardView
compile libraries.picasso
apt rootProject.ext.libraries.dagger2Compiler

或从以下申请:rootProject.file('checkstyle.gradle')