第一步
android{ buildTypes { //配置apk名称 android.applicationVariants.all { variant -> variant.outputs.all { outputFileName = getApkName() } } }}
android{ } //------------以下代码用于配置一键上传蒲公英----------------//获取apk路径def getApkFullPath() { return rootDir.getAbsolutePath() + "/app/build/outputs/apk/release/" + getApkName()}//此处可修改所打包的apk文件名def getApkName() { return "update-app-example-v${android.defaultConfig.versionName}-${releaseTime()}.apk"}//设定添加打包时间static def releaseTime() { return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))}//将密钥存在本地 防止泄露 local.properties 在Git的时候不会被上传def readProperties(key) { File file = rootProject.file('local.properties') if (file.exists()) { InputStream inputStream = rootProject.file('local.properties').newDataInputStream() Properties properties = new Properties() properties.load(inputStream) if (properties.containsKey(key)) { return properties.getProperty(key) }}}//描述logstatic def getUpdateDescription() { return '1.修复一些bug;\n2.提升用户体验!'}//执行打包上传任务task("uploadApk") { doLast { def command = "curl -F \"file=@${getApkFullPath()}\" -F \"uKey=${readProperties('pgyer.userKey')}\" -F \"_api_key=${readProperties('pgyer.apiKey')}\" -F \"buildUpdateDescription=${getUpdateDescription()}\" https://www.pgyer.com/apiv2/app/upload" try { exec { ExecSpec execSpec -> executable 'curl' args = ['-F', "file=@${getApkFullPath()}", '-F', "uKey=${readProperties('pgyer.userKey')}", '-F', "_api_key=${readProperties('pgyer.apiKey')}", '-F', "buildUpdateDescription=${getUpdateDescription()}", "${readProperties('pgyer.uploadurl')}"] } println "uploadApk success~" } catch (Exception e) { e.printStackTrace() } }}uploadApk.dependsOn("assembleRelease")
第二步:在项目根目录下local.properties文件中配置如下代码:
#蒲公英配置 apiKey和userKey替换成自己的就行pgyer.apiKey=711ea731f7e59d20a6279a884a2c76f8pgyer.userKey=61fedceea73bac2bdda4ac76kl8dbcac0pgyer.uploadurl=https://www.pgyer.com/apiv2/app/upload
最后:一键上传即可,如图
小结