apply plugin: 'com.android.application' android { //签名配置 signingConfigs { config { keyAlias 'key0' keyPassword 'android' storeFile file('D:/qwes-2.jks') storePassword 'android' } config1 { keyAlias 'key0' keyPassword 'android' storeFile file('D:/qwes-2.jks') storePassword 'android' } } compileSdkVersion 26 buildToolsVersion "27.0.2" defaultConfig { applicationId "com.example.x84091605.myapplication" minSdkVersion 22 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" signingConfig signingConfigs.config1 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.config1 } debug { signingConfig signingConfigs.config } } //发布渠道设置 productFlavors { uat { applicationId 'com.example.x84091605.myapplication.uat' Properties p = getProperties("../CI_Config/uat/assets/") versionCode java.lang.Integer.valueOf(p.hwc_versionCode) versionName p.getProperty("versionName") } sit { applicationId 'com.example.x84091605.myapplication.sit' Properties p = getProperties("../CI_Config /sit/assets/") versionCode java.lang.Integer.valueOf(p.hwc_versionCode) versionName p.getProperty("versionName") } produce { applicationId 'com.example.x84091605.myapplication' Properties p = getProperties("../CI_Config/release/assets/") versionCode java.lang.Integer.valueOf(p.hwc_versionCode) versionName p.getProperty("versionName") } } //根据发布渠道设置AndroidManifest.xml 里面的变量 productFlavors.all { flavor -> def appName = "@string/app_name" def env_type = "pro" def path = "pro" def facebook_app_id = "@string/app_id" def facebook_app_name = "@string/facebook_app_name" def alisa_name = "HWCMobile" println(getName()) if (getName().contains("produce")) { path = "release" env_type = "pro" appName = "@string/app_name" } else if (getName().contains("uat")) { env_type = "uat" path = "uat" appName = "@string/app_name_uat" facebook_app_id = "@string/app_uat_id" } else {//sit path = "sit" env_type = "sit" appName = "@string/app_name_sit" facebook_app_id = "@string/app_sit_id" } Properties p = getProperties("../CI_Config/${path}/assets/") flavor.manifestPlaceholders = [APP_NAME : appName, ENV_TYPE : env_type, RELEASE_DATE_KEY : "V" + getDateMore(), FACEBOOK_APP_ID : facebook_app_id, ] } //打包后的apk重命名 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith('.apk')) { def fileName = outputFile.name def environment = "release" boolean isRelease = !fileName.contains("debug") def path = getProjectDir().getParent() + "/apks/" if (fileName.contains("produce")) { environment = "release" path += "produce" fileName = "hwc-" + versionName + "-" + getDate(isRelease) + (!isRelease ? "-debug" : "") + ".apk" } else if (fileName.contains("uat")) { environment = "uat" path += "uat" fileName = "hwc-" + "uat-" + versionName + "-" + getDate(isRelease) + (!isRelease ? "-debug" : "") + ".apk" } else { environment = "sit" path += "sit" fileName = "hwc-" + "sit-" + versionName + "-" + getDate(isRelease) + (!isRelease ? "-debug" : "") + ".apk" } // output.outputFile = new File(outputFile.parent, fileName) output.outputFile = new File(path, fileName) } } } } /*日期获取和格式化*/ def getDate(boolean isRelease) { def date = new Date() def formattedDate = date.format(isRelease ? 'yyyyMMdd_HHmm' : 'yyyyMMdd') return formattedDate } /*** * 获取本地参数 * @param path * 配置文件路径 * @return */ Properties getProperties(String path) { def pFile = file(path + "hchannel.properties") Properties p = new Properties() pFile.withInputStream { stream -> p.load(stream) println p.versionName println p.hwc_versionCode } return p } /*日期获取和格式化*/ def getDateMore() { def date = new Date() def formattedDate = date.format('yyyyMMddHHmmss') return formattedDate } dependencies { compile 'com.android.support:cardview-v7:23.1.1' compile 'com.android.support:appcompat-v7:25.0.0' compile 'com.android.support:recyclerview-v7:23.1.1' compile(name: 'cptr-release', ext: 'aar') }
/*******************************************分割线***************************************/
/*******************************************分割线***************************************/
AndroidManifest配置使用:
<!-- 配置当前运行环境 -->
<meta-data
android:name="env"
android:value="${ENV_TYPE}" />
<!-- 打包时间 -->
<meta-data
android:name="hwc_release_time"
android:value="${RELEASE_DATE_KEY}" />
ApplicationInfo pinfo = context.getPackageManager()
.getApplicationInfo(context.getPackageName(),
PackageManager.GET_META_DATA);
versionDate = pinfo.metaData.getString("hwc_release_time", "");