2015-07-10 187 views
1

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

} 

回答

3

可以包括外部構建腳本。檢查official guide

只需使用:

apply from: 'signing.gradle' 
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')

+0

有趣,感謝名單 – cooperok

+0

升值來自senir開發者。 :-)讚賞接受.. :-P – abhishek

相關問題