{"id":539,"date":"2018-06-14T18:57:59","date_gmt":"2018-06-14T09:57:59","guid":{"rendered":"http:\/\/www.typea.info\/blog\/index.php\/2018\/06\/14\/android_kotlin_firebase\/"},"modified":"2018-06-14T18:57:59","modified_gmt":"2018-06-14T09:57:59","slug":"android_kotlin_firebase","status":"publish","type":"post","link":"https:\/\/www.typea.info\/blog\/index.php\/2018\/06\/14\/android_kotlin_firebase\/","title":{"rendered":"Android + Kotlin \u3067 Firebase\u306b\u3088\u308b\u30e1\u30fc\u30eb\u8a8d\u8a3c"},"content":{"rendered":"<p><a href=\"https:\/\/www.amazon.co.jp\/%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3%81%AE%E3%81%9F%E3%82%81%E3%81%AEGoogle-Cloud-Platform%E5%85%A5%E9%96%80-%E3%82%B5%E3%83%BC%E3%83%93%E3%82%B9%E3%81%AE%E5%85%A8%E4%BD%93%E5%83%8F%E3%81%8B%E3%82%89%E3%82%AF%E3%83%A9%E3%82%A6%E3%83%89%E3%83%8D%E3%82%A4%E3%83%86%E3%82%A3%E3%83%96%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E6%A7%8B%E7%AF%89%E3%81%BE%E3%81%A7-%E9%98%BF%E4%BD%90\/dp\/4798137146\/ref=as_li_ss_il?s=books&amp;ie=UTF8&amp;qid=1528970192&amp;sr=1-1&amp;keywords=GCP&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=490d6d2ddaa2b8a18158513907515267\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4798137146&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4798137146\" border=\"0\"><\/p>\n<p>\u4e45\u3005\u306bAndroid\u3002Android\u306b\u304b\u304b\u308f\u3089\u305a\u3001\u4f55\u304b\u30b5\u30fc\u30d3\u30b9\u306a\u308a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306a\u308a\u4f5c\u6210\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u30ed\u30b0\u30a4\u30f3\u306e\u4ed5\u7d44\u307f\u3060\u3068\u304b\u4f5c\u308b\u306e\u304c\u9762\u5012\u304f\u3055\u3044\u3001\u3068\u3044\u3046\u304b\u305d\u306e\u3042\u305f\u308a\u3092\u4f5c\u308a\u4e0a\u3052\u308b\u3068\u529b\u5c3d\u304d\u3066\u305d\u3082\u305d\u3082\u4f55\u304c\u3057\u305f\u304b\u3063\u305f\u306e\u304b\u308f\u304b\u3089\u306a\u304f\u306a\u308b\u3053\u3068\u304c\u307e\u307e\u3042\u308b\u3002<\/p>\n<p>\u81ea\u5206\u3092\u542b\u3081\u3001\u3068\u3044\u3046\u4eba\u306e\u305f\u3081\u306b\u3001<a href=\"https:\/\/www.google.co.jp\/search?q=Baas&amp;oq=Baas&amp;aqs=chrome.0.69i59j69i60j69i59.1040j0j4&amp;sourceid=chrome&amp;ie=UTF-8\">Baas(Backend as a service)<\/a> \u3068\u3044\u3046\u3001\u30d0\u30c3\u30af\u30a8\u30f3\u30c9\u6a5f\u69cb\u3092\u30b5\u30fc\u30d3\u30b9\u3068\u3057\u3066\u63d0\u4f9b\u3059\u308b\u8003\u3048\u65b9\u304c\u3042\u308a\u3001Google\u306eBaas\u3001Firebase\u3092\u4f7f\u7528\u3057\u3066\u3001Android\u30a2\u30d7\u30ea\u306b\u30e1\u30fc\u30eb\u8a8d\u8a3c\u6a5f\u80fd\u3092\u7d44\u307f\u8fbc\u3080\u3002<\/p>\n<h2>1.Firebase\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u4f5c\u6210\u3057\u30e1\u30fc\u30eb\u8a8d\u8a3c\u3092\u6709\u52b9\u5316<\/h2>\n<p>Firebase\u306b\u30a2\u30ab\u30a6\u30f3\u30c8\u3092\u767b\u9332\u3057\u3066\u3001\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u767b\u9332\u3002\u7121\u6599\u3067\u59cb\u3081\u3089\u308c\u308b<\/p>\n<p><a title=\"https:\/\/firebase.google.com\/?hl=ja\" href=\"https:\/\/firebase.google.com\/?hl=ja\">https:\/\/firebase.google.com\/?hl=ja<\/a><\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_getstart.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"237\" height=\"244\" title=\"firebase_auth_getstart\" style=\"display: inline; background-image: none;\" alt=\"firebase_auth_getstart\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_getstart_thumb.jpg\" border=\"0\"><\/a><\/p>\n<p>\u30ed\u30b0\u30a4\u30f3\u30d7\u30ed\u30d0\u30a4\u30c0\u3001\u30e1\u30fc\u30eb\/\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u6709\u52b9\u306b\u3059\u308b<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_mail_enable.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"425\" height=\"182\" title=\"firebase_auth_mail_enable\" style=\"display: inline; background-image: none;\" alt=\"firebase_auth_mail_enable\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_mail_enable_thumb.jpg\" border=\"0\"><\/a><\/p>\n<h2>2.Android Studio \u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057Firebase\u30a2\u30b7\u30b9\u30bf\u30f3\u30c8\u3092\u4f7f\u7528<\/h2>\n<p><a title=\"https:\/\/firebase.google.com\/docs\/android\/setup?authuser=0\" href=\"https:\/\/firebase.google.com\/docs\/android\/setup?authuser=0\">https:\/\/firebase.google.com\/docs\/android\/setup?authuser=0<\/a><\/p>\n<p>Tools \u304b\u3089\u3001Firebase\u3092\u9078\u629e\u3059\u308b\u3068\u3001\u753b\u9762\u306e\u53f3\u306b\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u304c\u8d77\u52d5<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_assistant.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"270\" height=\"201\" title=\"firebase_assistant\" style=\"display: inline; background-image: none;\" alt=\"firebase_assistant\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_assistant_thumb.jpg\" border=\"0\"><\/a><\/p>\n<p>Authentication \u306e\u3001Email and password authentication \u3092\u5c55\u958b<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_assistant2.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"266\" height=\"301\" title=\"firebase_assistant2\" style=\"display: inline; background-image: none;\" alt=\"firebase_assistant2\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_assistant2_thumb.jpg\" border=\"0\"><\/a><\/p>\n<p>\u30a6\u30a3\u30b6\u30fc\u30c9\u5f62\u5f0f\u3067\u624b\u9806\u304c\u8868\u793a\u3055\u308c\u308b\u306e\u3067\u3001\u305d\u308c\u306b\u5f93\u3046\u3002<\/p>\n<p>Java\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u3092\u306a\u308c\u306a\u3044Kotlin\u306b\u8aad\u307f\u66ff\u3048\u3064\u3064\u3002<\/p>\n<h2>3.\u30d7\u30ed\u30b0\u30e9\u30e0\u306e\u4f5c\u6210<\/h2>\n<p>\u3068\u308a\u3042\u3048\u305a\u3001\u753b\u9762\u306b\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u5165\u529b\u6b04\u3001\u30e6\u30fc\u30b6\u30fc\u4f5c\u6210\u3068\u3001\u30b5\u30a4\u30f3\u30a4\u30f3\u30dc\u30bf\u30f3\u3092\u914d\u7f6e\u3057\u3001MainActivity\u306b\u4e0a\u8a18\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306e\u30b3\u30fc\u30c9\u3092Kotlin\u306b\u8aad\u307f\u66ff\u3048\u3064\u3064\u3001\u52d5\u4f5c\u3092\u7c21\u6613\u306b\u78ba\u8a8d\u3067\u304d\u308b\u30ec\u30d9\u30eb\u3067\u5b9f\u88c5\u3057\u3066\u307f\u308b\u3002<\/p>\n<p><pre class=\"prettyprint linenums\">package info.typea.sentence2\r\n\r\nimport android.support.v7.app.AppCompatActivity\r\nimport android.os.Bundle\r\nimport android.util.Log\r\nimport android.widget.Button\r\nimport android.widget.EditText\r\nimport android.widget.TextView\r\nimport android.widget.Toast\r\nimport com.google.android.gms.tasks.Task\r\nimport com.google.firebase.auth.AuthResult\r\nimport com.google.firebase.auth.FirebaseAuth\r\nimport com.google.firebase.auth.FirebaseUser\r\n\r\nclass MainActivity : AppCompatActivity() {\r\n    companion object {\r\n        const val TAG: String = \"MainActivity\"\r\n    }\r\n    private var mAuth: FirebaseAuth? = null\r\n\r\n    override fun onCreate(savedInstanceState: Bundle?) {\r\n        super.onCreate(savedInstanceState)\r\n        setContentView(R.layout.activity_main)\r\n\r\n        mAuth = FirebaseAuth.getInstance()\r\n\r\n        \/\/ \u30e6\u30fc\u30b6\u30fc\u767b\u9332\u30dc\u30bf\u30f3\r\n        var btnCreate: Button = findViewById&lt;Button&gt;(R.id.btnCreate) as Button\r\n        btnCreate.setOnClickListener{\r\n            var email = (this.findViewById&lt;EditText&gt;(R.id.txtEmail) as EditText).text.toString()\r\n            var password = (this.findViewById&lt;EditText&gt;(R.id.txtPassword) as EditText).text.toString()\r\n            Log.i(TAG, String.format(\"create email=%s, password%s\", email, password))\r\n            createUserWithEmailAndPassword(email, password )\r\n        }\r\n\r\n        \/\/ \u30b5\u30a4\u30f3\u30a4\u30f3\u30dc\u30bf\u30f3\r\n        var btnSignIn: Button = findViewById&lt;Button&gt;(R.id.btnSignIn) as Button\r\n        btnSignIn.setOnClickListener{\r\n            var email = (this.findViewById&lt;EditText&gt;(R.id.txtEmail) as EditText).text.toString()\r\n            var password = (this.findViewById&lt;EditText&gt;(R.id.txtPassword) as EditText).text.toString()\r\n            Log.i(TAG, String.format(\"signin email=%s, password%s\", email, password))\r\n            signInWithEmailAndPassword(email, password)\r\n        }\r\n    }\r\n\r\n    override fun onStart() {\r\n        super.onStart()\r\n\r\n        var currentUser = mAuth?.currentUser\r\n        updateUI(currentUser)\r\n    }\r\n\r\n    \/\/ \u753b\u9762\u8868\u793a\u66f4\u65b0\r\n    private fun updateUI(currentUser:FirebaseUser?) {\r\n        Log.i(TAG, String.format(\"update ui user=%s\", currentUser?.email.toString()))\r\n        (findViewById&lt;TextView&gt;(R.id.lblUser) as TextView).text = currentUser?.email.toString()\r\n    }\r\n\r\n    \/\/ \u30e6\u30fc\u30b6\u30fc\u767b\u9332\u51e6\u7406\r\n    private fun createUserWithEmailAndPassword(email: String, password: String) {\r\n        mAuth?.createUserWithEmailAndPassword(email, password)?.addOnCompleteListener{\r\n            task: Task&lt;AuthResult&gt; -&gt;\r\n                if (task.isSuccessful) {\r\n                    Log.d(TAG, \"createUserWithEmail:success\")\r\n                    var user = mAuth?.currentUser\r\n                    updateUI(user)\r\n                } else {\r\n                    Log.w(TAG, \"createUserWithEmail:failure\")\r\n                    Toast.makeText(this, \"Authentication failed\",Toast.LENGTH_SHORT).show()\r\n                    updateUI(null)\r\n                }\r\n        }\r\n    }\r\n\r\n    \/\/ \u30b5\u30a4\u30f3\u30a4\u30f3\u51e6\u7406\r\n    private fun signInWithEmailAndPassword(email: String, password: String) {\r\n        mAuth?.signInWithEmailAndPassword(email, password)?.addOnCompleteListener{\r\n            task: Task&lt;AuthResult&gt; -&gt;\r\n                if(task.isSuccessful) {\r\n                    Log.d(TAG, \"signInWithEmail:success\")\r\n                    var user = mAuth?.currentUser\r\n                    updateUI(user)\r\n                } else {\r\n                    Log.w(TAG, \"signInWithEmail:failure\")\r\n                    Toast.makeText(this, \"Authentication failed\",Toast.LENGTH_SHORT).show()\r\n                    updateUI(null)\r\n                }\r\n        }\r\n    }\r\n}<\/pre>\n<h2>4.\u5b9f\u884c<\/h2>\n<p>\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u3068\u30d1\u30b9\u30ef\u30fc\u30c9(6\u6841\u4ee5\u4e0a)\u3092\u8a2d\u5b9a\u3057\u3001CREATE\u30dc\u30bf\u30f3\u3092\u62bc\u4e0b<\/p>\n<p>\u51e6\u7406\u304c\u6210\u529f\u3057\u3066\u3001\u753b\u9762\u6700\u4e0a\u6bb5\u306b\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u8868\u793a\u3055\u308c\u305f\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_screen.png\"><img loading=\"lazy\" decoding=\"async\" width=\"215\" height=\"378\" title=\"firebase_auth_screen\" style=\"display: inline; background-image: none;\" alt=\"firebase_auth_screen\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_screen_thumb.png\" border=\"0\"><\/a><\/p>\n<p>Firebase\u306e\u7ba1\u7406\u753b\u9762\u4e0a\u306b\u3082\u3001\u5165\u529b\u3057\u305f\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u767b\u9332\u3055\u308c\u305f\uff01<\/p>\n<p>\u3053\u308c\u306f\u6357\u308b\u3002<\/p>\n<p><a href=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_success.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"207\" title=\"firebase_auth_success\" style=\"display: inline; background-image: none;\" alt=\"firebase_auth_success\" src=\"https:\/\/www.typea.info\/blog\/wp-content\/uploads\/image\/Android--Kotlin--Firebase_103E8\/firebase_auth_success_thumb.jpg\" border=\"0\"><\/a><\/p>\n<p><a href=\"https:\/\/www.amazon.co.jp\/Kotlin%E3%82%A4%E3%83%B3%E3%83%BB%E3%82%A2%E3%82%AF%E3%82%B7%E3%83%A7%E3%83%B3-Dmitry-Jemerov\/dp\/4839961743\/ref=as_li_ss_il?ie=UTF8&amp;qid=1528970166&amp;sr=8-2&amp;keywords=kotlin&amp;linkCode=li2&amp;tag=typea09-22&amp;linkId=7c52302305befc03f5a794ce55eea747\" target=\"_blank\"><img decoding=\"async\" src=\"\/\/ws-fe.amazon-adsystem.com\/widgets\/q?_encoding=UTF8&amp;ASIN=4839961743&amp;Format=_SL160_&amp;ID=AsinImage&amp;MarketPlace=JP&amp;ServiceVersion=20070822&amp;WS=1&amp;tag=typea09-22\" border=\"0\"><\/a><img loading=\"lazy\" decoding=\"async\" width=\"1\" height=\"1\" style=\"margin: 0px !important; border: currentcolor !important; border-image: none !important;\" alt=\"\" src=\"https:\/\/ir-jp.amazon-adsystem.com\/e\/ir?t=typea09-22&amp;l=li2&amp;o=9&amp;a=4839961743\" border=\"0\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e45\u3005\u306bAndroid\u3002Android\u306b\u304b\u304b\u308f\u3089\u305a\u3001\u4f55\u304b\u30b5\u30fc\u30d3\u30b9\u306a\u308a\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306a\u308a\u4f5c\u6210\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u30ed\u30b0\u30a4\u30f3\u306e\u4ed5\u7d44\u307f\u3060\u3068\u304b\u4f5c\u308b\u306e\u304c\u9762\u5012\u304f\u3055\u3044\u3001\u3068\u3044\u3046\u304b\u305d\u306e\u3042\u305f\u308a\u3092\u4f5c\u308a\u4e0a\u3052\u308b\u3068\u529b\u5c3d\u304d\u3066\u305d\u3082\u305d\u3082\u4f55\u304c\u3057\u305f\u304b\u3063\u305f\u306e\u304b\u308f\u304b\u3089 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","sitemap_hide":"","_veu_custom_css":"","veu_display_promotion_alert":"","vkexunit_cta_each_option":"","footnotes":""},"categories":[39,104,105],"tags":[],"class_list":["post-539","post","type-post","status-publish","format-standard","hentry","category-android","category-firebase","category-kotlin"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=539"}],"version-history":[{"count":0,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.typea.info\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}