{"id":324,"date":"2022-03-21T11:59:16","date_gmt":"2022-03-21T03:59:16","guid":{"rendered":"https:\/\/fugary.com\/?p=324"},"modified":"2022-03-21T12:10:11","modified_gmt":"2022-03-21T04:10:11","slug":"cordova%e8%87%aa%e5%ae%9a%e4%b9%89android%e9%85%8d%e7%bd%aebuild-gradle","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=324","title":{"rendered":"Cordova\u81ea\u5b9a\u4e49Android\u914d\u7f6ebuild.gradle"},"content":{"rendered":"<p>\u6700\u8fd1\u5728\u7ed9<code>Cordova<\/code>\u5305\u88c5<code>APP<\/code>\u96c6\u6210\u6781\u5149\u63a8\u9001\u7684\u5382\u5546\u901a\u9053\uff0c\u53d1\u73b0\u5382\u5546\u901a\u9053\u90fd\u9700\u8981\u5728\u6a21\u5757\u7ea7<code>build.gradle<\/code>\u6dfb\u52a0\u4f9d\u8d56\u9879\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u6587\u4ef6\u5728<code>platforms\/android\/app<\/code>\u76ee\u5f55\u4e0b\uff0c\u5728<code>Cordova<\/code>\u5b98\u65b9\u8bf4\u660e\u4e2d\u662f\u4e0d\u63a8\u8350\u4fee\u6539\u7684\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u6587\u4ef6\u662f\u81ea\u52a8\u751f\u6210\u7684\uff0c\u5982\u679c\u4fee\u6539\u4e86\u518d\u6b21<code>cordova platform add android<\/code>\u7684\u8bdd\u5c31\u4f1a\u88ab\u8986\u76d6\uff0c\u56e0\u6b64\u9700\u8981\u4f7f\u7528\u5230<code>build-extras.gradle<\/code>\u4ee5\u53ca\u4f7f\u7528<code>hook<\/code>\u590d\u5236\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5907\u6ce8\uff1a\u76ee\u524d\u5bf9\u4e8e\u9879\u76ee\u7ea7\u7684build.gradle\u6ca1\u6709build-extras.gradle\u652f\u6301\uff0c\u53ea\u80fd\u91c7\u7528\u6587\u4ef6\u5185\u5bb9\u66ff\u6362\u6765\u4fee\u6539<\/strong><\/p>\n<h3>\u65b0\u589ebuild-extras.gradle<\/h3>\n<p>\u53c2\u8003\u6587\u6863\uff1a<a href=\"https:\/\/cordova.apache.org\/docs\/en\/latest\/guide\/platforms\/android\/index.html#extending-buildgradle\">https:\/\/cordova.apache.org\/docs\/en\/latest\/guide\/platforms\/android\/index.html#extending-buildgradle<\/a><\/p>\n<p>\u6587\u6863\u91cc\u9762\u63d0\u5230\u4e86\u6269\u5c55<code>build.gradle<\/code>\u4f7f\u7528<code>build-extras.gradle<\/code>\u6587\u4ef6\uff0c\u800c\u4e0d\u5efa\u8bae\u624b\u52a8\u4fee\u6539\u7684<\/p>\n<blockquote>\n<p>If you need to customize <code>build.gradle<\/code>, rather than edit it directly, you should create a sibling file named <code>build-extras.gradle<\/code>. This file will be included by the main <code>build.gradle<\/code> when present. This file must be placed in the <code>app<\/code> folder of the Android platform directory (<code>&lt;your-project&gt;\/platforms\/android\/app<\/code>), so it is recommended that you copy it over via a script attached to the <code>before_build<\/code> <a href=\"https:\/\/cordova.apache.org\/docs\/en\/latest\/guide\/appdev\/hooks\/index.html\">hook<\/a>.<\/p>\n<\/blockquote>\n<p>\u6587\u6863\u63d0\u5230\u65b0\u5efa\u4e00\u4e2a\u540d\u53eb<code>build-extras.gradle<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u653e\u5230<code>&lt;your-project&gt;\/platforms\/android\/app<\/code>\u6587\u4ef6\u5939\u4e2d\uff0c\u8fd9\u6837\u5c31\u4f1a\u81ea\u52a8\u4f7f\u7528\u4e0a<code>build-extras.gradle<\/code>\u4e2d\u5b9a\u4e49\u7684\u914d\u7f6e\u9879\u4e86\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5728\u5916\u9762\u65b0\u5efa\u597d\u8be5\u6587\u4ef6\uff0c\u7136\u540e\u590d\u5236\u5230\u6307\u5b9a\u76ee\u5f55\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u53c2\u8003\u76ee\u5f55\u7ed3\u6784\uff0c\u65b0\u5efa\u4e00\u4e2a<code>android_custom<\/code>\u76ee\u5f55\uff0c\u628a\u9700\u8981\u590d\u5236\u5230<code>android<\/code>\u5e73\u53f0\u4e0b\u9762\u7684\u6570\u636e\u653e\u5230\u8be5\u6587\u4ef6\u5939\u4e0b\u9762\uff0c\u622a\u56fe\u91cc\u7684<code>arr<\/code>\u662f<code>OPPO<\/code>\u63a8\u9001\u9700\u8981\u7684\u4f9d\u8d56\u6587\u4ef6\u3002<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic\/uploads\/80d67d62af68e2f2b06900b8db314b38\/202203211146097.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic\/uploads\/80d67d62af68e2f2b06900b8db314b38\/202203211146097.png\" alt=\"image-20220321114639973\" \/><\/div><\/p>\n<h3>build-extras.gradle\u6587\u4ef6\u5185\u5bb9<\/h3>\n<p>build-extras.gradle\u6587\u4ef6\u5185\u5bb9\u81ea\u5df1\u5b9e\u9645\u60c5\u51b5\u5b9a\u4e49\u5c31\u53ef\u4ee5\uff1a<\/p>\n<pre><code class=\"language-groovy\">android {\n    defaultConfig {\n        manifestPlaceholders=[\n            JPUSH_PKGNAME : privateHelpers.extractStringFromManifest(&quot;package&quot;),\n            VIVO_APPKEY : &quot;xxxx&quot;, \/\/ VIVO\u5e73\u53f0\u6ce8\u518c\u7684appkey\n            VIVO_APPID : &quot;xxxx&quot;, \/\/ VIVO\u5e73\u53f0\u6ce8\u518c\u7684appid\n            XIAOMI_APPKEY : &quot;MI-xxxx&quot;,\/\/ \u5c0f\u7c73\u5e73\u53f0\u6ce8\u518c\u7684appkey\n            XIAOMI_APPID : &quot;MI-xxxx&quot;, \/\/ \u5c0f\u7c73\u5e73\u53f0\u6ce8\u518c\u7684appid\n            OPPO_APPKEY   : &quot;OP-xxxx&quot;,\n            OPPO_APPID    : &quot;OP-xxxx&quot;,\n            OPPO_APPSECRET: &quot;OP-xxxx&quot;,\n            MEIZU_APPKEY  : &quot;MZ-xxxx&quot;, \/\/ \u9b45\u65cf\u5e73\u53f0\u6ce8\u518c\u7684appkey\n            MEIZU_APPID   : &quot;MZ-xxxx&quot;, \/\/ \u9b45\u65cf\u5e73\u53f0\u6ce8\u518c\u7684appid\n        ]\n    }\n}\ndependencies {\n    implementation &#039;cn.jiguang.sdk.plugin:vivo:4.2.8&#039;\n    implementation &#039;cn.jiguang.sdk.plugin:xiaomi:4.2.8&#039;\n    implementation &#039;cn.jiguang.sdk.plugin:oppo:4.2.8&#039;\n    implementation &#039;cn.jiguang.sdk.plugin:meizu:4.2.8&#039;\n    implementation &#039;cn.jiguang.sdk.plugin:huawei:4.2.8&#039;\n    implementation &#039;com.huawei.agconnect:agconnect-core:1.5.2.300&#039;\n    implementation fileTree(dir: &#039;libs&#039;, include: [&#039;*.jar&#039;, &#039;*.arr&#039;])\n}\napply plugin: &#039;com.huawei.agconnect&#039;<\/code><\/pre>\n<h3>\u5185\u5bb9\u66ff\u6362<\/h3>\n<p>\u534e\u4e3a\u63a8\u9001\u6bd4\u8f83\u7279\u6b8a\uff0c\u9700\u8981\u4fee\u6539\u9879\u76ee\u7ea7\u522b\u7684<code>build.gradle<\/code>\uff0c\u53e6\u5916\u4ed3\u5e93\u5730\u5740\u4e5f\u8981\u4fee\u6539\uff0c\u5373\uff1a<\/p>\n<p><code>repositories.gradle<\/code>\u6dfb\u52a0\u534e\u4e3a\u7684\u4ed3\u5e93\u5730\u5740<\/p>\n<pre><code class=\"language-groovy\">ext.repos = {\n    google()\n    mavenCentral()\n    maven { url &#039;https:\/\/developer.huawei.com\/repo\/&#039; }\n}<\/code><\/pre>\n<p><code>build.gradle<\/code>\u4e5f\u8981\u6dfb\u52a0<code>buildscript<\/code>\u4f9d\u8d56\u63d2\u4ef6<\/p>\n<pre><code class=\"language-groovy\">buildscript {\n    apply from: &#039;CordovaLib\/cordova.gradle&#039;\n    apply from: &#039;repositories.gradle&#039;\n    repositories repos\n    dependencies {\n        classpath &#039;com.huawei.agconnect:agcp:1.5.2.300&#039; \n        \/\/ \u5176\u4ed6\u4f9d\u8d56\n    }\n}<\/code><\/pre>\n<h3>\u7f16\u5199hook\u6587\u4ef6<\/h3>\n<p>\u8fd9\u91cc<code>hook<\/code>\u6587\u4ef6\u653e\u5230<code>hooks\u76ee\u5f55<\/code>\u4e0b\u9762\u7684<code>after_platform_add<\/code>\u76ee\u5f55\u4e0b\uff0c\u8fd9\u4e2a\u76ee\u5f55\u540d\u81ea\u5df1\u5b9a\u4e49\u5373\u53ef\uff0c\u6ca1\u6709\u8981\u6c42\uff0c\u6587\u4ef6\u540d\u4e5f\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u8fd9\u91cc\u8d77\u540d<code>AndroidPlatformInit.js<\/code><\/p>\n<p>\u4e3b\u8981\u4f5c\u7528\u662f\u628a\u521a\u624d\u7684<code>android_custom<\/code>\u4e0b\u7684\u6587\u4ef6\u590d\u5236\u5230<code>platforms\/android\/app<\/code>\u76ee\u5f55\u4e0b\uff0c\u53e6\u5916\u5c31\u662f\u4fee\u6539\u90e8\u5206\u6784\u5efa\u6587\u4ef6\u5185\u5bb9\uff0c\u5185\u5bb9\u53c2\u8003\uff1a<\/p>\n<p><strong>\u53e6\u5916\u9488\u5bf9\u534e\u4e3a\u63a8\u9001\u9700\u8981\u4fee\u6539\u9879\u76ee\u7ea7build.gradle\u4ee5\u53carepositories.gradle\uff0c\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u66ff\u6362<\/strong><\/p>\n<pre><code class=\"language-js\">const path = require(&quot;path&quot;);\nconst fs = require(&quot;fs&quot;);\n\/**\n * \u4ecefromDir\u590d\u5236\u6587\u4ef6\u5230toDir\n * @param fromDir\n * @param toDir\n *\/\nfunction copyFiles(fromDir, toDir) {\n    var arr = fs.readdirSync(fromDir);\n    arr.forEach(function (item) {\n        var fromPath = path.join(fromDir, item);\n        var toPath = path.join(toDir, item);\n        var file = fs.lstatSync(fromPath);\n        if (file.isDirectory()) {\n            copyFiles(fromPath, toPath)\n        } else {\n            console.log(&#039;Copy &#039; + fromPath + &#039; to &#039; + toPath);\n            fs.createReadStream(fromPath)\n                .pipe(fs.createWriteStream(toPath));\n        }\n    });\n}\n\/**\n * \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u66ff\u6362\u90e8\u5206\u5185\u5bb9\u540e\u5199\u56de\n * @param file\n * @param callback\n *\/\nfunction replaceContents(file, callback) {\n    fs.readFile(file, &#039;utf8&#039;, function (err, data) {\n        if (err) throw err;\n        data = callback(data);\n        fs.writeFile(file, data, &#039;utf8&#039;, writeErr =&gt; {\n            if (err) throw err;\n            console.log(file, &#039;replace success.&#039;);\n        });\n    });\n}\n\/**\n * \u4e00\u4e9b\u81ea\u5b9a\u4e49\u64cd\u4f5c\n * @param ctx\n *\/\nmodule.exports = function (ctx) {\n    console.log(&#039;======================android init hook==========================&#039;)\n    var rootDir = ctx.opts.projectRoot;\n    var androidCustomDir = path.join(rootDir, &#039;android_custom&#039;);\n    var androidAppDir = path.join(rootDir, &#039;platforms\/android\/app&#039;);\n    copyFiles(androidCustomDir, androidAppDir);\n    var projectRepositoriesGradleFile = path.join(rootDir, &#039;platforms\/android\/repositories.gradle&#039;);\n    var appRepositoriesGradleFile = path.join(rootDir, &#039;platforms\/android\/app\/repositories.gradle&#039;);\n    \/\/ \u534e\u4e3a\u7684repository\n    var huaweiRepository = data =&gt; data.replace(\/(mavenCentral\\(\\))\/, &#039;$1\\n\\tmaven { url \\&#039;https:\/\/developer.huawei.com\/repo\/\\&#039; }&#039;);\n    replaceContents(projectRepositoriesGradleFile, huaweiRepository)\n    replaceContents(appRepositoriesGradleFile, huaweiRepository)\n    \/\/ \u534e\u4e3a\u7684build\u63d2\u4ef6\n    var projectBuildGradleFile = path.join(rootDir, &#039;platforms\/android\/build.gradle&#039;);\n    replaceContents(projectBuildGradleFile, data =&gt; data.replace(\/(dependencies {)\/, &#039;$1\\n\\t\\tclasspath \\&#039;com.huawei.agconnect:agcp:1.5.2.300\\&#039;&#039;))\n}<\/code><\/pre>\n<h3>\u914d\u7f6ehook<\/h3>\n<p><code>hook<\/code>\u914d\u7f6e\u6587\u6863\uff1a<a href=\"https:\/\/cordova.apache.org\/docs\/en\/latest\/guide\/appdev\/hooks\/index.html\">https:\/\/cordova.apache.org\/docs\/en\/latest\/guide\/appdev\/hooks\/index.html<\/a><\/p>\n<p>\u7f16\u5199\u597d<code>hook<\/code>\u6587\u4ef6\u4e4b\u540e\uff0c\u5176\u5b9e\u5e76\u4e0d\u80fd\u81ea\u52a8\u751f\u6548\uff0c\u9700\u8981\u5728<code>config.xml<\/code>\u4e2d\u914d\u7f6e\uff0c\u76ee\u524d\u53ea\u5728<code>android<\/code>\u4e0b\u6709\u7528\uff0c\u76f4\u63a5\u914d\u7f6e\u5230<code>android<\/code>\u5e73\u53f0\u7684\u8282\u70b9\u4e4b\u4e0b\u3002<\/p>\n<p>\u8fd9\u91cc\u9009\u62e9\u5728<code>platform<\/code>\u6dfb\u52a0\u7684\u65f6\u5019\u6267\u884c\u64cd\u4f5c\uff0c\u5e38\u7528\u7c7b\u578b\u53c2\u8003\u6587\u6863\u4e2d\u7684\u8bf4\u660e\u3002<\/p>\n<pre><code class=\"language-xml\">&lt;platform name=&quot;android&quot;&gt;\n    &lt;hook type=&quot;after_platform_add&quot; src=&quot;hooks\/after_platform_add\/AndroidPlatformInit.js&quot;\/&gt;\n&lt;\/platform&gt;<\/code><\/pre>\n<p>\u6267\u884c<code>cordova platform add android<\/code>\u7684\u65f6\u5019\u53ef\u4ee5\u770b\u5230\u6b63\u5e38\u6267\u884c\u4e86\u6587\u4ef6\u590d\u5236\u4ee5\u53ca\u6587\u4ef6\u5185\u5bb9\u66ff\u6362\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic\/uploads\/81fb89288f5c5f176fbc6baae4426347\/202203181519735.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic\/uploads\/81fb89288f5c5f176fbc6baae4426347\/202203181519735.png\" alt=\"image-20220318151937644\" \/><\/div><\/p>\n<p>\u7136\u540e\u5728\u5b9e\u9645\u8fd0\u884c\u7684\u65f6\u5019\u53ef\u4ee5\u53d1\u73b0<code>build-extras.gradle<\/code>\u6587\u4ef6\u4e5f\u5df2\u7ecf\u751f\u6548\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u7ed9Cordova\u5305\u88c5APP\u96c6\u6210\u6781\u5149\u63a8\u9001\u7684\u5382\u5546\u901a\u9053\uff0c\u53d1\u73b0\u5382\u5546\u901a\u9053\u90fd\u9700\u8981\u5728\u6a21\u5757\u7ea7build.gradle\u6dfb\u52a0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[26,70,36],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/324"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324"}],"version-history":[{"count":2,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions"}],"predecessor-version":[{"id":326,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/324\/revisions\/326"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}