{"id":1243,"date":"2023-03-18T09:35:19","date_gmt":"2023-03-18T01:35:19","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1243"},"modified":"2023-04-29T09:30:59","modified_gmt":"2023-04-29T01:30:59","slug":"using-variables-declared-in-gradle-in-java","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/using-variables-declared-in-gradle-in-java\/","title":{"rendered":"Java\u4e2d\u4f7f\u7528Gradle\u4e2d\u58f0\u660e\u7684\u53d8\u91cf"},"content":{"rendered":"<h2>\u95ee\u9898\u63cf\u8ff0<\/h2>\n<p>\u662f\u5426\u53ef\u4ee5\u5728Gradle\u4e2d\u58f0\u660e\u4e00\u4e2a\u53ef\u7528\u4e8eJava\u7684\u53d8\u91cf\uff1f\u5373\u5728build.gradle\u4e2d\u58f0\u660e\u4e00\u4e9bvars\uff0c\u7136\u540e\u5728\u6784\u5efa\u65f6\u5f97\u5230\u8fd9\u4e9b\u53d8\u91cf\u3002\u5c31\u50cf\u5728C\/C++\u4e2d\u7684pre-processor\u5b8f\u2026<\/p>\n<p>\u53c2\u8003\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/stackoverflow.com\/questions\/17197636\/is-it-possible-to-declare-a-variable-in-gradle-usable-in-java\">https:\/\/stackoverflow.com\/questions\/17197636\/is-it-possible-to-declare-a-variable-in-gradle-usable-in-java<\/a><\/p>\n<p><!-- more --><\/p>\n<h2>\u6700\u4f73\u89e3\u51b3\u65b9\u6848<\/h2>\n<h3>\u751f\u6210Java\u5e38\u91cf<\/h3>\n<pre><code class=\"language-java\">android {\n    buildTypes {\n        debug {\n            buildConfigField &quot;int&quot;, &quot;FOO&quot;, &quot;42&quot;\n            buildConfigField &quot;String&quot;, &quot;FOO_STRING&quot;, &quot;\\&quot;foo\\&quot;&quot;\n            buildConfigField &quot;boolean&quot;, &quot;LOG&quot;, &quot;true&quot;\n        }\n\n        release {\n            buildConfigField &quot;int&quot;, &quot;FOO&quot;, &quot;52&quot;\n            buildConfigField &quot;String&quot;, &quot;FOO_STRING&quot;, &quot;\\&quot;bar\\&quot;&quot;\n            buildConfigField &quot;boolean&quot;, &quot;LOG&quot;, &quot;false&quot;\n        }\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>BuildConfig.FOO<\/code>\u8bbf\u95ee<\/p>\n<h3>\u751f\u6210Android\u8d44\u6e90<\/h3>\n<pre><code class=\"language-java\">android {\n    buildTypes {\n        debug {\n            resValue &quot;string&quot;, &quot;app_name&quot;, &quot;My App Name Debug&quot;\n        }\n        release {\n            resValue &quot;string&quot;, &quot;app_name&quot;, &quot;My App Name Release&quot;\n        }\n    }\n}<\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528<code>@string\/app_name<\/code>\u6216<code>R.string.app_name<\/code>\u4ee5\u5e38\u89c4\u65b9\u5f0f\u8bbf\u95ee<\/p>\n<h2>\u6b21\u4f73\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>Store api keys with help of gradle and the gradle.properties file (Java + XML)<\/p>\n<ul>\n<li>gradle.properties<\/li>\n<\/ul>\n<pre><code>AppKey=&quot;XXXX-XXXX&quot;<\/code><\/pre>\n<ul>\n<li>build.gradle<\/li>\n<\/ul>\n<pre><code class=\"language-java\">buildTypes {\n\/\/...\n    buildTypes.each {\n        it.buildConfigField &#039;String&#039;, &#039;APP_KEY_1&#039;, AppKey\n        it.resValue &#039;string&#039;, &#039;APP_KEY_2&#039;, AppKey\n    }\n}<\/code><\/pre>\n<ul>\n<li>Usage in java code<\/li>\n<\/ul>\n<pre><code class=\"language-java\">Log.d(&quot;UserActivity&quot;, &quot;onCreate, APP_KEY: &quot; + getString(R.string.APP_KEY_2));\n\nBuildConfig.APP_KEY_1<\/code><\/pre>\n<ul>\n<li>Usage in xml code<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;data android:scheme=&quot;@string\/APP_KEY_2&quot; \/&gt;<\/code><\/pre>\n<p><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/gist.github.com\/VDenis\/46c222b16683447bab33\" title=\"Link to an example of Api App Key usage in an Android application\">Link to an example of Api App Key usage in an Android application<\/a><\/p>\n<p><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/michiganlabs.com\/2014\/09\/23\/string-constants-generated-gradle-build-configurations\/\" title=\"Using String Constants Generated by Gradle Build Configurations\">Using String Constants Generated by Gradle Build Configurations<\/a><\/p>\n<h2>\u7b2c\u4e09\u79cd\u89e3\u51b3\u65b9\u6848<\/h2>\n<p>\u4f7f\u7528\u7cfb\u7edf\u5c5e\u6027\uff0c\u5728build.gradle\u4e2d\u8bbe\u7f6e\uff0c\u4eceJava\u5e94\u7528\u7a0b\u5e8f\u8bfb\u53d6<\/p>\n<p>\u5728build.gradle\u4e2d\u4f7f\u7528test\u4efb\u52a1\uff0c\u4f7f\u7528\u6d4b\u8bd5\u4efb\u52a1\u65b9\u6cd5systemProperty\u8bbe\u7f6e\u5728\u8fd0\u884c\u65f6\u4f20\u9012\u7684\u7cfb\u7edf\u5c5e\u6027\uff1a<\/p>\n<pre><code class=\"language-java\">apply plugin: &#039;java&#039;\ngroup = &#039;example&#039;\nversion = &#039;0.0.1-SNAPSHOT&#039;\n\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    testCompile &#039;junit:junit:4.8.2&#039;\n    compile &#039;ch.qos.logback:logback-classic:1.1.2&#039;\n}\n\ntest {\n    logger.info &#039;==test==&#039;\n    systemProperty &#039;MY-VAR1&#039;, &#039;VALUE-TEST&#039;\n}<\/code><\/pre>\n<p>Java\u4e2d\u83b7\u53d6\u7cfb\u7edf\u5c5e\u6027MY-VAR1\uff0c\u5728runtime\u88ab\u8bbe\u7f6e\u4e3aVALUE-TEST\uff1a<\/p>\n<pre><code class=\"language-java\">package example;\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\n\npublic class HelloWorld {\n    static final Logger log = LoggerFactory.getLogger(HelloWorld.class);\n    public static void main(String args[]) {\n        log.info(&quot;entering main...&quot;);\n        final String val = System.getProperty(&quot;MY-VAR1&quot;, &quot;UNSET (MAIN)&quot;);\n        System.out.println(&quot;(main.out) hello, world: &quot; + val);\n        log.info(&quot;main.log) MY-VAR1=&quot; + val);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u63cf\u8ff0 \u662f\u5426\u53ef\u4ee5\u5728Gradle\u4e2d\u58f0\u660e\u4e00\u4e2a\u53ef\u7528\u4e8eJava\u7684\u53d8\u91cf\uff1f\u5373\u5728build.gradle\u4e2d\u58f0\u660e\u4e00\u4e9bvars [&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-1243","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\/1243","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=1243"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1243\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}