{"id":524,"date":"2022-12-16T13:19:41","date_gmt":"2022-12-16T05:19:41","guid":{"rendered":"https:\/\/iichen.cn\/?p=524"},"modified":"2022-12-16T13:19:41","modified_gmt":"2022-12-16T05:19:41","slug":"android-gradle%e6%89%93%e5%8c%85%e4%b8%8a%e4%bc%a0%e5%88%b0%e8%92%b2%e5%85%ac%e8%8b%b1%e5%b9%b6%e6%8f%90%e9%86%92%e5%88%b0%e9%92%89%e9%92%89","status":"publish","type":"post","link":"https:\/\/iichen.cn\/?p=524","title":{"rendered":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489"},"content":{"rendered":"<h4>1. \u65b0\u5efadingding.gradle<\/h4>\n<blockquote><p>\n  \u672a\u5f00\u542f\u7b7e\u540d\u8ba4\u8bc1,\u5982\u9700\u7b7e\u540d\u6309\u7167\u9489\u9489\u6587\u6863\u6765\u5373\u53ef\n<\/p><\/blockquote>\n<pre><code class=\"language-java line-numbers\">import groovy.json.JsonBuilder\nimport groovy.json.JsonOutput\n\ndef dingDingUrl = \"https:\/\/oapi.dingtalk.com\/robot\/send?access_token=344de308d49322348c1356de11cac6bbbf6f75ca610b801b1b4bd3f2a079b61c\"\n\next.postDingMsg = { String qrcodeUrl, String downloadUrl -&gt;\n    String allTips =\n            \"\u7248\u672c\u53f7\uff1aV${rootProject.ext.android[\"versionName\"]}\\n\" +\n                    \"${downloadUrl}\\n\"\n\n    postAll(dingDingUrl, allTips, qrcodeUrl, downloadUrl)\n}\n\n\/\/\u53d1\u9001\u5230\u7fa4\uff0c@\u6240\u6709\u4eba\ndef postAll(url, tips, qrcodeUrl, downloadUrl) {\n\n    JsonBuilder builder = new JsonBuilder()\n    builder {\n        msgtype 'link'\n        link {\n            title '\u5b89\u5353\u5185\u6d4b\u66f4\u65b0'\n            text tips\n            messageUrl downloadUrl\n            picUrl qrcodeUrl\n        }\n        at {\n            isAtAll false   \/\/@\u6240\u6709\u4eba(\u53ea\u6709text\uff0cmarkdown\uff0cactioncard\u8fd9\u4e09\u79cd\u6d88\u606f\u7c7b\u578b\u652f\u6301@\u529f\u80fd)\n        }\n    }\n    String data = JsonOutput.prettyPrint(builder.toString())\n    postDingDing(url, data)\n}\n\n\/\/\u8c03\u7528\u63a5\u53e3\uff0c\u53d1\u9001\u6d88\u606f\ndef postDingDing(urlString, msg) {\n    if (msg == null) {\n        return\n    }\n    HttpURLConnection conn = null\n    OutputStream outputStream = null\n    try {\n        if (conn == null) {\n            Long timestamp = System.currentTimeMillis()\n            URL url = new URL(urlString + \"&amp;timestamp=${timestamp}\")\n            conn = (HttpURLConnection) url.openConnection()\n        }\n        if (conn != null) {\n            conn.setReadTimeout(15000)\n            conn.setConnectTimeout(15000)\n            conn.setDoOutput(true)\n            conn.setDoInput(true);\n            conn.setRequestMethod(\"POST\");\n            conn.setUseCaches(false)\n            conn.setRequestProperty(\"Content-Type\", \"application\/json;charset=utf-8\")\n            conn.connect()\n        }\n\n        if (msg != null &amp;&amp; msg.length() &gt; 0) {\n            DataOutputStream dataOutputStream = new DataOutputStream(conn.getOutputStream())\n            byte[] t = msg.getBytes(\"utf-8\")\n            dataOutputStream.write(t)\n            dataOutputStream.flush()\n            dataOutputStream.close()\n\n            int res = conn.getResponseCode()\n            if (res == 200) {  \/\/\u6210\u529f\n                InputStream input = conn.getInputStream()\n                println \"\u53d1\u9001\u6d88\u606f\u6210\u529f: ${readString(input)}\"\n            } else {\n                println(\"\u53d1\u9001\u6d88\u606f\u5931\u8d25: \" + conn.getResponseCode())\n            }\n        }\n    } catch (EOFException e) {\n        e.printStackTrace()\n    } catch (IOException e) {\n        e.printStackTrace()\n    } finally {\n        if (outputStream != null) {\n            try {\n                outputStream.close()\n            } catch (IOException e) {\n                e.printStackTrace()\n            }\n        }\n        if (conn != null) {\n            conn.disconnect()\n        }\n    }\n}\n\n\/**\n * \u8bfb\u53d6\u7f51\u7edc\u6d41\n *\/\nprivate static String readString(InputStream inputStream) throws IOException {\n    int count = 0;\n    while (count == 0) {\n        count = inputStream.available();\n        \/\/\u6211\u4eec\u53ef\u4ee5\u5728\u6b21\u5904\u52a0\u4e0a\u8d85\u65f6\u5224\u65ad\n    }\n    byte[] b = new byte[count];\n    if (inputStream.read(b) != -1) {\n        return new String(b);\n    }\n    return \"\";\n}\n\n<\/code><\/pre>\n<h4>2. \u9879\u76eebuild.gradle<\/h4>\n<pre><code class=\"language-java line-numbers\">def getApkFullPath() {\n    return \"${projectDir.absolutePath}\/build\/outputs\/apk\/release\/$apkFileName\"\n}\n\nprivate def uploadPGY(String filePath) {\n    def stdout = new ByteArrayOutputStream()\n    exec {\n        executable = 'curl'\n        args = ['-F', \"file=@${filePath}\", '-F', \"_api_key=${rootProject.ext.pgy[\"apiKey\"]}\", rootProject.ext.pgy[\"uploadUrl\"]]\n        standardOutput = stdout\n    }\n    String output = stdout.toString()\n    def parsedJson = new JsonSlurper().parseText(output)\n    println parsedJson.data.buildQRCodeURL\n    println \"\u84b2\u516c\u82f1\u4e0a\u4f20\u5b8c\u6210\u3000\u7248\u672c\u53f7\uff1a\" + parsedJson.data.buildVersion\n\n    println \"\u5f00\u59cb\u53d1\u9001\u9489\u9489\u7fa4\u901a\u77e5\"\n    def downloadUrl = \"https:\/\/www.pgyer.com\/\" + parsedJson.data.buildShortcutUrl\n    postDingMsg(parsedJson.data.buildQRCodeURL, downloadUrl)\n}\n\ntask uploadApk(dependsOn: \"assemble\") {\n    group = \"publish\"\n    doLast {\n        def apk = getApkFullPath()\n        println \"\u6253\u5305\u5b8c\u6210: $apk\"\n        println \"\u5f00\u59cb\u4e0a\u4f20\u84b2\u516c\u82f1\"\n        uploadPGY(apk)\n    }\n}\n<\/code><\/pre>\n<h4>3. \u5176\u4ed6\u8865\u5145\u8bb0\u5f55<\/h4>\n<ul>\n<li>\u83b7\u53d6\u6253\u5305\u540e\u8def\u5f84<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">android.applicationVariants.all { variant -&gt;\n    if ( (variant.name).equals(\"release\") ) {\n        variant.outputs.each { output -&gt;\n            apk = output.outputFile\n        }\n    }\n}\n<\/code><\/pre>\n<ul>\n<li>\u4fee\u6539\u6253\u5305\u540e\u7684\u8def\u5f84\u548c\u6587\u4ef6\u540d\u79f0<\/li>\n<\/ul>\n<pre><code class=\"language-java line-numbers\">    applicationVariants.all { variant -&gt;\n        variant.outputs.all { output -&gt;\n            String timeStr = new SimpleDateFormat(\"MM-dd-HH-mm\").format(new Date());\n            if (variant.buildType.name == \"debug\") {\n                apkFileName = \"anmo-debug-${defaultConfig.versionName}-${timeStr}.apk\"\n                output.outputFileName = apkFileName\n            } else if (variant.buildType.name == \"release\") {\n                apkFileName = \"anmo-${defaultConfig.versionName}-${timeStr}.apk\"\n                output.outputFileName = apkFileName\n            }\n        }\n    }\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. \u65b0\u5efadingding.gradle \u672a\u5f00\u542f\u7b7e\u540d\u8ba4\u8bc1,\u5982\u9700\u7b7e\u540d\u6309\u7167\u9489\u9489\u6587\u6863\u6765\u5373\u53ef import groov [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8],"tags":[],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-android","category-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iichen.cn\/?p=524\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen\" \/>\n<meta property=\"og:description\" content=\"1. \u65b0\u5efadingding.gradle \u672a\u5f00\u542f\u7b7e\u540d\u8ba4\u8bc1,\u5982\u9700\u7b7e\u540d\u6309\u7167\u9489\u9489\u6587\u6863\u6765\u5373\u53ef import groov [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iichen.cn\/?p=524\" \/>\n<meta property=\"og:site_name\" content=\"IIchen\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-16T05:19:41+00:00\" \/>\n<meta name=\"author\" content=\"iichen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"iichen\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/iichen.cn\/?p=524#article\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/?p=524\"},\"author\":{\"name\":\"iichen\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"headline\":\"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489\",\"datePublished\":\"2022-12-16T05:19:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/iichen.cn\/?p=524\"},\"wordCount\":5,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"articleSection\":[\"Android\",\"\u7b14\u8bb0\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/iichen.cn\/?p=524#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iichen.cn\/?p=524\",\"url\":\"https:\/\/iichen.cn\/?p=524\",\"name\":\"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/#website\"},\"datePublished\":\"2022-12-16T05:19:41+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/iichen.cn\/?p=524#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iichen.cn\/?p=524\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iichen.cn\/?p=524#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/iichen.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iichen.cn\/#website\",\"url\":\"https:\/\/iichen.cn\/\",\"name\":\"IIchen\",\"description\":\"Just do it!\",\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iichen.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\",\"name\":\"iichen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"contentUrl\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"width\":940,\"height\":940,\"caption\":\"iichen\"},\"logo\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.iichen.cn\"],\"url\":\"https:\/\/iichen.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iichen.cn\/?p=524","og_locale":"zh_CN","og_type":"article","og_title":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen","og_description":"1. \u65b0\u5efadingding.gradle \u672a\u5f00\u542f\u7b7e\u540d\u8ba4\u8bc1,\u5982\u9700\u7b7e\u540d\u6309\u7167\u9489\u9489\u6587\u6863\u6765\u5373\u53ef import groov [&hellip;]","og_url":"https:\/\/iichen.cn\/?p=524","og_site_name":"IIchen","article_published_time":"2022-12-16T05:19:41+00:00","author":"iichen","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"iichen"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/iichen.cn\/?p=524#article","isPartOf":{"@id":"https:\/\/iichen.cn\/?p=524"},"author":{"name":"iichen","@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"headline":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489","datePublished":"2022-12-16T05:19:41+00:00","mainEntityOfPage":{"@id":"https:\/\/iichen.cn\/?p=524"},"wordCount":5,"commentCount":0,"publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"articleSection":["Android","\u7b14\u8bb0"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/iichen.cn\/?p=524#respond"]}]},{"@type":"WebPage","@id":"https:\/\/iichen.cn\/?p=524","url":"https:\/\/iichen.cn\/?p=524","name":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489 - IIchen","isPartOf":{"@id":"https:\/\/iichen.cn\/#website"},"datePublished":"2022-12-16T05:19:41+00:00","breadcrumb":{"@id":"https:\/\/iichen.cn\/?p=524#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iichen.cn\/?p=524"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iichen.cn\/?p=524#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/iichen.cn\/"},{"@type":"ListItem","position":2,"name":"Android-Gradle\u6253\u5305\u4e0a\u4f20\u5230\u84b2\u516c\u82f1\u5e76\u63d0\u9192\u5230\u9489\u9489"}]},{"@type":"WebSite","@id":"https:\/\/iichen.cn\/#website","url":"https:\/\/iichen.cn\/","name":"IIchen","description":"Just do it!","publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iichen.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c","name":"iichen","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/","url":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","contentUrl":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","width":940,"height":940,"caption":"iichen"},"logo":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.iichen.cn"],"url":"https:\/\/iichen.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=524"}],"version-history":[{"count":0,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/524\/revisions"}],"wp:attachment":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}