import java.text.SimpleDateFormat apply plugin: 'com.android.application' // 定义当前gradle文件的全局变量 ext { //主版本号 MAJOR_VERSION = 1 //次版本号 MINOR_VERSION = 0 createFile() buildTimes = getBuildTime() isRelease = checkRelease() gitBranchName = getWorkingBranchName() } android { compileSdkVersion rootProject.ext.android.compileSdkVersion defaultConfig { applicationId "com.zhidao.adas.client" minSdkVersion rootProject.ext.android.minSdkVersion targetSdkVersion 25 multiDexEnabled true versionCode increasedVersionCode() versionName increasedVersionName() testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" flavorDimensions "multi_device" packagingOptions { //解决编译时com.android.builder.merge.DuplicateRelativeFileException: More than one file was found with OS independent path 'META-INF/rxjava.properties'这个错误 exclude 'META-INF/rxjava.properties' } externalNativeBuild { ndk { abiFilters 'armeabi-v7a', 'arm64-v8a' } } } signingConfigs { release { keyAlias 'CarLauncher' keyPassword 'ZDauto123456' storeFile file(getProjectDir().getParent() + '/keystore/car_launcher.jks') storePassword 'ZDauto123456' } } buildTypes { debug { signingConfig signingConfigs.release } release { minifyEnabled false signingConfig signingConfigs.release proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } //配置自定义打包名称 applicationVariants.all { variant -> def buildTypeName = variant.buildType.name //println("buildTypeName-------->" + buildTypeName) variant.outputs.all { StringBuffer buffer = new StringBuffer() buffer.append(getProject().name) buffer.append("_") buffer.append(defaultConfig.versionName) buffer.append(".") buffer.append(buildTimes) buffer.append("_") buffer.append(gitBranchName) buffer.append("_") //获取渠道编号 String flavor = variant.flavorName if (flavor != null && flavor != "") { buffer.append(flavor) buffer.append("_") } //获取打包时间 def fileName = "${buffer.toString()}${buildTypeName}.apk" //测试文件名称 buildConfigField 'String', 'APK_NAME', "\"${fileName}\"" //println("fileName-------->" + fileName) //这里修改apk文件名 outputFileName = fileName } } //插入构建时间 buildTypes.each { it.buildConfigField 'String', 'BUILD_TIME', "\"${buildTimes}\"" it.buildConfigField 'String', 'GIT_BRANCH_NAME', "\"${gitBranchName}\"" it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_TAXI', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_BUS', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_R);}}' it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M1', 'null' it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_M2', 'new java.util.HashSet(){{add(chassis.Chassis.GearPosition.GEAR_N);add(chassis.Chassis.GearPosition.GEAR_P);add(chassis.Chassis.GearPosition.GEAR_R);}}' it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_SWEEPER', 'null' it.buildConfigField 'java.util.Set', 'UNABLE_LAUNCH_AUTOPILOT_GEAR_VAN', 'null' } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } productFlavors { passenger { dimension "multi_device" buildConfigField 'boolean', 'IS_CLIENT', 'true' } driver { dimension "multi_device" buildConfigField 'boolean', 'IS_CLIENT', 'false' } } lintOptions { checkReleaseBuilds false // Or, if you prefer, you can continue to check for errors in release builds, // but continue the build even when errors are found: abortOnError false } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation rootProject.ext.dependencies.material implementation rootProject.ext.dependencies.androidxconstraintlayout implementation rootProject.ext.dependencies.androidxappcompat implementation "androidx.recyclerview:recyclerview:1.2.0" testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation rootProject.ext.dependencies.gson implementation 'com.github.bumptech.glide:glide:4.11.0' implementation project(':libraries:mogo-adas') implementation project(':libraries:mogo-adas-data') implementation project(':libraries:mogo-obu') // implementation 'com.zhidao.support.adas:high:2.8.0.0' // implementation 'com.zhjt.mogo.adas.data:adas-data:2.6.6.0' // compileOnly project(':core:mogo-core-data') // implementation project(':core:mogo-core-utils') implementation 'org.greenrobot:eventbus:3.2.0' implementation 'com.android.support:multidex:1.0.3' // implementation 'com.mogo.cloud:telematic:1.3.59'//注释掉司机端 乘客端 implementation 'com.jcraft:jsch:0.1.55' } // 删除老的APK def checkRelease() { def runTasks = gradle.startParameter.taskNames for (String task : runTasks) { // 我这里认为执行“assembleRelease”和非“debug”的任务就是执行“release”的任务 if (task.contains("assembleRelease") || task.contains("Release")) { deleteOldAPK("release") return true } else if (task.contains("Debug")) { deleteOldAPK("debug") return false } } return false } //在Moudle 根目录中创建gradle.properties文件存储VersionCode def createFile() { def propFile = file('./gradle.properties') Properties props = new Properties() if (!propFile.exists()) { propFile.createNewFile() props.load(new FileInputStream(propFile)) props['VERSION_CODE'] = '1' props.store(propFile.newWriter(), null) } else { props.load(new FileInputStream(propFile)) def code = props['VERSION_CODE'] if (code == null || code == "0") { props['VERSION_CODE'] = '1' props.store(propFile.newWriter(), null) } } } //读取VersionCode def readVersionCode() { def propFile = file('./gradle.properties') Properties props = new Properties() props.load(new FileInputStream(propFile)) // 读取gradle.properties文件中定义的VERSION_CODE属性 def code = props['VERSION_CODE'].toInteger() //def code = props.getProperty('VERSION_CODE').toInteger() return code } //当打包的是Release版本时自动更新VersionCode def increasedVersionCode() { def code = readVersionCode() //println("isRelease====" + isRelease) if (isRelease) { code++ def propFile = file('./gradle.properties') Properties props = new Properties() props.load(new FileInputStream(propFile)) props['VERSION_CODE'] = code.toString() // 将最新的versionCode写入gradle.properties文件中 props.store(propFile.newWriter(), null) } return code } //删除无用apk def deleteOldAPK(String buildTypeName) { // 使用map创建一个树 def tree = fileTree(dir: './build/outputs/apk', include: "${buildTypeName}/*.apk") //println("文件个数:" + tree.size()) // 遍历文件树 tree.each { File file -> //println("删除文件名称:" + file) file.delete() } } def increasedVersionName() { def versionName = "${MAJOR_VERSION}.${MINOR_VERSION}.${readVersionCode()}" return versionName } def getBuildTime() { //设置时间格式 SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd.HHmmss", Locale.getDefault()) //获取当前时间 Date curDate = new Date(System.currentTimeMillis()) return formatter.format(curDate) } /** * @return 获取当前分支名称 */ def getWorkingBranchName() { def workingBranchName = "" def proc = "git rev-parse --abbrev-ref HEAD".execute() proc.in.eachLine { line -> workingBranchName = line } proc.err.eachLine { line -> println line } proc.waitFor() workingBranchName = "${workingBranchName}" println "Working branch name: " + workingBranchName return workingBranchName }