Getting started with Firebase on Android (2020) – Firecasts

hey there Android developers interested in getting started with firebase in your Android apps well you've come to the right place let's get started on this episode of buyer casts so this here is my little sample app I've got a button that I can click on and some text that updates and I'm gonna get it set up to start working with firebase go ahead and grab your own basic app to work with it doesn't really have to look like mine honestly as long as it's some kind of Android app you should be fine now there are two main parts to getting the firebase platform up and running adding your app to the firebase console and installing the SDK and there are two ways to do that either by heading over to the firebase console directly or by using the firebase assistant in Android studio now we've made some recent improvements to the firebase assistant that's made it easier to work with but personally I think it's worth taking the time to get to know the firebase console so that's what we're gonna do here so let's get started and as with most projects we get to start with the most exciting part and that's making sure my app fulfills all the minimum requirements and prerequisites I know it's not even my birthday so first off let's head over to the documentation and here I can see the various API targets and tools that I need to get started obviously depending on when you're watching this video exact version numbers might look slightly different so go with what you see in the documentation not with what I'm saying in this video so first let's see if my Android studio is mostly up to date I just recently updated to version 3 6 – and it looks like this is still the latest version of Android studio so I'm good here next it looks like I need to target a minimum API of 16 or later let me open up my app level build.

gradle file and here I can see my minimum SDK is version 19 so that's okay I also need to make sure I'm running Gradle 4.

1 or later I can find that out by opening my Gradle dot wrapper properties file here and it looks like our version 564 so that's good next I need to make sure I'm using the new and improved Android X support libraries and have the right setting in my project to support those so I can make sure I have Android X enabled by opening up my Gradle dot properties file and looking for this line here and then let's make sure I'm using an up-to-date version of the Gradle plug-in and I can find this in my project level build a cradle file and this looks good and finally I need to make sure my compile SDK version is version 28 or later I'll go back to my app level Gradle file and it looks like I'm good there – finally since I'm going to run this on an emulator the documentation recommends that I run this on one with Google Play available I'll pick this pixel 3a running Android P from my AVD manager but for best results go ahead and pick any device with the Play Store icon here alright time to start creating a project in order to do that let's visit the console over at firebase google.

