Gradle节点解释

848 阅读2分钟
gradle使用groovy语言(类似于java的动态语言)
顶层gradle文件
// org.gradle.api.Project看源码有哪些配置项
buildscript { 
// 基础构建脚本
        repositories { //远程仓库地址-dependencies依赖库必须有对应的远程仓库地址
        maven { // 私有仓库
            credentials { // 仓库认证信息
                username "deployment"
                password "deployment123"
            }
            url "http://10.4.41.99:8081/nexus/content/repositories/thirdparty/"
        }
        jcenter() //google基础仓库包含mavenCentral
        maven { url 'https://dl.google.com/dl/android/maven2/' }
        maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven { url 'https://maven.aliyun.com/repository/google' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.0.1'
    }
}
allprojects { //子工程通用脚本
    repositories {
        maven {
            credentials {
                username "deployment"
                password "deployment123"
            }
            url "http://10.4.41.99:8081/nexus/content/repositories/thirdparty/"
        }
        jcenter()
        maven {
          url 'https://dl.google.com/dl/android/maven2/'
        }
        maven{url 'http://maven.aliyun.com/nexus/content/groups/public/'}
        maven { url 'https://maven.aliyun.com/repository/google' }
    }
    configurations.all { // 遍历configurations配置项
        resolutionStrategy { // 解析策略 过滤 或强制使用制定版本的库
            resolutionStrategy.eachDependency { details ->
                if (details.requested.group == 'androidx.core') {
                    details.useVersion "1.0.0"
                }
            }
        }
        resolutionStrategy.force "androidx.appcompat:appcompat:1.0.1"
        resolutionStrategy.force "androidx.recyclerview:recyclerview:1.0.0-rc01"
        resolutionStrategy.force "androidx.licle:lifecycle-livedata:2.0.0"
        resolutionStrategy.force "androidx.versionedparcelable:versionedparcelable:1.1.0"
        resolutionStrategy.force "com.squareup.okhttp3:okhttp:3.12.1"
        resolutionStrategy.force "com.squareup.okio:okio:2.6.0"
    }
}
rootProject.buildDir = '../build' 
// 设置应用编译生成路径subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}" // 设置子工程编译生成路径
}
subprojects {
    project.evaluationDependsOn(':app') // 执行子工程任务时自动先执行:app任务
}
task clean(type: Delete) {
    delete rootProject.buildDir // 清空编译目录
}
ext { // 扩展常量
    dependencies = [ gson : 'com.google.code.gson:gson:2.8.5']
}

----------------子工程gradle文件apply plugin: 'com.android.application'  // 工程引入插件
apply plugin: 'org.grobot.greendao'

android {  // 工程配置 com.android.build.gradle.BaseExtension查看还有那些配置项
    compileSdkVersion rootProject.ext.android.compileSdkVersion // 编译Sdk版本
    buildToolsVersion rootProject.ext.android.buildToolsVersion // 编译工具版本
    defaultConfig {
        applicationId "com.to8to.rabbitcircle" // apk包名覆盖manifest pageName
        minSdkVersion rootProject.ext.android.minSdkVersion // 支持最低版本sdk
        targetSdkVersion rootProject.ext.android.targetSdkVersion // 目标b
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName
        javaCompileOptions { // java代码编译选项
            annotationProcessorOptions { // 动态注解
                arguments = [moduleName: project.getName()] // 添加新参数
            }
        }
        vectorDrawables.useSupportLibrary = true
        
        resConfigs "hdpi","xxhdpi","en","cn" // 指定密集度图片、语言类型 降低apk大小
        ndk {
            abiFilters "armeabi", "armeabi-v7a" // 指定动态链接库 降低apk大小
        }
    }
    compileOptions {
        sourceCompatibility 1.8 // 指定java编译环境版本
        targetCompatibility 1.8
    }
    lintOptions {
        checkReleaseBuilds false        // Or, if you pre you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false // 代码构建忽略布局中的错误,如自定义控件不存在    }
    signingConfigs {  // 签名配置
        myConfig {
            storeFile file("xxx")
            storePassword "xxx"
            keyAlias "xxx"
            keyPassword "xxx"
        }
    }
    dexOptions {  // dex打包配置
        incremental true // 是否增量
        javaMaxHeapSize "4g" // 打包运行 堆内存 理论上越大打包越快
        preDexLibraries = false // 预加载已有的库
    }
    sourceSets { // 设置资源路径
        main {  // 未设置则为默认路径 jni默认路径为src/main/jniLibs
         manifest.srcFile 'src/main/AndroidManifest.xml'
         java.srcDirs = ['src/main/java']
         resources.srcDirs = ['src/main/java']
         renderscript.srcDirs = ['src/main/java']
         res.srcDirs = ['src/main/res']
         assets.srcDirs = ['src/main/assets']
        }
    }
}
compileOptions {
      sourceCompatibility = 1.8 //jdk编译版本
      targetCompatibility = 1.8 // 打包版本
    }
    buildTypes {
        debug {
            shrinkResources false // 是否打包未使用的资源res 跟minifyEnable同时使用才生效
            minifyEnabled false // 是否混淆压缩优化代码 
            multiDexEnabled true // 是否分包
            signingConfig signingConfigs.myConfig
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            shrinkResources false
            minifyEnabled false
            multiDexEnabled true
            signingConfig signingConfigs.myConfig
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    // 自定义打包 打多渠道包
 productFlavors {
  to8to {
    name=baiduapp,
    dimension=null,
    minSdkVersion=null,
    targetSdkVersion=null,
    renderscriptTargetApi=null,
    renderscriptSupportModeEnabled=null,
    renderscriptSupportModeBlasEnabled=null,
    renderscriptNdkModeEnabled=null,
    versionCode=null,
    versionName=null,
    applicationId=null,
    testApplicationId=null,
    testInstrumentationRunner=null,
    testInstrumentationRunnerArguments={},
    testHandleProfiling=null,
    testFunctionalTest=null,
    signingConfig=null,
    resConfig=null,
    mBuildConfigFields={},
    mResVals={},
    mProguardFiles=[],
    mConsumerProguardFiles=[],
    mManistPlaceholders={UMENG_CHANNEL_VALUE=土巴兔}, //中文描述
    mWearAppUnbundled=null
  }
 }
  productFlavors.all { flavor ->
      	flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] // 替换成name to8to
      }
}
dependencies {
    def roomVersion = "2.2.0" // 定义变量
    api fileTree(include: ['*.jar','*.aar'], dir: 'libs')    api 'com.sun.mail:android:($roomVersion).aar' // 不导入内部依赖(如该aar中引入gson,但module工程不会自动导入gson)
    api 'com.sun.mail:android:$roomVersion' // 导入内部依赖
    api ('com.sun.mail:android:$roomVersion', {
      exclude module:'okhttp'  // 过滤包含okhttp的库
    }) 
    // 传递依赖(类似于public)
    // debug 模式执行
    // release 模式执行
    api 'com.sun.mail:android:$roomVersion'
    debugApi ...
    releaseApi ...

    // 非传递依赖(类似于private) 同时阻断内部引用库的传递依赖
    implementation ...
    debugImplementation ...
    releaseImplementation ...

    // 不打包进module中(module被引用时需要手动导入该库)
    compileOnly ...
    debugCompileOnly ...
    releaseCompileOnly ...
    implementation(rootProject.ext.to8to.filepreview, {
       exclude module: "okhttp"       
       exclude module: "okio"
    })    
    annotationProcessor "androidx.licle:lifecycle-compiler:$archLifecycleVersion"
}