[Building an Android App] Step 1: Setting up a Server
This is the first post in the [Building an Android App] series. Zero to hero baby!
Are you excited? I am! We are going to build an android app! Like an actual app, that you can use on your phone. And you are going to build it. Phone. App. You.
Assuming you've got your killer idea (or actually any idea will do, this is about the process), let's get started.
I'll be writing this for the novice programmer. But even then, I'm sure there will be a few gaps in my documentation...so when in doubt just fricken google whatever error or issue you are having (if you don't know already, google and stack overflow are your besties).
Background: You can divide the code we'll be writing for the app into two repositories. There is app-side code that is used to create the application on your phone, like all the displays, the buttons, input boxes, the logic for the app and there isserver-side code that has a database that stores and saves any changes to users' information (or other info). The two repositories talk to each other through an API (to be explained further, later).
For example, suppose you have a simple Favorite Restaurant App that saves all the locations of your favorite restaurants:
All the code used to interact directly with the user is app-side
When the user saves a new favorite restaurant, we will make an API call (app-side code), which sends the new info to the server to be stored in a database (storage done through server-side code).
Next time the user opens the app to see her favorite spots, the app will make another API call to retrieve the all the users' favorited restaurants from the server database
The two repositories of code will be in different languages (app-side in this case will be in Java, since that's the Android programming language and we'll use python + tornado & mySQL libraries on the server-side).
So today, we are focusing on setting up the server. The server is basically a machine that will constantly be "listening" for any requests made by a person using the app. It's always on, working hard for us (and not in great conditions :p).
So this server exists somewhere on a "farm" - how the hell are we going to reach and build something on it? We use a function in the shell (if you aren't familiar with Terminal, learn a bit here) called SSH which allows us to get into the server from our own machine (own machine = "local" machine) and basically do whatever we want on the server...install software, write code, etc.
[Note: these instructions will help you set up your own server to handle the backend for your app. However, there are services out there like, Parse, that do majority of the work for you - depending on what you want to learn and the demands of your app, this may be a better option for you. Thanks to @Azeem for the hat tip]
Step 1: Get a Domain Name, Buy a Server (well an EC2), Install Python, Tornado
GoDaddy.com for a domain name and amazon for a server.
Follow these superb instructions to do the above.
In order to ssh into the server through the terminal, you also need to add an SSH rule to the server on AWS. You do this the same way that you add the HTTP request rule.
On AWS, in the lefthand menu,
1. Go to Security Groups --> select the appropriate security group to edit it (if you are not sure which one, go back to your Instance and see which security group it's connected to)
2. At the bottom, hit the Inbound tab --> Create a new Rule
3. Add SSH from the dropdown menu
4. Keep the default source to be 0.0.0.0/0 (meaning anyone can connect)
Once you have successfully SSH'd into the server, you should also install Python and Tornado on the server (along with your local machine, if you haven't already done so). Don't worry about installing nginx, or Dropbox (we are going to use Tornado and Git).
NOTE: Typically you should be able to use the terminal command
to install software onto the server. However, if this is not working, you may need to download the set-up file directly from the web. You can do this from the command line too, using the awesomely useful function:
$ wget -0 <destination> <downloadlink>
$ wget -0 setuptools-0.6c11-py2.7.egg "http://pypi.python.org/..."
Once it has been downloaded you can use easy_install to install it.
Step 2: Download Git and Set up GitHub
Now, every time we want to write code, we don't want to rely on SSHing into the server. We want to be able to write code on our local machine, and have that automatically be updated on the server.
To do this we are going to use the dynamite duo: Git & GitHub.
This is our version control system. So, with it, we can allow multiple machines to have access and update the latest version of code (i.e., both our local machine and the server). Obviously, this version control system becomes even more useful when you have multiple people writing code. Git saves each version as a new file on your local machine (instead of just keeping track of changes, like traditional VCSs). With GitHub we can also post all our code online, so others can learn from it too. Convinced? Alrightythen.
1. Create a GitHub account.
2. Install git on the server, create an SSH key and link it to GitHub following this guide.
3. Repeat everything on your local machine, but this time, we do not want to create a new SSH key. Instead, we will use a secure copying procedure to copy the SSH key we created on the server, and store it on our local machine.
Before you do this though, make sure your destination filename does not already exist, otherwise you will write over it with this command.
$ scp <from> <destination>
**When you create the keys on the server. You created a public key and a private key. The private key (id_rsa, not id_rsa.pub) is used to access github, and that's what you need to store on your local machine.
4. Add the SSH files on both your local machine and the server.
If you are having a permissions error. You may need to:
$ exec ssh-agent /usr/bin/bash
5. Now, you are ready to create a new GitHub repository. In this process you'll create a folder (again you'll want to create a folder both on your local machine and on your server). This folder is where we will store all your code!
(Also, done with the boring stuff! \o/)