{"id":717,"date":"2023-02-26T15:24:29","date_gmt":"2023-02-26T07:24:29","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=717"},"modified":"2023-04-29T17:15:52","modified_gmt":"2023-04-29T09:15:52","slug":"dynamic-modification-of-app-name-icon-applicationid-version-number-and-resource-addition-for-android-multi-channel-packaging","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/dynamic-modification-of-app-name-icon-applicationid-version-number-and-resource-addition-for-android-multi-channel-packaging\/","title":{"rendered":"Android\u591a\u6e20\u9053\u6253\u5305\u4e4b\u52a8\u6001\u4fee\u6539App\u540d\u79f0\uff0c\u56fe\u6807\uff0capplicationId\uff0c\u7248\u672c\u53f7\uff0c\u6dfb\u52a0\u8d44\u6e90"},"content":{"rendered":"<p>\u540c\u4e00\u5957\u4ee3\u7801\uff0c\u6253\u5305N\u5957APP\uff0c\u800c\u4e14\u8fd9\u4e9bAPP\u7684\u540d\u79f0\u3001\u56fe\u6807\u3001applicationId\u3001\u7248\u672c\u53f7\uff0c\u751a\u81f3\u4e3b\u9875\u90fd\u4e0d\u4e00\u6837\u3002\u4e4b\u524d\u90fd\u662f\u5355\u6b21\u4fee\u6539\uff0c\u5355\u6b21\u6253\u5305\uff0c\u53ef\u968f\u7740\u9700\u6c42\u8d8a\u6765\u8d8a\u591a\uff0c\u9700\u8981\u6253\u7684\u5305\u4e5f\u4f1a\u8d8a\u6765\u8d8a\u591a\uff0c\u5355\u6b21\u6253\u5305\u8d39\u65f6\u8d39\u529b\uff0c\u5f88\u660e\u663e\u5df2\u7ecf\u4e0d\u518d\u9002\u5408\uff0c\u4e8e\u662f\u501f\u52a9Gradle\u7684productFlavors\u5b9e\u73b0\u6240\u9700\u529f\u80fd\uff0c\u6253\u5305\u8fc7\u7a0b\u4e5f\u53d8\u7684\u66f4\u4e3a\u7b80\u5355\u3002<\/p>\n<p>Gradle\u662f\u4e00\u4e2a\u57fa\u4e8eApache Ant\u548cApache Maven\u6982\u5ff5\u7684\u9879\u76ee\u81ea\u52a8\u5316\u5efa\u6784\u5de5\u5177\u3002\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8f7b\u677e\u5b9e\u73b0\u591a\u6e20\u9053\u6253\u5305\u7684\u529f\u80fd\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>build.gradle<\/h2>\n<pre><code class=\"language-java\">apply plugin: &#039;com.android.application&#039;\n\n\/\/\u6253\u5305\u65f6\u95f4\ndef releaseTime() {\n    return new Date().format(&quot;yyyy-MM-dd&quot;, TimeZone.getTimeZone(&quot;UTC&quot;))\n}\n\n\/\/\u83b7\u53d6local.properties\u7684\u5185\u5bb9\nProperties properties = new Properties()\nproperties.load(project.rootProject.file(&#039;local.properties&#039;).newDataInputStream())\n\nandroid {\n    compileSdkVersion 25\n    buildToolsVersion &quot;25.0.3&quot;\n\n    \/\/ \u4f7f\u7528\u7b7e\u540d\u6587\u4ef6\u8fdb\u884c\u7b7e\u540d\u7684\u4e24\u79cd\u65b9\u5f0f\n\/\/    \/\/\u7b2c\u4e00\u79cd\uff1a\u4f7f\u7528gradle\u76f4\u63a5\u7b7e\u540d\u6253\u5305\n\/\/    signingConfigs {\n\/\/        config {\n\/\/            storeFile file(&#039;keystore.jks&#039;)\n\/\/            storePassword &#039;123456&#039;\n\/\/            keyAlias &#039;HomeKey&#039;\n\/\/            keyPassword &#039;123456&#039;\n\/\/        }\n\/\/    }\n    \/\/\u7b2c\u4e8c\u79cd\uff1a\u4e3a\u4e86\u4fdd\u62a4\u7b7e\u540d\u6587\u4ef6\uff0c\u5c06\u5176\u653e\u5728local.properties\u4e2d\u5e76\u5728\u7248\u672c\u5e93\u4e2d\u6392\u9664\uff0c\u4e0d\u628a\u8fd9\u4e9b\u4fe1\u606f\u5199\u5165\u5230\u7248\u672c\u5e93\u4e2d\uff08\u6ce8\u610f\uff0c\u6b64\u79cd\u65b9\u5f0f\u7b7e\u540d\u6587\u4ef6\u4e2d\u4e0d\u80fd\u6709\u4e2d\u6587\uff09\n    signingConfigs {\n        config {\n            storeFile file(properties.getProperty(&quot;keystroe_storeFile&quot;))\n            storePassword properties.getProperty(&quot;keystroe_storePassword&quot;)\n            keyAlias properties.getProperty(&quot;keystroe_keyAlias&quot;)\n            keyPassword properties.getProperty(&quot;keystroe_keyPassword&quot;)\n        }\n    }\n\n    \/\/ \u9ed8\u8ba4\u914d\u7f6e\n    defaultConfig {\n        minSdkVersion 16\n        targetSdkVersion 25\n        versionCode 1\n        versionName &quot;1.0.1&quot;\n    }\n\n    \/\/ \u591a\u6e20\u9053 \u7684\u4e0d\u540c\u914d\u7f6e\n    productFlavors {\n         baidu {\n            \/\/ \u6bcf\u4e2a\u73af\u5883\u5305\u540d\u4e0d\u540c\n            applicationId &quot;cn.appblog.multichannel.baidu&quot;\n            \/\/ \u52a8\u6001\u6dfb\u52a0 string.xml \u5b57\u6bb5\uff1b\n            \/\/ \u6ce8\u610f\uff0c\u8fd9\u91cc\u662f\u6dfb\u52a0\uff0c\u5728 string.xml \u4e0d\u80fd\u6709\u8fd9\u4e2a\u5b57\u6bb5\uff0c\u4f1a\u91cd\u540d\uff01\uff01\uff01\n            resValue &quot;string&quot;, &quot;app_name&quot;, &quot;\u767e\u5ea6&quot;\n            resValue &quot;bool&quot;, &quot;auto_updates&quot;, &#039;false&#039;\n            \/\/ \u52a8\u6001\u4fee\u6539 \u5e38\u91cf \u5b57\u6bb5\n            buildConfigField &quot;String&quot;, &quot;ENVIRONMENT&quot;, &#039;&quot;\u6211\u662f\u767e\u5ea6\u9996\u9875&quot;&#039;\n            \/\/ \u4fee\u6539 AndroidManifest.xml \u91cc\u6e20\u9053\u53d8\u91cf\n            manifestPlaceholders = [CHANNEL_VALUE: &quot;baidu&quot;, app_icon: &quot;@mipmap\/logo_baidu&quot;]\n        }\n        qq {\n            applicationId &quot;cn.appblog.multichannel.qq&quot;\n\n            resValue &quot;string&quot;, &quot;app_name&quot;, &quot;\u817e\u8baf&quot;\n            resValue &quot;bool&quot;, &quot;auto_updates&quot;, &#039;true&#039;\n\n            buildConfigField &quot;String&quot;, &quot;ENVIRONMENT&quot;, &#039;&quot;\u6211\u662f\u817e\u8baf\u9996\u9875&quot;&#039;\n\n            manifestPlaceholders = [CHANNEL_VALUE: &quot;qq&quot;, app_icon: &quot;@mipmap\/icon_qq&quot;]\n        }\n        xiaomi {\n            applicationId &quot;com.shi.androidstudio.multichannel.xiaomi&quot;\n\n            resValue &quot;string&quot;, &quot;app_name&quot;, &quot;\u5c0f\u7c73&quot;\n            resValue &quot;bool&quot;, &quot;auto_updates&quot;, &#039;true&#039;\n            resValue &quot;drawable&quot;, &quot;isrRank&quot;, &#039;true&#039;\n\n            buildConfigField &quot;String&quot;, &quot;ENVIRONMENT&quot;, &#039;&quot;\u6211\u662f\u5c0f\u7c73\u9996\u9875&quot;&#039;\n\n            manifestPlaceholders = [CHANNEL_VALUE: &quot;xiaomi&quot;, app_icon: &quot;@mipmap\/icon_xiaomi&quot;]\n        }\n    }\n\n    \/\/\u79fb\u9664lint\u68c0\u6d4b\u7684error\n    lintOptions {\n        abortOnError false\n    }\n\n    buildTypes {\n        debug {\n            \/\/ debug\u6a21\u5f0f\u4e0b\uff0c\u663e\u793alog\n            buildConfigField(&quot;boolean&quot;, &quot;LOG_DEBUG&quot;, &quot;true&quot;)\n            \/\/\u4e3a\u5df2\u7ecf\u5b58\u5728\u7684applicationId\u6dfb\u52a0\u540e\u7f00\n            applicationIdSuffix &quot;.debug&quot;\n            \/\/ \u4e3a\u7248\u672c\u540d\u6dfb\u52a0\u540e\u7f00\n            versionNameSuffix &quot;-debug&quot;\n            \/\/ \u4e0d\u5f00\u542f\u6df7\u6dc6\n            minifyEnabled false\n            \/\/ \u4e0d\u5f00\u542fZipAlign\u4f18\u5316\n            zipAlignEnabled false\n            \/\/ \u4e0d\u79fb\u9664\u65e0\u7528\u7684resource\u6587\u4ef6\n            shrinkResources false\n            \/\/ \u4f7f\u7528config\u7b7e\u540d\n            signingConfig signingConfigs.config\n\n        }\n        release {\n            \/\/ release\u6a21\u5f0f\u4e0b\uff0c\u4e0d\u663e\u793alog\n            buildConfigField(&quot;boolean&quot;, &quot;LOG_DEBUG&quot;, &quot;false&quot;)\n            \/\/ \u4e3a\u7248\u672c\u540d\u6dfb\u52a0\u540e\u7f00\n            versionNameSuffix &quot;-relase&quot;\n            \/\/ \u4e0d\u5f00\u542f\u6df7\u6dc6\n            minifyEnabled false\n            \/\/ \u5f00\u542fZipAlign\u4f18\u5316\n            zipAlignEnabled true\n            \/\/ \u79fb\u9664\u65e0\u7528\u7684resource\u6587\u4ef6\n            shrinkResources true\n            \/\/ \u4f7f\u7528config\u7b7e\u540d\n            \/\/ signingConfig signingConfigs.config\n            \/\/ \u6df7\u6dc6\u6587\u4ef6\u4f4d\u7f6e\n            proguardFiles getDefaultProguardFile(&#039;proguard-android.txt&#039;), &#039;proguard-rules.pro&#039;\n\n            \/\/ \u6279\u91cf\u6253\u5305\n            applicationVariants.all { variant -&gt;\n                variant.outputs.each { output -&gt;\n                    def outputFile = output.outputFile\n                    if (outputFile != null &amp;&amp; outputFile.name.endsWith(&#039;.apk&#039;)) {\n                        \/\/\u8f93\u51faapk\u540d\u79f0\u4e3a\uff1a\u6e20\u9053\u540d_\u7248\u672c\u540d_\u65f6\u95f4.apk\n                        def fileName = &quot;${variant.productFlavors[0].name}_v${defaultConfig.versionName}_${releaseTime()}.apk&quot;\n                        output.outputFile = new File(outputFile.parent, fileName)\n                    }\n                }\n            }\n\n        }\n    }\n}\n\n\/\/\u9879\u76ee\u4f9d\u8d56\ndependencies {\n    compile fileTree(dir: &#039;libs&#039;, include: [&#039;*.jar&#039;])\n    testCompile &#039;junit:junit:4.12&#039;\n    compile &#039;com.android.support:appcompat-v7:23.3.0&#039;\n}<\/code><\/pre>\n<h2>local.properties<\/h2>\n<pre><code>## This file is automatically generated by Android Studio.\n# Do not modify this file -- YOUR CHANGES WILL BE ERASED!\n#\n# This file should *NOT* be checked into Version Control Systems,\n# as it contains information specific to your local configuration.\n#\n# Location of the SDK. This is only used by Gradle.\n# For customization when using a Version Control System, please read the\n# header note.\n\nsdk.dir=E\\:\\\\Android-Sdk\n\n#\u5bf9\u5e94\u81ea\u5df1\u5b9e\u9645\u7684\u8bc1\u4e66\u8def\u5f84\u548c\u540d\u5b57\uff0c\u5728\u8fd9\u91cc\u7531\u4e8e\u7b7e\u540d\u6587\u4ef6\u662f\u653e\u5728app\u76ee\u5f55\u4e0b\uff0c\u6ca1\u6709\u5199\u7edd\u5bf9\u8def\u5f84\nkeystroe_storeFile=keystore.jks\nkeystroe_storePassword=123456\nkeystroe_keyAlias=HomeKey\nkeystroe_keyPassword=123456<\/code><\/pre>\n<h2>\u6ce8\u610f\u4e8b\u9879<\/h2>\n<h3>signingConfigs<\/h3>\n<p>signingConfigs\u4e2d\u4e3b\u8981\u662f\u4e3a\u6253\u5305\u914d\u7f6e\u7b7e\u540d\u6587\u4ef6\u5177\u4f53\u4fe1\u606f\u7684\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e24\u79cd\u65b9\u5f0f<\/p>\n<p>\u7b2c\u4e00\u79cd\u65b9\u5f0f\u628a\u7b7e\u540d\u6587\u4ef6\u7684\u4f4d\u7f6e\u3001storePassword\u3001keyAlias\u3001keyPassword \u7b49\u5177\u4f53\u5185\u5bb9\u90fd\u76f4\u63a5\u5199\u5728\u5176\u4e2d\uff0c\u7136\u540e\u4f7f\u7528Gradle\u8fdb\u884c\u6253\u5305<br \/>\n\u7b2c\u4e8c\u79cd\u662f\u901a\u8fc7\u4f7f\u7528local.properties\u6587\u4ef6\u95f4\u63a5\u52a0\u8f7d\u7b7e\u540d\u6587\u4ef6\u7684\u5177\u4f53\u4fe1\u606f\u3002<\/p>\n<p>\u4e00\u822c\u6211\u4eec\u66f4\u503e\u5411\u4e8e\u7b2c\u4e8c\u79cd\u65b9\u6cd5\uff0c\u8fd9\u6837\u6709\u52a9\u4e8e\u4fdd\u62a4\u7b7e\u540d\u6587\u4ef6\uff08\u5728local.properties\u4e2d\u4e0d\u80fd\u6709\u4e2d\u6587\uff09<\/p>\n<h3>productFlavors<\/h3>\n<p>\u4e0d\u540c\u6e20\u9053\u7684\u8bbe\u7f6e\u57fa\u672c\u90fd\u662f\u5728 productFlavors \u4e2d\u8bbe\u7f6e\u7684\uff0c\u5728\u91cc\u9762\u60f3\u8981\u6dfb\u52a0\u591a\u5c11\u4e2a\u6e20\u9053\u90fd\u53ef\u4ee5\u3002<\/p>\n<h4>\u4fee\u6539app\u540d\u79f0<\/h4>\n<pre><code class=\"language-java\">resValue &quot;string&quot;, &quot;app_name&quot;, &quot;\u817e\u8baf&quot;\nresValue &quot;bool&quot;, &quot;auto_updates&quot;, &#039;true&#039;<\/code><\/pre>\n<p>\u901a\u8fc7resValue \u6211\u4eec\u53ef\u4ee5\u5728 string.xml \u91cc\u9762\u6dfb\u52a0\u4e00\u4e2a\u65b0\u7684\u5b57\u6bb5app_name\uff0c\u7531\u4e8e\u662f\u6dfb\u52a0\uff0c\u6240\u4ee5\u539f\u6765\u7684string.xml \u6587\u4ef6\u4e2d\u4e0d\u80fd\u5b58\u5728app_name\u5b57\u6bb5\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002 <\/p>\n<p>\u5f53\u7136\u6211\u4eec\u8fd8\u53ef\u4ee5\u6dfb\u52a0\u5e03\u5c14\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u4e3acolor.xml\u3001dimen.xml\u6dfb\u52a0\u4e00\u4e9b\u6211\u4eec\u9700\u8981\u7684\u5b57\u6bb5\u3002<\/p>\n<h4>\u4fee\u6539app\u56fe\u6807<\/h4>\n<p>\u5f53\u6211\u4eec\u5728productFlavors \u4e2d\u6dfb\u52a0\u4e86\u4e0d\u540c\u6e20\u9053\u73af\u5883\u540d\u79f0\u4e4b\u540e\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728mian\u6587\u4ef6\u5939\u540c\u5c42\u7ea7\u4e2d\u5efa\u7acb\u548cbaidu\uff0cqq\uff0cxiaomi\u540d\u79f0\u5bf9\u5e94\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u653e\u5165\u7279\u5b9a\u7684\u56fe\u6807\u6587\u4ef6\uff0c\u5f53\u7136\u6211\u4eec\u8fd8\u53ef\u4ee5\u653e\u5165\u5176\u4ed6\u8d44\u6e90\u6587\u4ef6\uff0c\u751a\u81f3AndroidManifest.xml\u90fd\u53ef\u4ee5\u653e\u5165\uff0cGradle\u5728\u6784\u5efa\u5e94\u7528\u65f6\uff0c\u4f1a\u4f18\u5148\u4f7f\u7528flavor\u6240\u5c5edataSet\u4e2d\u7684\u540c\u540d\u8d44\u6e90\u3002\u6240\u4ee5\uff0c\u5728flavor\u7684dataSet\u4e2d\u6dfb\u52a0\u540c\u540d\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u4f1a\u8986\u76d6\u9ed8\u8ba4\u7684\u8d44\u6e90\u6587\u4ef6\uff0c\u8fd9\u6837\u5c31\u80fd\u8fbe\u5230\u4e0d\u540c\u73af\u5883\u4e0d\u540c\u8f6f\u4ef6\u56fe\u6807\u7684\u529f\u80fd\u3002<\/p>\n<h4>\u4e0d\u540c\u73af\u5883\uff0c\u4fee\u6539 AndroidManifest.xml \u91cc\u6e20\u9053\u53d8\u91cf<\/h4>\n<p>\u2460 \u5728 AndroidManifest.xml \u91cc\u6dfb\u52a0\u6e20\u9053\u53d8\u91cf<\/p>\n<pre><code class=\"language-xml\">&lt;application\n    android:icon=&quot;${app_icon}&quot;\n    android:label=&quot;@string\/app_name&quot;\n    android:theme=&quot;@style\/AppTheme&quot;&gt;\n    ...\n    &lt;meta-data\n        android:name=&quot;CHANNEL&quot;\n        android:value=&quot;${CHANNEL_VALUE}&quot; \/&gt;\n    ...\n&lt;\/application&gt;<\/code><\/pre>\n<p>\u2461 \u5728 build.gradle \u8bbe\u7f6e productFlavors<\/p>\n<pre><code class=\"language-java\">productFlavors {\n    baidu {\n        manifestPlaceholders = [CHANNEL_VALUE: &quot;baidu&quot;, app_icon: &quot;@mipmap\/logo_baidu&quot;]\n    }\n    qq {\n        manifestPlaceholders = [CHANNEL_VALUE: &quot;qq&quot;, app_icon: &quot;@mipmap\/icon_qq&quot;]]\n    }\n    xiaomi {\n        manifestPlaceholders = [CHANNEL_VALUE: &quot;xiaomi&quot;, app_icon: &quot;@mipmap\/icon_xiaomi&quot;]]\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u540c\u73af\u5883\u4f7f\u7528\u4e0d\u540c\u7684 key \u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u4e00\u5957\u4ee3\u7801\uff0c\u6253\u5305N\u5957APP\uff0c\u800c\u4e14\u8fd9\u4e9bAPP\u7684\u540d\u79f0\u3001\u56fe\u6807\u3001applicationId\u3001\u7248\u672c\u53f7\uff0c\u751a\u81f3\u4e3b\u9875\u90fd\u4e0d\u4e00\u6837\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[202],"tags":[64],"class_list":["post-717","post","type-post","status-publish","format-standard","hentry","category-android-build","tag-gradle"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/717","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=717"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/717\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}