Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在build.gradle文件中包含其他配置文件_Android_Gradle_Android Gradle Plugin_Build.gradle - Fatal编程技术网

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

Android 如何在build.gradle文件中包含其他配置文件,android,gradle,android-gradle-plugin,build.gradle,Android,Gradle,Android Gradle Plugin,Build.gradle,是否可以在build.gradle文件中包含文件? 我想将几个项目中相同的配置分离到一个文件中,而不仅仅是将其包含在build.gradle中。 例如,现在我有如下文件: apply plugin: 'com.android.application' apply plugin: 'io.fabric' android { compileSdkVersion 15 buildToolsVersion "22.0.1" defaultConfig { a

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

您可以包含外部生成脚本。检查:

只需使用:

apply from: 'signing.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}"
]
}
在我的应用程序中

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')

我做过类似的事情

在我的图书馆里

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}"
]
}
在我的应用程序中

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')

senir developer的赞赏::-)接受感谢..:-p来自senir developer的沉淀。:-)接受感谢..:-P