{"id":1369,"date":"2023-03-19T10:27:16","date_gmt":"2023-03-19T02:27:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1369"},"modified":"2023-04-28T21:16:59","modified_gmt":"2023-04-28T13:16:59","slug":"android-gradle-config-debug-and-release-parameters-methods","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/android-gradle-config-debug-and-release-parameters-methods\/","title":{"rendered":"Android Gradle\u914d\u7f6eDebug\u548cRelease\u53c2\u6570\u7684\u65b9\u6cd5"},"content":{"rendered":"<h2>\u4f7f\u7528BuildConfig\u7c7b\u4fee\u6539\u53c2\u6570\u503c<\/h2>\n<p>Gradle Android\u81ea\u5e26BuildConfig\u914d\u7f6e\u7c7b\uff0c\u5728build\u5de5\u7a0b\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5728build\/generated\/source\/buildConfig\u4e0b\u7684debug\u548crelease\u8def\u5f84\u4e0b\u627e\u5230\u3002<\/p>\n<p><code>BuildConfig.java<\/code>\u7c7b\u53ef\u4ee5\u5728\u5de5\u7a0b\u4e2d\u5f15\u7528\u3002<code>BuildConfig.java<\/code>\u81ea\u5e26DEBUG\u53c2\u6570\uff0c\u5728\u6253\u5305debug\u7248\u672c\u65f6<code>DEBUG = true<\/code>\uff0c\u5728\u6253\u5305release\u7248\u672c\u65f6<code>DEBUG = false<\/code>\u3002<\/p>\n<p><!-- more --><\/p>\n<p>\u5982\u4f55\u81ea\u5b9a\u4e49BuildConfig\u53c2\u6570\u5462\uff1f\u4f7f\u7528buildConfigField\u8fdb\u884c\u914d\u7f6e\uff1a<\/p>\n<pre><code class=\"language-java\">buildTypes {\n    debug {\n        buildConfigField &quot;String&quot;, &quot;CustomValue&quot;, &#039;&quot;buildType&quot;&#039;\n        buildConfigField &quot;boolean&quot;, &quot;LOG_DEBUG&quot;, &quot;true&quot;\n    }\n    release {\n        buildConfigField &quot;String&quot;, &quot;CustomValue&quot;, &#039;&quot;releaseType&quot;&#039;\n        buildConfigField &quot;boolean&quot;, &quot;LOG_DEBUG&quot;, &quot;false&quot;\n    }\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\uff1abuildConfigField\u914d\u7f6e\u683c\u5f0f\u4e3a\uff1a&quot;type&quot;,&quot;name&quot;,&quot;value&quot; \u5f62\u5f0f\uff0c\u5982\u679c\u53c2\u6570\u662fString\u7c7b\u578b\uff0c&quot;value&quot;\u5916\u90e8\u9700\u52a0\u5355\u5f15\u53f7\u7533\u660e\u5185\u90e8\u662fString\u7c7b\u578b\u7684\uff0c\u6216\u8005\u4f7f\u7528\u8f6c\u79fb\u7b26 \\ ,\u5982\uff1a<br \/>\n&quot;\\&quot;buildType\\&quot;&quot;<\/p>\n<\/blockquote>\n<p>\u8fd9\u6837\u5728build\u5de5\u7a0b\u7684\u65f6\u5019\uff0c\u751f\u6210\u7684BuildConfig.java\u4e2d\u5c31\u5305\u542b\u4e86\u8fd9\u4e9b\u53c2\u6570\uff1a<\/p>\n<pre><code class=\"language-java\">public final class BuildConfig {\npublic static final boolean DEBUG = false;\n  public static final String APPLICATION_ID = &quot;cn.appblog.example&quot;;\n  public static final String BUILD_TYPE = &quot;release&quot;;\n  public static final String FLAVOR = &quot;&quot;;\n  public static final int VERSION_CODE = 1;\n  public static final String VERSION_NAME = &quot;1.0.0&quot;;\n  \/\/ Fields from build type: release\n  public static final String CustomValue = &quot;releaseType&quot;;\n}<\/code><\/pre>\n<p>\u6700\u540e\uff0c\u5728\u4ee3\u7801\u4e2d\u8d4b\u503c\uff1a<\/p>\n<pre><code class=\"language-java\">\/** \u662f\u5426\u5f00\u53d1\u6a21\u5f0f *\/\nprivate static final boolean isDebug = BuildConfig.DEBUG;<\/code><\/pre>\n<h2>\u4f7f\u7528AndroidManifest.xml\u4e2d\u7684meta-data<\/h2>\n<p>\u5728AndroidManifest.xml\u4e2d\u7533\u660emeta-data\u53c2\u6570\uff0c\u6ce8\u610fvalue\u7684\u8bed\u6cd5<\/p>\n<pre><code class=\"language-xml\">&lt;meta-data\n    android:name=&quot;Logs.enable&quot;\n    android:value=&quot;${LogEnable}&quot;\n    \/&gt;<\/code><\/pre>\n<p>\u7136\u540e\u5728gradle\u4e2d\u914d\u7f6ebuildTypes;<\/p>\n<pre><code class=\"language-java\">buildTypes {\n    debug {\n        manifestPlaceholders = [LogEnable : true]\n    }\n    release {\n        manifestPlaceholders = [LogEnable : false]\n    }\n}<\/code><\/pre>\n<p>\u6700\u540e\u5728\u5de5\u7a0b\u4ee3\u7801\u4e2d\u52a0\u8f7dmeta-data\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-java\">ApplicationInfo appInfo = null;\ntry {\n    appInfo = this.getPackageManager()\n                    .getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);\n    boolean enable = appInfo.metaData.getBoolean(&quot;Logs.enable&quot;);\n    Log.d(&quot;yezhou&quot;, &quot;Logs.enable = &quot; + enable);\n} catch (PackageManager.NameNotFoundException e) {\n    e.printStackTrace();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528BuildConfig\u7c7b\u4fee\u6539\u53c2\u6570\u503c Gradle Android\u81ea\u5e26BuildConfig\u914d\u7f6e\u7c7b\uff0c\u5728bui [&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-1369","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\/1369","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=1369"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1369\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}