{"id":5341,"date":"2015-02-10T00:42:02","date_gmt":"2015-02-09T22:42:02","guid":{"rendered":"http:\/\/rainbowbreeze.it\/?p=5341"},"modified":"2015-02-10T00:46:10","modified_gmt":"2015-02-09T22:46:10","slug":"environmental-variables-api-key-and-secret-buildconfig-and-android-studio","status":"publish","type":"post","link":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/","title":{"rendered":"Environmental variables, API key and secret, BuildConfig and Android Studio"},"content":{"rendered":"<p>You wanna create an Android app that uses Twittet APIs, so yo need an API key and an API secrets only you and your apps know. Because you need these values inside you app, it&#8217;s easy and quick to write them\u00a0down directly in your source code. But when you commit this code to GitHub (or any other public repo), practically you&#8217;re telling your secret keys to entire world. Seems uncommon? Unfortunately, <a href=\"https:\/\/github.com\/search?p=2&amp;q=setOAuthConsumerKey&amp;type=Code&amp;utf8=%E2%9C%93\">not so much<\/a>! Same for <a href=\"https:\/\/github.com\/search?l=java&amp;q=APP_SECRET+%3D+%22&amp;ref=searchresults&amp;type=Code&amp;utf8=%E2%9C%93\">Dropbox SDK<\/a>.<\/p>\n<p>One simple way to avoid this bad practice is to store your values inside an environmental variable, so only your machine knows it, then read this values\u00a0in some way and inject them\u00a0in your code at build time.<br \/>\nLet&#8217;s see how to do that using <strong>Android Studio<\/strong>, <strong>Gradle<\/strong>, and <strong><a href=\"http:\/\/toastdroid.com\/2014\/03\/28\/customizing-your-build-with-gradle\/\">BuildConfig<\/a><\/strong>.<\/p>\n<p>First, we need to create these environmental vars. In Linux and Mac, create or edit the file\u00a0<strong><em>~\/.gradle\/gradle.properties<\/em><\/strong> (pay attention to the actual Gradle User Home directory position) and add some values:<\/p>\n<pre lang=\"bash\" highlight=\"2\">#define your secret values\r\nTwitterConsumerKeyProp=xxxxxx66666666634333333dddddd\r\nTwitterConsumerSecretProp=3nkl3sds3skmslSDF394asdk39dmasd\r\n<\/pre>\n<p>Second, in your module&#8217;s <em><strong>build.gradle<\/strong><\/em> file, add these lines<\/p>\n<pre lang=\"scala\" highlight=\"4,8\">apply plugin: 'com.android.application'\r\n\r\n\/\/Add these lines\r\ndef TWITTER_CONSUMER_KEY = '\"' + TwitterConsumerKeyProp + '\"' ?: '\"Define Twitter Consumer key\"';\r\ndef TWITTER_CONSUMER_SECRET = '\"' + TwitterConsumerSecretProp + '\"' ?: '\"Define Twitter Consumer secret\"';\r\n\r\nandroid.buildTypes.each { type -&gt;\r\n    type.buildConfigField 'String', 'TWITTER_CONSUMER_KEY', TWITTER_CONSUMER_KEY\r\n    type.buildConfigField 'String', 'TWITTER_CONSUMER_SECRET', TWITTER_CONSUMER_SECRET\r\n}\r\n<\/pre>\n<p>Please note the &#8220;<em>TwitterConsumerKeyProp<\/em>&#8221; and &#8220;<em>TwitterConsumerSecretProp<\/em>&#8221; have to be the same in both Gradle settings file and Gradle build file.<br \/>\nFinally, to use these values in your code, filled at runtime by Gradle in the build script for you, simply use:<\/p>\n<pre lang=\"java\" highlight=\"4\">ConfigurationBuilder cb = new ConfigurationBuilder()\r\n    .setDebugEnabled(BuildConfig.DEBUG)\r\n    .setApplicationOnlyAuthEnabled(true)\r\n    .setOAuthConsumerKey(BuildConfig.TWITTER_CONSUMER_KEY)\r\n    .setOAuthConsumerSecret(BuildConfig.TWITTER_CONSUMER_SECRET);\r\n<\/pre>\n<p>That&#8217;s all, then it&#8217;s up to you how to create more elaborated configurations. For example, you can have different values based on different\u00a0<em>android.buildTypes<\/em> types, or the gradle settings file in a common network folder used by the entire team or&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You wanna create an Android app that uses Twittet APIs, so yo need an API key and an API secrets only you and your apps know. Because you need these values inside you app, it&#8217;s easy and quick to write them\u00a0down directly in your source code. But when you commit this code to GitHub (or &hellip; <a href=\"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Environmental variables, API key and secret, BuildConfig and Android Studio<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[520],"tags":[620],"class_list":["post-5341","post","type-post","status-publish","format-standard","hentry","category-dev","tag-android"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze<\/title>\n<meta name=\"description\" content=\"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze\" \/>\n<meta property=\"og:description\" content=\"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere\" \/>\n<meta property=\"og:url\" content=\"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/\" \/>\n<meta property=\"og:site_name\" content=\"Rainbowbreeze\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-09T22:42:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-02-09T22:46:10+00:00\" \/>\n<meta name=\"author\" content=\"Alfredo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@rainbowbreeze\" \/>\n<meta name=\"twitter:site\" content=\"@rainbowbreeze\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alfredo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/\"},\"author\":{\"name\":\"Alfredo\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#\\\/schema\\\/person\\\/00e63c64a3939938a7467e3aaf54f4cf\"},\"headline\":\"Environmental variables, API key and secret, BuildConfig and Android Studio\",\"datePublished\":\"2015-02-09T22:42:02+00:00\",\"dateModified\":\"2015-02-09T22:46:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/\"},\"wordCount\":273,\"commentCount\":8,\"publisher\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#\\\/schema\\\/person\\\/00e63c64a3939938a7467e3aaf54f4cf\"},\"keywords\":[\"Android\"],\"articleSection\":[\"Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/\",\"url\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/\",\"name\":\"Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#website\"},\"datePublished\":\"2015-02-09T22:42:02+00:00\",\"dateModified\":\"2015-02-09T22:46:10+00:00\",\"description\":\"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/rainbowbreeze.it\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Environmental variables, API key and secret, BuildConfig and Android Studio\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#website\",\"url\":\"https:\\\/\\\/rainbowbreeze.it\\\/\",\"name\":\"Rainbowbreeze\",\"description\":\"Alfredo Morresi&#039;s digital harbor\",\"publisher\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#\\\/schema\\\/person\\\/00e63c64a3939938a7467e3aaf54f4cf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/rainbowbreeze.it\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/#\\\/schema\\\/person\\\/00e63c64a3939938a7467e3aaf54f4cf\",\"name\":\"Alfredo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/rainbowbreeze_profile_700px.jpg\",\"url\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/rainbowbreeze_profile_700px.jpg\",\"contentUrl\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/rainbowbreeze_profile_700px.jpg\",\"width\":700,\"height\":700,\"caption\":\"Alfredo\"},\"logo\":{\"@id\":\"https:\\\/\\\/rainbowbreeze.it\\\/wordpress\\\/wp-content\\\/uploads\\\/2021\\\/03\\\/rainbowbreeze_profile_700px.jpg\"},\"description\":\"79ers, Italian, with a passion for creating environments in which great ideas can happen and people feel empowered, thanks to community dynamics and technological innovation. I also love nature, open-source, running on the beach, snowboarding and Tiramisu\u2019\",\"sameAs\":[\"https:\\\/\\\/rainbowbreeze.it\\\/\",\"https:\\\/\\\/www.instagram.com\\\/rainbowbreez3\\\/\",\"https:\\\/\\\/linkedin.com\\\/in\\\/alfredomorresi\",\"https:\\\/\\\/x.com\\\/rainbowbreeze\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze","description":"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/","og_locale":"en_US","og_type":"article","og_title":"Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze","og_description":"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere","og_url":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/","og_site_name":"Rainbowbreeze","article_published_time":"2015-02-09T22:42:02+00:00","article_modified_time":"2015-02-09T22:46:10+00:00","author":"Alfredo","twitter_card":"summary_large_image","twitter_creator":"@rainbowbreeze","twitter_site":"@rainbowbreeze","twitter_misc":{"Written by":"Alfredo","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/#article","isPartOf":{"@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/"},"author":{"name":"Alfredo","@id":"https:\/\/rainbowbreeze.it\/#\/schema\/person\/00e63c64a3939938a7467e3aaf54f4cf"},"headline":"Environmental variables, API key and secret, BuildConfig and Android Studio","datePublished":"2015-02-09T22:42:02+00:00","dateModified":"2015-02-09T22:46:10+00:00","mainEntityOfPage":{"@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/"},"wordCount":273,"commentCount":8,"publisher":{"@id":"https:\/\/rainbowbreeze.it\/#\/schema\/person\/00e63c64a3939938a7467e3aaf54f4cf"},"keywords":["Android"],"articleSection":["Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/","url":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/","name":"Environmental variables, API key and secret, BuildConfig and Android Studio - Rainbowbreeze","isPartOf":{"@id":"https:\/\/rainbowbreeze.it\/#website"},"datePublished":"2015-02-09T22:42:02+00:00","dateModified":"2015-02-09T22:46:10+00:00","description":"How to use environmental variables, gradle settings file and BuildConf to have API key and secret without committing them on GitHub or elsewhere","breadcrumb":{"@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/rainbowbreeze.it\/wordpress\/environmental-variables-api-key-and-secret-buildconfig-and-android-studio\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/rainbowbreeze.it\/"},{"@type":"ListItem","position":2,"name":"Environmental variables, API key and secret, BuildConfig and Android Studio"}]},{"@type":"WebSite","@id":"https:\/\/rainbowbreeze.it\/#website","url":"https:\/\/rainbowbreeze.it\/","name":"Rainbowbreeze","description":"Alfredo Morresi&#039;s digital harbor","publisher":{"@id":"https:\/\/rainbowbreeze.it\/#\/schema\/person\/00e63c64a3939938a7467e3aaf54f4cf"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/rainbowbreeze.it\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/rainbowbreeze.it\/#\/schema\/person\/00e63c64a3939938a7467e3aaf54f4cf","name":"Alfredo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/rainbowbreeze.it\/wordpress\/wp-content\/uploads\/2021\/03\/rainbowbreeze_profile_700px.jpg","url":"https:\/\/rainbowbreeze.it\/wordpress\/wp-content\/uploads\/2021\/03\/rainbowbreeze_profile_700px.jpg","contentUrl":"https:\/\/rainbowbreeze.it\/wordpress\/wp-content\/uploads\/2021\/03\/rainbowbreeze_profile_700px.jpg","width":700,"height":700,"caption":"Alfredo"},"logo":{"@id":"https:\/\/rainbowbreeze.it\/wordpress\/wp-content\/uploads\/2021\/03\/rainbowbreeze_profile_700px.jpg"},"description":"79ers, Italian, with a passion for creating environments in which great ideas can happen and people feel empowered, thanks to community dynamics and technological innovation. I also love nature, open-source, running on the beach, snowboarding and Tiramisu\u2019","sameAs":["https:\/\/rainbowbreeze.it\/","https:\/\/www.instagram.com\/rainbowbreez3\/","https:\/\/linkedin.com\/in\/alfredomorresi","https:\/\/x.com\/rainbowbreeze"]}]}},"_links":{"self":[{"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/posts\/5341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/comments?post=5341"}],"version-history":[{"count":0,"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/posts\/5341\/revisions"}],"wp:attachment":[{"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/media?parent=5341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/categories?post=5341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rainbowbreeze.it\/wordpress\/wp-json\/wp\/v2\/tags?post=5341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}