繁体   English   中英

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

[英]How to include additional configuration file inside build.gradle file

是否可以在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'

}

您可以包括一个外部构建脚本。 查看官方指南

只需使用:

apply from: 'signing.gradle'

我做了这样的事情。

在我的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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM