{"id":348,"date":"2023-02-25T07:04:27","date_gmt":"2023-02-24T23:04:27","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=348"},"modified":"2023-04-30T14:47:05","modified_gmt":"2023-04-30T06:47:05","slug":"how-android-distinguish-between-debug-and-release","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/how-android-distinguish-between-debug-and-release\/","title":{"rendered":"Android\u5982\u4f55\u533a\u5206debug\u548crelease\u4e24\u79cd\u72b6\u6001"},"content":{"rendered":"<h2>BuildConfig.DEBUG<\/h2>\n<p>Android\u5f00\u53d1\u4e2d\u8bc6\u522bdebug\u8fd8\u662frelease\u72b6\u6001\u8fd8\u662f\u5f88\u6709\u7528\u7684\uff0c\u6bd4\u5982\u8bf4\u6253\u5370\u65e5\u5fd7\uff0c\u6709\u4e9b\u65e5\u5fd7\u5f00\u53d1\u7684\u65f6\u5019\u9700\u8981\uff0c\u53ef\u662f\u7ebf\u4e0a\u6b63\u5f0f\u5305\u4e0d\u9700\u8981\uff0c\u8fd9\u65f6\u77e5\u9053debug\u72b6\u6001\u5c31\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u9690\u85cf\u975e\u5fc5\u8981\u65e5\u5fd7\u800c\u53c8\u4e0d\u5f71\u54cd\u5f00\u53d1\u3002<\/p>\n<p>\u4e00\u822c\u4f7f\u7528<code>BuildConfig.DEBUG<\/code>\u6765\u83b7\u53d6\u5e94\u7528\u7684\u72b6\u6001\uff0cdebug\u5305\u8fd4\u56detrue, release\u8fd4\u56defalse<\/p>\n<p>\u4f46\u662f\u5728\u4e3bMoudle\u91cc\u662f\u597d\u4f7f\u7684\uff0c\u5728Library\u91cc\u9762\uff0c\u65e0\u8bba\u662fdebug\u5305\u8fd8\u662frelease\u90fd\u662f\u8fd4\u56defalse<\/p>\n<p><!-- more --><\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\uff1a<code>BuildConfig<\/code>\u662fbuild\u8fc7\u7a0b\u4e2d\u751f\u6210\u7684\u6587\u4ef6\uff0c\u5728Library\u7684<code>build.gradle<\/code>\u4e2d\u914d\u7f6e<\/p>\n<pre><code>gradle.startParameter.getTaskNames().each { task -&gt;\n    println(&quot;task: &quot; + task)\n    \/\/library\u91cc BuildConfig.DEBUG\u9ed8\u8ba4\u4e00\u76f4\u662fflase\uff0c\u6240\u4ee5\u9700\u8981\u81ea\u5b9a\u4e49\n    if (task.contains(&quot;Debug&quot;)) {\n        android {\n            defaultPublishConfig &quot;debug&quot;\n        }\n    } else if (task.contains(&quot;Release&quot;)) {\n        android {\n            defaultPublishConfig &quot;release&quot;\n        }\n    }\n}<\/code><\/pre>\n<p>\u914d\u7f6e\u540e\uff0c<code>BuildConfig.DEBUG<\/code>\u5373\u53ef\u8fd4\u56de\u6b63\u5e38\u503c<\/p>\n<h2>ApplicationInfo.FLAG_DEBUGGABLE<\/h2>\n<p>\u90a3\u4e48\u8fd8\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u8bc6\u522bdebug\u548crelease\u4e24\u79cd\u72b6\u6001\u5462\uff1f\u53ef\u4ee5\u901a\u8fc7\u83b7\u53d6\u6807\u5fd7<code>ApplicationInfo.FLAG_DEBUGGABLE<\/code><\/p>\n<pre><code class=\"language-java\">public boolean isDebug(Context context) {\n    boolean isDebug = context.getApplicationInfo() != null &amp;&amp;\n        (context.getApplicationInfo().flags &amp; ApplicationInfo.FLAG_DEBUGGABLE) != 0;\n  return isDebug;\n}<\/code><\/pre>\n<blockquote>\n<p>\u8be5\u65b9\u6848\u6709\u4e2a\u6ce8\u610f\u4e8b\u9879\u5c31\u662f\uff1aApp Module \u7684\u6e05\u5355\u6587\u4ef6\u4e2d\u4e0d\u80fd\u4e3b\u52a8\u8bbe\u7f6e android:debuggable\uff0c\u5426\u5219\u65e0\u8bba Debug \u8fd8\u662f Release \u7248\u4f1a\u59cb\u7ec8\u662f\u8bbe\u7f6e\u7684\u503c\u3002\u5f53\u7136\u672c\u8eab\u5c31\u6ca1\u6709\u4e3b\u52a8\u8bbe\u7f6e\u7684\u5fc5\u8981\u3002<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>BuildConfig.DEBUG Android\u5f00\u53d1\u4e2d\u8bc6\u522bdebug\u8fd8\u662frelease\u72b6\u6001\u8fd8\u662f\u5f88\u6709\u7528\u7684\uff0c\u6bd4\u5982 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-348","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/348","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=348"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/348\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}