不一样的build.gradle

作者: CokeNello | 来源:发表于2017-10-22 09:56 被阅读75次

0. Thanks

build.gradle App重命名
Gradle多渠道多环境打包自动重命名
看不懂的 build.gradle 代码

1.常见写法解释

apply plugin: 'com.android.application'
//Model都有各自的build.gradle,这里声明该Model作为主项目,常见的还有另一个取值:
//apply plugin: 'com.android.library' 声明该Model作为库使用,当然还有其他取值,后面博客会介绍
apply plugin: 'me.tatarka.retrolambda'

//这里是groovy的代码了,定义了一个获取时间的方法,groovy是兼容java,它可以直接使用jdk里的方法
def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

android {
    //你SDK的版本号,也就是API Level,例如API-19、API-20、API-21等等。
    compileSdkVersion 25
    //你构建工具的版本,其中包括了打包工具aapt、dx等等。
    buildToolsVersion "25.0.1"

    //基本设置
    defaultConfig {
        applicationId "com.hy.HoneyMarket"  //包名
        minSdkVersion 19            //最小的SDK版本
        targetSdkVersion 25         //目标SDK版本
        versionCode 6               //内部版本号
        versionName "1.0.8"         //对外版本号
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        // dex突破65535的限制
        multiDexEnabled true
    }

    //路径设置,一般不需要设置,使用默认就好
    sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/resources']
            aidl.srcDirs = ['src/main/aidl']
            renderscript.srcDirs = ['src/maom']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            jniLibs.srcDir 'src/main/jniLibs'
        }
        instrumentTest.setRoot('tests')
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }

    //执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉。
    //Lint: 静态代码分析
    lintOptions {
        abortOnError false
    }

    //签名:如下的配置,其签名放在:app/下
    signingConfigs {
        debug {
            keyAlias '名称'
            keyPassword '密码'
            storeFile file('ChestnutPlus.jks')
            storePassword "密码"
        }
        release {
            keyAlias '名称'
            keyPassword '密码'
            storeFile file('ChestnutPlus.jks')
            storePassword "密码"
        }
    }

    //编译设置
    buildTypes {
        //Debug模式下的设置:
        debug {
            //设置Log日志,true为开启
            buildConfigField "boolean", "LOG_DEBUG", "true" 
            //混淆关闭
            minifyEnabled false
            //Zipalign优化
            zipAlignEnabled true
            //是否去除重复的Res
            shrinkResources false
            //配置混淆文件
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            buildConfigField "boolean", "LOG_DEBUG", "false"
            minifyEnabled true
            zipAlignEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            //配置release版的名称
            android.applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith(".apk")) {
                        def version= "HoneyApp-v${defaultConfig.versionName}"
                        def time = "-${releaseTime()}.apk"
                        output.outputFile = new File(outputFile.parent, version+time)
                    }
                }
            }
        }
    }
    //声明使用的是JDK1.8
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    //声明使用SVG图片
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
    //仓库声明
    repositories {
        flatDir {
            dirs 'libs'
            //当其他的Module中有aar的时候,需要如下的声明:
            //  QrCode是你的库名字
            dirs project(':QrCode').file('libs')
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.android.support:design:25.1.1'
    testCompile 'junit:junit:4.12'
    //RX
    compile 'com.trello:rxlifecycle:0.8.0'
    compile 'com.trello:rxlifecycle-components:0.8.0'
    //注解框架
    compile files('libs/ButterKnife/butterknife-7.0.1.jar')
    //Json
    compile files('libs/Json/gson-2.2.4.jar')
    //下载框架
    compile 'com.liulishuo.filedownloader:library:1.4.1'
    //图片加载框架
    compile files('libs/Glide/glide-3.7.0.jar')
    //下拉刷新
    compile(name: 'RefreshLayout/MaterialRefreshLayout', ext: 'aar')
    //DB
    compile files('libs/DB/lite-orm-1.9.1.jar')
    //Bugly
    compile 'com.tencent.bugly:crashreport:latest.release'
}

代码中已经有注释

2. 全局配置

1.png

当你的项目中有多个Module的时候,而每一个Module下,你的Gradle都有如下的代码:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0.2"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.bm.photoview:library:1.4.1'
}

其中,像compileSdkVersionbuildToolsVersion,等等,每个模块里面都有,这是编译工具的版本,和其他一下SDK的版本等等,
有时候,编译出错就是因为版本的不统一所导致的,那么,我们可以把这些都集合其他,统一到一个配置文件中。

  • 1)在项目下,新建一个:config.gradle
