repositories
- buildscript {
- repositories {
- //国内可以使用镜像阿里仓库
- maven { url 'https://maven.aliyun.com/repository/google' }
- maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
- maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
- // ---------------
- jcenter()//由JFrog公司提供的Bintray中的Java仓库。它是当前世界上最大的Java和Android开源软件构件仓库。代替 mavenCentral()
- google()//google代码仓库
- maven {
- credentials {
- username 'dxxxxxx'
- password 'password'
- }
- url 'http://xxxxxxxx.com/repository/maven-public/'
- allowInsecureProtocol(true)
- }
- }
- }
- dependencies {
- //使用的gradle:7.0.4版本号,
- classpath "com.android.tools.build:gradle:7.0.4"
- //alibaba arouter版本
- classpath "com.alibaba:arouter-register:1.0.2"
- //kotlin
- classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
- classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
- classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
- }
加载基本插件
- plugins {
- id 'com.android.application'
- id 'kotlin-android'
- id 'kotlin-kapt'
- id 'dagger.hilt.android.plugin'
- }
-
- apply plugin: 'com.xxxx.xxxx.xxxx'
常用:基础配置项
1、Manifest entries (Manifest属性)
2、Build Types(构建类型)
3、signing configurations(签名配置)
4、Running Proguard(运行 Proguard)
- android {
- compileSdkVersion 19
- buildToolsVersion "19.0.0"
-
- defaultConfig {
- versionCode 12
- versionName 1.0.0
- minSdkVersion 16
- targetSdkVersion 16
- }
- }
- buildTypes {
-
- //自定义string buildconfig
- buildConfigField "String", "LAUNCHER_IMAGE_URL_TEST", "\"" + rootProject.api["launcher_image_url_test"] + "\""
-
- //build debug配置
- debug {
- versionNameSuffix "-debug"
- //
- zipAlignEnabled false
- // 移除无用的resource文件
- shrinkResources false
- // 是否进行混淆
- minifyEnabled false
- signingConfig signingConfigs.release
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- // DEBUG 模式,输出日志
- debuggable true
- jniDebuggable true
- }
- //build release 配置
- release {
- zipAlignEnabled true
- // 移除无用的resource文件
- shrinkResources true
- // 是否进行混淆
- minifyEnabled true
- // 混淆
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- signingConfig signingConfigs.release
- }
- //自定义环境
- 'mydev' {
- //测试环境的渠道
- buildConfigField "String", "LAUNCHER_IMAGE_URL", "\"" + rootProject.wtvdev_api["launcher_image_url"] + "\""
-
- versionNameSuffix "-mydev"
- //
- zipAlignEnabled false
- // 移除无用的resource文件
- shrinkResources false
- // 是否进行混淆
- minifyEnabled false
- signingConfig signingConfigs.release
- proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
- debuggable true
- jniDebuggable true
- }
- }
- android {
- signingConfigs {
- debug {
- storeFile file("debug.keystore")
- }
-
- myConfig {
- storeFile file("../other.keystore") // 加载签名文件
- storePassword "android" //密码
- keyAlias "androiddebugkey"
- keyPassword "android"
- v1SigningEnabled true
- v2SigningEnabled true
- }
- }
- }
- // 多渠道打包 根据不同的productflavors加载不同的变量,如ENVIRONMENT_VALUE-test或用release
- // Specifies one flavor dimension.
- flavorDimensions "version"
- productFlavors {
-
- mytest {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "mytest",
- ENVIRONMENT_VALUE: "TEST"
- ]
- }
-
- store_xiaomi {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "store_xiaomi",
- ENVIRONMENT_VALUE: "RELEASE"
- ]
- }
- store_vivo {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "store_vivo",
- ENVIRONMENT_VALUE: "RELEASE"
- ]
- }
- store_huawei {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "store_huawei",
- ENVIRONMENT_VALUE: "RELEASE"
- ]
- }
- store_oppo {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "store_oppo",
- ENVIRONMENT_VALUE: "RELEASE"
- ]
- }
- store_yingyongbao {
- dimension = "version"
- manifestPlaceholders = [
- CHANNEL_VALUE : "store_yingyongbao",
- ENVIRONMENT_VALUE: "RELEASE"
- ]
- }
- }
TASK
- channel {
- channelFile = file("app_channel.txt")
- //多渠道包的输出目录,默认为new File(project.buildDir,"channel")
- outputDir = new File(project.buildDir, "channels")
- //多渠道包的命名规则,默认为:${appName}-${versionName}-${versionCode}-${flavorName}-${buildType}-${buildTime}
- apkNameFormat = '${appName}-${versionName}-${flavorName}-${buildType}'
- //快速模式:生成渠道包时不进行校验(速度可以提升10倍以上,默认为false)
- fastMode = false
- //buildTime的时间格式,默认格式:yyyyMMdd-HHmmss
- buildTimeDateFormat = 'yyyyMMdd-HH:mm:ss'
- //低内存模式(仅针对V2签名,默认为false):只把签名块、中央目录和EOCD读取到内存,不把最大头的内容块读取到内存,在手机上合成APK时,可以使用该模式
- lowMemory = false
- }
-
- -----------------------------------------------------------------------------------------
- def startBuildTime, endBuildTime
- task publish {
- doFirst {
- startBuildTime = System.currentTimeMillis()
- }
- //依赖channelRelease的task,也就是channelRelease执行完再执行publish继续往下走
- dependsOn("channelRelease")
- doLast {
- endBuildTime = System.currentTimeMillis()
- println "the build time is: ${endBuildTime - startBuildTime}"
- println "publish success!"
- }
- }
- -------------------------------------------------------------------------
- //清理 task
- task clean(type: Delete) {
- delete rootProject.buildDir
- }
dependencies 加载依赖
- implementation 'com.tencent.mm.opensdk:wechat-sdk-android:+'
-
- api rootProject.depsLibs.retrofit
-
- debugImplementation 'com.github.eleme.UETool:uetool:1.3.2'
- debugImplementation 'com.github.eleme.UETool:uetool-base:1.3.2'
- releaseImplementation 'com.github.eleme.UETool:uetool-no-op:1.3.2'
-
-
- compileOnly "com.tencent.mm.opensdk:wechat-sdk-android:+"
两个关键字的具体区别就很明显,api关键字以及compile关 键字引用的依赖包对于其他模块来说是可见的,可传递性的,而使用implementation关键字引用的依赖包对于其他模块来说是不可见的,无法将此依赖传递下去。
| 名称 | 说明 |
| compileOnly | gradle2.12之后版本新添加的,2.12版本时期曾短暂的叫provided,后续版本已经改成了compileOnly,由java插件提供,适用于编译期需要而不需要打包的情况 |
| providedCompile | war插件提供的范围类型:与compile作用类似,但不会被添加到最终的war包中这是由于编译、测试阶段代码需要依赖此类jar包,而运行阶段容器已经提供了相应的支持,所以无需将这些文件打入到war包中了;例如Servlet API就是一个很明显的例子. |
| api | 3.4以后由java-library提供 当其他模块依赖于此模块时,此模块使用api声明的依赖包是可以被其他模块使用 |
| implementation | 3.4以后由java-library提供 当其他模块依赖此模块时,此模块使用implementation声明的依赖包只限于模块内部使用,不允许其他模块使用。 |
| compile | 编译范围依赖在所有的classpath中可用,同时它们也会被打包。 |
| providedRuntime | 同proiveCompile类似。 |
| runtime | runtime依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC驱动实现。 |
| testCompile | 测试期编译需要的附加依赖 |
| testRuntime | 测试运行期需要 |
| archives | - |
| default | 配置默认依赖范围 |
- api (rootProject.ext.dependencies["retrofit2-retrofit"]){
- exclude group:'com.squareup.okhttp3',module:'okhttp' //剔除okhttp
- }
ext 定义扩展程序
- rootProject.ext.androidx["compileSdkVersion"]
-
-
- ext {
- /* android sdk的使用版本配置
- * app 版本配置 */
- android = [
- applicationId : "com.xxxxx",
- compileSdkVersion: 31,
- buildToolsVersion: "30.0.2",
- minSdkVersion : 21,
- targetSdkVersion : 28,
- versionCode : 113,
- versionName : "1.1.3"
- ]
-
- /**
- * 项目使用到的常量
- */
- constants = [
- ]
- }
maven仓库上传
- apply plugin: 'maven'
- uploadArchives {
- repositories {
- mavenDeployer {
- pom.artifactId = '项目信息'
- pom.version = '版本信息'
- repository(url: '私服仓库地址') {
- authentication(userName: '账号', password: '密码')
- }
- snapshotRepository(url: '私服快照地址') {
- authentication(userName: '账号', password: '密码')
- }
- }
- }
- }
sourceSets
可以指定哪些源文件(或文件夹下的源文件)要被编译,哪些源文件要被排除
- sourceSets {
- main {
- jniLibs.srcDirs = ['libs']
- }
- }