Getting started with Firebase on Android (2020) - Firecasts - YouTube

Channel: Firebase

[0]
hey there Android developers interested
[2]
in getting started with firebase in your
[4]
Android apps well you've come to the
[6]
right place let's get started on this
[7]
episode of buyer casts so this here is
[13]
my little sample app I've got a button
[15]
that I can click on and some text that
[17]
updates and I'm gonna get it set up to
[19]
start working with firebase go ahead and
[21]
grab your own basic app to work with it
[23]
doesn't really have to look like mine
[24]
honestly as long as it's some kind of
[26]
Android app you should be fine now there
[29]
are two main parts to getting the
[30]
firebase platform up and running adding
[32]
your app to the firebase console and
[34]
installing the SDK and there are two
[37]
ways to do that either by heading over
[38]
to the firebase console directly or by
[41]
using the firebase assistant in Android
[43]
studio now we've made some recent
[45]
improvements to the firebase assistant
[46]
that's made it easier to work with but
[48]
personally I think it's worth taking the
[50]
time to get to know the firebase console
[52]
so that's what we're gonna do here so
[54]
let's get started and as with most
[56]
projects we get to start with the most
[58]
exciting part and that's making sure my
[60]
app fulfills all the minimum
[61]
requirements and prerequisites I know
[63]
it's not even my birthday
[65]
so first off let's head over to the
[67]
documentation and here I can see the
[69]
various API targets and tools that I
[71]
need to get started obviously depending
[73]
on when you're watching this video exact
[75]
version numbers might look slightly
[76]
different so go with what you see in the
[78]
documentation not with what I'm saying
[79]
in this video so first let's see if my
[81]
Android studio is mostly up to date
[83]
I just recently updated to version 3 6 -
[86]
and it looks like this is still the
[87]
latest version of Android studio so I'm
[89]
good here next it looks like I need to
[91]
target a minimum API of 16 or later let
[94]
me open up my app level build.gradle
[95]
file and here I can see my minimum SDK
[98]
is version 19 so that's okay I also need
[101]
to make sure I'm running Gradle 4.1 or
[103]
later I can find that out by opening my
[105]
Gradle dot wrapper properties file here
[107]
and it looks like our version 564 so
[109]
that's good
[110]
next I need to make sure I'm using the
[112]
new and improved Android X support
[113]
libraries and have the right setting in
[115]
my project to support those so I can
[117]
make sure I have Android X enabled by
[119]
opening up my Gradle dot properties file
[121]
and looking for this line here and then
[122]
let's make sure I'm using an up-to-date
[124]
version of the Gradle plug-in and I can
[125]
find this in my project level build a
[127]
cradle file and this looks good and
[129]
finally I need to make sure my compile
[131]
SDK version is version 28 or later
[133]
I'll go back to my app level Gradle file
[135]
and it looks like I'm good there -
[137]
finally since I'm going to run this on
[139]
an emulator the documentation recommends
[141]
that I run this on one with Google Play
[142]
available I'll pick this pixel 3a
[144]
running Android P from my AVD manager
[146]
but for best results go ahead and pick
[149]
any device with the Play Store icon here
[151]
alright time to start creating a project
[154]
in order to do that let's visit the
[156]
console over at firebase google.com
[158]
slash console again the UI here might
[160]
look a little different than what you
[161]
have on your screen but the general
[163]
concepts should remain the same now when
[165]
you first visit the firebase console
[166]
you'll see a few options here for what
[168]
to do next depending on your situation
[170]
you might see a list of existing
[171]
fire-based projects or you might just
[173]
have an ADD project button I'm gonna go
[175]
ahead and click that because I do want
[177]
to create a new project but before we go
[179]
further let me take a moment to explain
[181]
the difference between projects and apps
[183]
a project can contain one or more apps
[186]
all apps in the same project use the
[188]
same cloud firestore real time database
[190]
and cloud storage backends and you can
[192]
view combined analytics data across all
[194]
apps in the same project you can also
[196]
use features like firebase Cloud
[198]
messaging or in-app messaging to talk to
[200]
all of your apps at once you don't have
[201]
to but you can which is sometimes
[203]
convenient so our recommendation is if
[206]
you've got versions of the same app on
[207]
different platforms such as Android iOS
[210]
and the web you'll want to connect each
[211]
of those platform specific apps to the
[214]
same firebase project this way your
[216]
users can access the same data if they
[218]
switch back and forth between different
[219]
versions of your app and you can use
[221]
different firebase products like Rio
[222]
config or FCM to target all apps at once
[225]
and so on on the other hand if your apps
[227]
really are different and you don't need
[229]
to share that much data between them you
[231]
should put those in different firebase
[232]
projects and for those of you building
[234]
white label or multi-tenant apps we
[236]
generally recommend using a separate
[238]
firebase project for each of your
[239]
customers in order to keep that data and
[241]
nicely separated so if your iOS or web
[244]
team has already created a firebase
[245]
project for their app you should
[247]
probably just select that project and
[248]
add your Android app to it but if you're
[251]
the first one to be adding firebase II
[252]
capabilities to your app lucky you you
[254]
get to be the one to create the new
[256]
firebase project and that's what I'm
[257]
going to do here so I'm gonna create a
[260]
new project here I could give it a name
[262]
although you'll notice I also have this
[264]
drop-down box underneath the name
[267]
this gives me the option to add fire
[268]
based features to any existing Google
[270]
cloud projects I have access to and this
[272]
will still keep all the capabilities of
[274]
your existing Google Cloud project while
[276]
adding in the new services needed for
[278]
firebase so if you're working on a
[280]
project that you've already setup
[281]
through Google cloud make sure you pick
[283]
that don't create a new firebase project
[285]
in my case I am starting a brand new
[287]
project so I'll type in a new name and
[289]
you can see it's giving me a little ID
[291]
string here which will get updated as I
[293]
type this is the project ID which is a
[295]
globally unique identifier for your
[297]
project across firebase and Google Cloud
[299]
it's not really publicly facing so don't
[302]
worry too much if you see random
[303]
characters at the end or anything like
[304]
this it doesn't really matter I'm gonna
[307]
click on the continue button and now I
[308]
get to choose whether or not to add
[310]
Google Analytics to my project
[311]
enabling analytics will allow me to use
[313]
a number of useful features such as AV
[315]
testing running firebase predictions and
[317]
getting crash free user reports if I
[319]
don't enable analytics I won't be able
[321]
to use those features it's totally
[322]
optional so go with it whatever works
[324]
for you personally I'm gonna keep
[326]
analytics enabled now if you do you're
[329]
gonna need to associate this project
[330]
with an account now a Google Analytics
[333]
account isn't an account like a gmail
[334]
account or anything it's really just
[336]
like a folder of analytics projects it's
[338]
mostly just there for organizational
[340]
purposes some developers like to have
[342]
separate accounts for each project me I
[344]
have one for all of my test projects and
[346]
so that's where this is going
[348]
now I can proceed to the next step which
[349]
is actually creating the project bribe
[351]
ace will think for a few moments and
[353]
then I will have a shiny new project to
[354]
work with who it still has that new
[356]
project smell hmm okay the next step is
[359]
actually adding my Android app to this
[361]
project I'm gonna go ahead and click on
[363]
the Android icon here and I have a few
[365]
fields to fill out first up adding my
[368]
Android package name also known as your
[370]
application ID in my case it's calm
[372]
example dot button clicker but go ahead
[374]
and use your own package name here the
[376]
app nickname is strictly internal and
[378]
just a way of identifying my app in a
[380]
user friendly way in the firebase
[381]
console I'm gonna go ahead and call it
[383]
button clicker Android finally I'm being
[385]
asked to optionally add the sha-1 hash
[387]
of my debug signing certificate so I'm a
[390]
app can kind of prove that it belongs to
[391]
me yeah that's an oversimplification go
[393]
check out the docs for real answer
[395]
note that this is optional it's really
[397]
only used in a few situations like
[399]
firebase dynamic links and Google
[400]
so you can leave it out or provide it
[402]
later in your fire based project
[404]
settings in fact I kind of recommend
[406]
leaving this out until you need it
[408]
because you can't have the same
[409]
combination of package name and sha-1
[411]
hash in more than one place and if you
[414]
or somebody else on your project team
[416]
ends up registering your app in a test
[418]
project with your shared production key
[420]
and then decide you want to register it
[422]
in a different project later on you're
[423]
gonna have to hunt down that original
[425]
project and delete the signature and
[426]
that might be kind of a hassle
[427]
particularly if you have a lot of folks
[429]
in your company that have access to that
[430]
sha-1 so you know what I'm not even
[433]
going to do this right now if you want
[435]
to do it follow the instructions of the
[436]
link here but for now I'm gonna move on
[438]
to the next step which is to download
[440]
this JSON file I'm gonna download Google
[443]
services JSON by clicking this button
[445]
and then we're gonna drag it in to the
[447]
right place in my android project let's
[450]
see here my project panel I'm gonna
[452]
switch my view from Android to project
[454]
which gives me more of a file system
[455]
eview of my project and then I'm gonna
[457]
drag that JSON file I just downloaded
[459]
into my button clicker slash app folder
[462]
now if we look at this file you can see
[464]
that it basically contains a bunch of
[466]
constants that the firebase SDK needs to
[468]
configure itself correctly it's got the
[470]
name of my project the cloud storage
[472]
bucket I might use some offline IDs and
[474]
so on now none of this is secret but you
[477]
should really only share this file with
[478]
your team if you're making like an
[480]
open-source demo app to be shared
[481]
publicly with the world you'll probably
[483]
want anybody who runs their own version
[485]
of this app to generate their own
[486]
version of this file to hook up to their
[488]
very own firebase project in my case I'm
[491]
not going to be sharing this that
[492]
publicly this is just for me personally
[493]
which is why I told Android studio to go
[496]
ahead and check it into my git
[497]
repository okay next we're being told to
[500]
make a few Gradle changes in a somewhat
[502]
unusual move I guess the first change we
[504]
need to make is in our project level
[505]
Gradle file so let me open this one up
[508]
and confirm first that Google is listed
[510]
in my build script repositories and in
[512]
my all projects repositories and it is
[514]
then I'll copy this line into my
[517]
dependencies section to make sure that
[518]
we're using this plug-in here by the way
[521]
don't confuse this with like Google Play
[522]
services this is just the plug-in that
[524]
parses that JSON file we added okay next
[527]
up let's open up our App level Gradle
[529]
file adds this one and this is where
[531]
you're going to add the dependencies for
[532]
the firebase Android libraries that
[534]
going to be working with now the first
[536]
thing I have to do is apply that Google
[537]
services plugin in my app Gradle file
[539]
which I can do by copy and pasting this
[541]
line up at the beginning of my file now
[544]
because I said I'm using analytics in my
[546]
project the console is helpfully giving
[548]
me a line I can use to load up the
[549]
analytics library again these numbers
[551]
will probably be quite different than
[553]
what you're seeing when you watch this
[554]
video so let me copy and paste that into
[556]
my Gradle file like so and I'll click
[559]
sync now now if you want to add other
[562]
libraries there's a full list of them
[564]
over in the Android getting started
[565]
documentation down in the available
[567]
library section just make sure you have
[569]
the right tab selected since these
[571]
library names are sometimes different
[572]
between Java and Kotlin so in fact let
[575]
me copy over this line to use the cloud
[578]
storage for firebase library now one
[580]
thing I will point out is you can
[581]
sometimes run into strange and hard to
[583]
track down errors if your libraries are
[585]
out of sync so if you go ahead and
[587]
update one of your libraries to the
[588]
latest version make sure you update the
[590]
rest of them to G in fact it looks like
[592]
my analytics library is already slightly
[594]
out of date so I could update it by
[596]
copying and pasting this line but it
[598]
looks like Android studio is helpfully
[600]
telling me the same thing so let's fix
[602]
it that way and if all this versioning
[604]
work sounds like a hassle to deal with
[606]
hey you're in luck there's actually a
[607]
fairly new way to handle all of this
[609]
called a bill of materials basically a
[612]
bill of materials is an officially
[613]
published list of libraries and their
[615]
version numbers that work well together
[617]
so let's try using one of these instead
[619]
so if you have Gradle version 5 or later
[622]
and as you recall I do you can head on
[624]
over to the Bill of Materials section of
[626]
the documentation and then you can just
[628]
copy and paste this line here into your
[630]
Gradle Dependencies then and this is
[632]
important go ahead and remove your
[634]
version numbers from these individual
[636]
libraries Gradle will then find the
[638]
versions of these libraries in this bill
[640]
of materials and use those for my app
[642]
then when you want to update to the
[644]
latest version of firebase you just
[645]
update this one Bill of Materials
[647]
version instead of all the individual
[649]
libraries the only thing I noticed is
[651]
that Android studio doesn't seem to tell
[653]
you when your bill of materials version
[654]
is old you'll need to go to the Android
[657]
release notes section of the
[658]
documentation and see what the latest
[659]
version is that'll also tell you exactly
[662]
what library version is in each one of
[664]
these things in case you are curious
[665]
okay so I'll once again sync by Gradle
[667]
and I should have fire-based properly
[670]
installed and working so how can I make
[672]
sure I did this right well let's see if
[674]
I run my app and I search my log cat for
[676]
fire base I see this line and that
[678]
should be enough to confirm that fire
[680]
base is properly initialized don't worry
[682]
too much about this line underneath I
[684]
see that sometimes when I'm working on
[685]
the emulator finally you might notice
[687]
the console is also waiting to see if
[688]
it's gotten any contact from your app to
[691]
be honest I don't always find this
[692]
dialog super reliable but it seems like
[694]
it generally finds my app if I go ahead
[696]
and move it to the background like this
[697]
ah there we go but hey while we're here
[700]
let's go ahead and add an analytics
[702]
caller - now this isn't an analytics
[704]
tutorial that's a whole separate video
[705]
so I'll keep this simple first let me
[707]
add a latent variable to store my
[709]
analytics reference I will initialize it
[712]
here my oncreate then let me go ahead
[714]
and make an analytics call in my app
[716]
let's see here maybe I will add a custom
[719]
event for clicking the button then let
[721]
me jump into my terminal and semi
[722]
logging level 2 4 boasts for both F a
[724]
and F a - SVC these are the analytics
[728]
tags and if you're wondering how I knew
[730]
this I read the documentation
[732]
so now let's restart my app once it
[735]
starts up I'm going to head on over to
[736]
my log cat and I can see one analytics
[738]
message here but I think to properly
[740]
view my firebase message logs it helps
[742]
to select firebase from this drop-down
[744]
on the right hopefully you have
[745]
something similar in your own version of
[747]
Android studio and now when I click my
[749]
buttons these button click events are
[751]
properly being logged by the firebase
[753]
SDK for Google Analytics nice now that
[756]
you have firebase properly set up and
[758]
initialized in your app there's lots
[760]
more you can do you can store query and
[762]
sync data from the cloud using cloud
[763]
firestore are globally scalable no
[765]
sequel database use firebase off to
[768]
easily sign in your users from a variety
[769]
of sign and providers record custom
[772]
events and user properties in analytics
[774]
and combine that with remote config to
[776]
run AV tests or personalize your app add
[779]
services like crashlytics so you can
[781]
find out exactly where your app is
[782]
crashing and why and much more if you
[785]
want to find out more why not subscribe
[786]
to our youtube channel you can also
[788]
check out the firebase documentation or
[790]
play around with any of our sample apps
[792]
have fun creating and I will see you
[794]
soon on another episode of fire casts
[797]
you
[798]
[Music]