ext {
   android = [
           compileSdkVersion   :   25,
           buildToolsVersion   :   "25.0.3",
           minSdkVersion       :   19,
           targetSdkVersion    :   25,
           mainVersionCode     :   4,
           mainVersionName     :   "1.2.1",
   ]

   dependencies = [
           supportV7           :   'com.android.support:appcompat-v7:25.3.1',
           design              :   'com.android.support:design:25.3.1',
   ]
}

这里,声明了一些通用的配置。

  • 2)项目build.gradle
apply from: "config.gradle"
buildscript {
   repositories {
       jcenter()
   }
   dependencies {
       classpath 'com.android.tools.build:gradle:2.3.3'
       classpath 'me.tatarka:gradle-retrolambda:3.2.3'
   }
}
allprojects {
   repositories {
       jcenter()
       maven { url 'https://jitpack.io' }
   }
}
task clean(type: Delete) {
   delete rootProject.buildDir
}

主要是:apply from: "config.gradle"

  • 3)模块的配置就变成这样:
apply plugin: 'com.android.library'
def configInfo = rootProject.ext.android;
def library = rootProject.ext.dependencies;
android {
   compileSdkVersion configInfo.compileSdkVersion
   buildToolsVersion configInfo.buildToolsVersion
   defaultConfig {
       minSdkVersion configInfo.minSdkVersion
       targetSdkVersion configInfo.targetSdkVersion
       versionCode 2
       versionName "1.0"
   }
   buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
   }
}
dependencies {
   compile library.supportV7
   compile library.common
}
  • 4)app模块就变成这样
apply plugin: 'com.android.application'
def configInfo = rootProject.ext.android;
def library = rootProject.ext.dependencies;
static def releaseTime() {
   return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android {
   compileSdkVersion configInfo.compileSdkVersion
   buildToolsVersion configInfo.buildToolsVersion
   defaultConfig {
       multiDexEnabled true
       applicationId "com.huiyu.honeybot.honeybotapplication"
       minSdkVersion configInfo.minSdkVersion
       targetSdkVersion configInfo.targetSdkVersion
       versionCode configInfo.mainVersionCode
       versionName configInfo.mainVersionName
       resConfigs "zh", "en"
   }
   signingConfigs {
       debug {
           keyAlias ''
           keyPassword ''
           storeFile file('ChestnutPlus.jks')
           storePassword ""
       }
       release {
           keyAlias ''
           keyPassword ''
           storeFile file('ChestnutPlus.jks')
           storePassword ""
       }
   }
   buildTypes {
       debug {
           buildConfigField "boolean", "LOG_DEBUG", "true"
           minifyEnabled false
           zipAlignEnabled true
           shrinkResources false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
       release {
           buildConfigField "boolean", "LOG_DEBUG", "false"
           minifyEnabled true
           zipAlignEnabled true
           shrinkResources true
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
           android.applicationVariants.all { variant ->
               variant.outputs.each { output ->
                   def outputFile = output.outputFile
                   if (outputFile != null && outputFile.name.endsWith(".apk")) {
                       def version= "HoneyApp-v${defaultConfig.versionName}"
                       def time = "-${releaseTime()}.apk"
                       output.outputFile = new File(outputFile.parent, version+time)
                   }
               }
           }
       }
   }
   compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
   }
   repositories {
       flatDir {
           dirs 'libs'
           dirs project(':QrCode').file('libs')
       }
   }
   dependencies {
       //阿里百川
       compile(name: 'Feedback/alisdk-feedback-1.2.0', ext: 'aar')
       compile(name: 'Feedback/securityguardaar3-5.1.81', ext: 'aar')
       compile files('libs/Feedback/app-monitor-sdk-2.5.1.1_for_bc_proguard.jar')
       compile files('libs/Feedback/utdid4all-1.1.5.3_proguard.jar')
       //友盟
       compile files('libs/UMeng/umeng-analytics-v6.0.1.jar')
       //代码库
       compile project(path: ':Dialog')
       compile project(path: ':Web')
       compile project(path: ':QrCode')
       compile project(path: ':SoundTouch')
       compile project(path: ':Media')
       compile project(path: ':PhotoView')
       //微信
       compile files('libs/WeiChat/wechat-sdk-android-without-mta-1.0.2.jar')
       //环信
       compile 'com.hyphenate:hyphenate-sdk:3.3.0'
       //解决64K,Android
       compile 'com.android.support:multidex:1.0.1'
       compile library.supportV7
       compile library.design
   }
}

相关文章

网友评论

    本文标题:不一样的build.gradle

    本文链接:https://www.haomeiwen.com/subject/valguxtx.html