com slash console again the UI here might look a little different than what you have on your screen but the general concepts should remain the same now when you first visit the firebase console you'll see a few options here for what to do next depending on your situation you might see a list of existing fire-based projects or you might just have an ADD project button I'm gonna go ahead and click that because I do want to create a new project but before we go further let me take a moment to explain the difference between projects and apps a project can contain one or more apps all apps in the same project use the same cloud firestore real time database and cloud storage backends and you can view combined analytics data across all apps in the same project you can also use features like firebase Cloud messaging or in-app messaging to talk to all of your apps at once you don't have to but you can which is sometimes convenient so our recommendation is if you've got versions of the same app on different platforms such as Android iOS and the web you'll want to connect each of those platform specific apps to the same firebase project this way your users can access the same data if they switch back and forth between different versions of your app and you can use different firebase products like Rio config or FCM to target all apps at once and so on on the other hand if your apps really are different and you don't need to share that much data between them you should put those in different firebase projects and for those of you building white label or multi-tenant apps we generally recommend using a separate firebase project for each of your customers in order to keep that data and nicely separated so if your iOS or web team has already created a firebase project for their app you should probably just select that project and add your Android app to it but if you're the first one to be adding firebase II capabilities to your app lucky you you get to be the one to create the new firebase project and that's what I'm going to do here so I'm gonna create a new project here I could give it a name although you'll notice I also have this drop-down box underneath the name this gives me the option to add fire based features to any existing Google cloud projects I have access to and this will still keep all the capabilities of your existing Google Cloud project while adding in the new services needed for firebase so if you're working on a project that you've already setup through Google cloud make sure you pick that don't create a new firebase project in my case I am starting a brand new project so I'll type in a new name and you can see it's giving me a little ID string here which will get updated as I type this is the project ID which is a globally unique identifier for your project across firebase and Google Cloud it's not really publicly facing so don't worry too much if you see random characters at the end or anything like this it doesn't really matter I'm gonna click on the continue button and now I get to choose whether or not to add Google Analytics to my project enabling analytics will allow me to use a number of useful features such as AV testing running firebase predictions and getting crash free user reports if I don't enable analytics I won't be able to use those features it's totally optional so go with it whatever works for you personally I'm gonna keep analytics enabled now if you do you're gonna need to associate this project with an account now a Google Analytics account isn't an account like a gmail account or anything it's really just like a folder of analytics projects it's mostly just there for organizational purposes some developers like to have separate accounts for each project me I have one for all of my test projects and so that's where this is going now I can proceed to the next step which is actually creating the project bribe ace will think for a few moments and then I will have a shiny new project to work with who it still has that new project smell hmm okay the next step is actually adding my Android app to this project I'm gonna go ahead and click on the Android icon here and I have a few fields to fill out first up adding my Android package name also known as your application ID in my case it's calm example dot button clicker but go ahead and use your own package name here the app nickname is strictly internal and just a way of identifying my app in a user friendly way in the firebase console I'm gonna go ahead and call it button clicker Android finally I'm being asked to optionally add the sha-1 hash of my debug signing certificate so I'm a app can kind of prove that it belongs to me yeah that's an oversimplification go check out the docs for real answer note that this is optional it's really only used in a few situations like firebase dynamic links and Google so you can leave it out or provide it later in your fire based project settings in fact I kind of recommend leaving this out until you need it because you can't have the same combination of package name and sha-1 hash in more than one place and if you or somebody else on your project team ends up registering your app in a test project with your shared production key and then decide you want to register it in a different project later on you're gonna have to hunt down that original project and delete the signature and that might be kind of a hassle particularly if you have a lot of folks in your company that have access to that sha-1 so you know what I'm not even going to do this right now if you want to do it follow the instructions of the link here but for now I'm gonna move on to the next step which is to download this JSON file I'm gonna download Google services JSON by clicking this button and then we're gonna drag it in to the right place in my android project let's see here my project panel I'm gonna switch my view from Android to project which gives me more of a file system eview of my project and then I'm gonna drag that JSON file I just downloaded into my button clicker slash app folder now if we look at this file you can see that it basically contains a bunch of constants that the firebase SDK needs to configure itself correctly it's got the name of my project the cloud storage bucket I might use some offline IDs and so on now none of this is secret but you should really only share this file with your team if you're making like an open-source demo app to be shared publicly with the world you'll probably want anybody who runs their own version of this app to generate their own version of this file to hook up to their very own firebase project in my case I'm not going to be sharing this that publicly this is just for me personally which is why I told Android studio to go ahead and check it into my git repository okay next we're being told to make a few Gradle changes in a somewhat unusual move I guess the first change we need to make is in our project level Gradle file so let me open this one up and confirm first that Google is listed in my build script repositories and in my all projects repositories and it is then I'll copy this line into my dependencies section to make sure that we're using this plug-in here by the way don't confuse this with like Google Play services this is just the plug-in that parses that JSON file we added okay next up let's open up our App level Gradle file adds this one and this is where you're going to add the dependencies for the firebase Android libraries that going to be working with now the first thing I have to do is apply that Google services plugin in my app Gradle file which I can do by copy and pasting this line up at the beginning of my file now because I said I'm using analytics in my project the console is helpfully giving me a line I can use to load up the analytics library again these numbers will probably be quite different than what you're seeing when you watch this video so let me copy and paste that into my Gradle file like so and I'll click sync now now if you want to add other libraries there's a full list of them over in the Android getting started documentation down in the available library section just make sure you have the right tab selected since these library names are sometimes different between Java and Kotlin so in fact let me copy over this line to use the cloud storage for firebase library now one thing I will point out is you can sometimes run into strange and hard to track down errors if your libraries are out of sync so if you go ahead and update one of your libraries to the latest version make sure you update the rest of them to G in fact it looks like my analytics library is already slightly out of date so I could update it by copying and pasting this line but it looks like Android studio is helpfully telling me the same thing so let's fix it that way and if all this versioning work sounds like a hassle to deal with hey you're in luck there's actually a fairly new way to handle all of this called a bill of materials basically a bill of materials is an officially published list of libraries and their version numbers that work well together so let's try using one of these instead so if you have Gradle version 5 or later and as you recall I do you can head on over to the Bill of Materials section of the documentation and then you can just copy and paste this line here into your Gradle Dependencies then and this is important go ahead and remove your version numbers from these individual libraries Gradle will then find the versions of these libraries in this bill of materials and use those for my app then when you want to update to the latest version of firebase you just update this one Bill of Materials version instead of all the individual libraries the only thing I noticed is that Android studio doesn't seem to tell you when your bill of materials version is old you'll need to go to the Android release notes section of the documentation and see what the latest version is that'll also tell you exactly what library version is in each one of these things in case you are curious okay so I'll once again sync by Gradle and I should have fire-based properly installed and working so how can I make sure I did this right well let's see if I run my app and I search my log cat for fire base I see this line and that should be enough to confirm that fire base is properly initialized don't worry too much about this line underneath I see that sometimes when I'm working on the emulator finally you might notice the console is also waiting to see if it's gotten any contact from your app to be honest I don't always find this dialog super reliable but it seems like it generally finds my app if I go ahead and move it to the background like this ah there we go but hey while we're here let's go ahead and add an analytics caller – now this isn't an analytics tutorial that's a whole separate video so I'll keep this simple first let me add a latent variable to store my analytics reference I will initialize it here my oncreate then let me go ahead and make an analytics call in my app let's see here maybe I will add a custom event for clicking the button then let me jump into my terminal and semi logging level 2 4 boasts for both F a and F a – SVC these are the analytics tags and if you're wondering how I knew this I read the documentation so now let's restart my app once it starts up I'm going to head on over to my log cat and I can see one analytics message here but I think to properly view my firebase message logs it helps to select firebase from this drop-down on the right hopefully you have something similar in your own version of Android studio and now when I click my buttons these button click events are properly being logged by the firebase SDK for Google Analytics nice now that you have firebase properly set up and initialized in your app there's lots more you can do you can store query and sync data from the cloud using cloud firestore are globally scalable no sequel database use firebase off to easily sign in your users from a variety of sign and providers record custom events and user properties in analytics and combine that with remote config to run AV tests or personalize your app add services like crashlytics so you can find out exactly where your app is crashing and why and much more if you want to find out more why not subscribe to our youtube channel you can also check out the firebase documentation or play around with any of our sample apps have fun creating and I will see you soon on another episode of fire casts you [Music].

Hãy bình luận đầu tiên

Để lại một phản hồi

Thư điện tử của bạn sẽ không được hiện thị công khai.