美文网首页
android 一键自动上传蒲公英

android 一键自动上传蒲公英

作者: XII01 | 来源:发表于2020-03-24 13:27 被阅读0次

    首先注册蒲公英,拿到api_key,在下面的文件中用的到
    一、在工程目录下建一个pgyerUpload.gradle

    import groovy.json.JsonSlurper
    
    project.ext {
        upload2d = { extension, buildUpdateDescription ->
            extension.android {
                //在这里获取: https://www.pgyer.com/doc/view/api#uploadApp
                task upload2Pgyer(group: 'upload2Pgyer', description: 'upload apk to pgyer', dependsOn: 'assembleDebug') {
                    doLast {
                        applicationVariants.all {
                            it.outputs.each { out ->
                                def outputFile = out.outputFile
                                print("outputFile.name" + outputFile.name)
                                if (outputFile != null && it.buildType.name == "debug") {//release修改这里
                                    logger.log(LogLevel.ERROR, "start up load apk file : ${outputFile.absolutePath}")
                                    uploadApk(outputFile.absolutePath, buildUpdateDescription)
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    
    /**
     * 上传apk到蒲公英
     */
    def uploadApk(String path, String buildUpdateDescription) {
        //查找上传的apk文件,这里需要换成自己apk路径
        println "上传apk到蒲公英apk=" + path + ":buildUpdateDescription=" + buildUpdateDescription
        def apk = new File(path)
    
        println "*************** start upload file ***************"
    
        def twoHyphens = "--"
        def boundary = "*********"
        def end = "\r\n"
    
        //模拟表单上传 multipart/form-data
        def conn = new URL("https://www.pgyer.com/apiv2/app/upload").openConnection()
        conn.setRequestMethod('POST')
        conn.setRequestProperty("Connection", "Keep-Alive")
        conn.setRequestProperty("Charset", "UTF-8")
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary)
        conn.setDoInput(true)
        conn.setDoOutput(true)
        //添加参数:_api_key
        def sb = new StringBuilder()
        sb.append(twoHyphens).append(boundary).append(end)
        sb.append("Content-Disposition: form-data; name=_api_key")
        sb.append(end).append(end)
        sb.append("你的key").append(end)
    
        //添加参数:buildUpdateDescription 更新日志,取值gradle.properties中的 BUILD_NOTES
        sb.append(twoHyphens).append(boundary).append(end)
        sb.append("Content-Disposition: form-data; name=buildUpdateDescription")
        sb.append(end).append(end)
        sb.append("").append(end)
    
        //添加参数file: 需要上传的apk文件
        sb.append(twoHyphens).append(boundary).append(end)
        sb.append("Content-Disposition: form-data; name=file;filename=").append(apk.getName())
        sb.append(end).append(end)
    
        def dos = new DataOutputStream(conn.getOutputStream())
        dos.writeBytes(sb.toString())
        dos.flush()
        sb.delete(0, sb.length())
    
        def fis = new FileInputStream(apk)
        byte[] bf = new byte[8192]
        int len
        while ((len = fis.read(bf)) != -1) {
            dos.write(bf, 0, len)
        }
        sb.append(end)
        sb.append(twoHyphens).append(boundary).append(end)
        dos.writeBytes(sb.toString())
    
        dos.flush()
        fis.close()
        dos.close()
        conn.connect()
    
        def text = conn.getContent().text
        def resp = new JsonSlurper().parseText(text)
    
        println text
        println "*************** upload finish ***************"
    
        if (resp.code != 0) {
            throw new RuntimeException(resp.message)
        }
        println resp
        //浏览器中打开短连接
        def url = "https://www.pgyer.com/" + resp.data.buildShortcutUrl
        exec {
            commandLine "powershell", "start", url
        }
        //这里还可以处理你想要的地方
    }
    

    二、在APP模块下面的build.gradle配置以下代码

    apply from: "${rootProject.rootDir}/pgyerUpload.gradle"
    
    android{
    ...
     //上传apk描述,建议不传
        project.ext.upload2d project,""
    }
    

    在app gradle 任务中,找到下图任务,双击在控制台中查看上传日志


    upload2pgyer

    如果有需要还可以在拿到上传回来的信息后,发送二维码到邮件,钉钉等等。
    如果对gradle task 不是很理解的,可以去补补

    相关文章

      网友评论

          本文标题:android 一键自动上传蒲公英

          本文链接:https://www.haomeiwen.com/subject/pmcoyhtx.html