FTC #11117 Autonomice

Set-Up GitHub Integration with Android Studio on Windows

By Gabriel and Michaela

Last Updated: 11/1/2019

Note: This document comprises section four of our Android Studio and GitHub Integration guide. Click here to view sections one through three. This tutorial is for the Windows operating system; if you’re using a Mac, use this tutorial. Additionally, if you’ve already followed these steps for another repository in the past, you can skip to step 7. Otherwise, start here:

  1. Install Git, if you haven’t already. There are a number of ways to do this; one way is using “GitHub Desktop”:
  1. Go to desktop.github.com, read the license agreement, and download the program.
  2. Open the installer “GitHubDesktopSetup.exe.”
  3. Once GitHub Desktop has been installed, you can open the shortcut it gives you to see what it’s like, but that’s actually just a graphical interface to Git and GitHub. Android Studio has its own interface, so we won’t be using this one.
  1. Find the path to Git. If you followed step 1 and installed GitHub Desktop, follow the substeps below. If not, you’re looking for a file called “git.exe” in whatever you installed.
  1. Right-click the start menu, click “Run,” then type in “%appdata%” (without the quotation marks) and click OK.
  2. Go up a folder to “AppData.”
  3. Enter these folders, one after another: Local, GitHubDesktop, app-[version number], resources, app, git, cmd.
  4. If you can’t find these folders, try searching AppData for “git” or “git.exe”; that’s the file you’re looking for.
  5. Select the “git.exe” file (if you have extensions hidden, it might just appear as “git).
  6. Click “Copy path” in the Home menu of File Explorer. You’re going to put this path into Android Studio.
  1. Configure Android Studio to work with Git
  1. Open Android Studio (if you don’t have it installed, go here and install it).
  2. Go to the settings (File → Settings, or, if you don’t have a project open, Configure → Settings).
  3. Go to Version Control → Git (not GitHub).
  4. Paste the path to Git from the previous step into the “Path to Git executable” box. Click “Test.” If it doesn’t work, try deleting the quotation marks around the path.
  5. Change “Update method” to “Merge” and click “Apply.”
  1. Configure Android Studio to work with your GitHub account
  1. In Android Studio’s settings go to Version Control → GitHub (not Git).
  2. For “Server,” enter “github.com”. (if it isn’t already there)
  3. For “Login,” enter your GitHub username. For “Password,” enter your GitHub password.
  4. Click “Login.” If it doesn’t work, check your GitHub credentials by trying to log in with them online. Also check your Git path from the last steps.
  5. Click “Apply.” It might ask you to set up a master password; this is optional.
  1. Find the path to the Android SDK using the steps below.
  1. Go to Android Studio’s settings again (see step 3b).
  2. Go to Appearance & Behavior → System Settings → Android SDK
  3. At the top of the window, you should see a box labeled “Android SDK Location.” Select the path in this box and copy it.
  1. Make an environment variable with the Android SDK path in it
  1. Open Control Panel, search for “path,” and select “Edit environment variables for your account”
  2. In the upper half (“user variables for…”), click “New…”, and add a variable with the name “ANDROID_HOME” (no quotation marks); for the value, paste the path to the “sdk” folder. It is important that neither the name nor the value have quotation marks around them.
  3. Click “OK” twice to exit the environment variables windows; you can close Control Panel now.
  1. Import the GitHub repository into Android Studio
  1. Open Android Studio. If you have an existing project open in Android Studio, close it (File → Close Project).
  2. Click “Check out project from Version Control” and select “GitHub.”
  3. For “Git Repository URL,” use the drop-down menu to select your team’s repository. If you can’t find it:
  1. Go to your team’s repository’s GitHub page online (if you have access to that repository, it should probably be under “Repositories you contribute to” on the GitHub homepage when you are signed in).
  2. On the GitHub page, click “Clone or download,” and copy the URL that it gives you into “Git Repository URL.” The URL should end in “.git” and should clearly refer to the repository you want to import.
  1. For “Parent Directory” enter wherever you want the files to be stored on your computer. The default is fine; otherwise you can browse with the “…” button.
  2. The “Directory Name” box should be filled in with the repository name. If not, you can find the name on the online repository page, or in the URL you copied.
  3. Click “Clone.” Android Studio will begin to download all the code you need from GitHub. This may take a while.
  1. Configure the Android Studio project
  1. When Android Studio is finished importing it will ask if you would like to open the project. Click “Yes” to opening/creating a Studio project file.
  2. Click through the next screen; a progress bar should appear. Wait for that to finish; it may take a while.
  3. If you get an error about not being able to find the SDK, make sure you did steps 5 and 6 properly. If you browse to the location you selected in step 7d (by default it’s a folder called “StudioProjects” in your user home folder) and delete/rename it, you can start over at step 5. If you’re still having trouble, or if you get an error about failing to find a target with a hash string, follow these steps:
  1. Cancel, close Android Studio, and open it again.
  2. Instead of importing a project, click “Open an existing Android Studio project”; browse to the location you selected in step 7d (by default it’s a folder called “StudioProjects” in your user home folder).
  3. Select the folder named identically (or similarly) to your repository, and click OK. This should bring you to the progress bar in step 7b again.
  1. You should now see a large window with a progress bar at the bottom. Let it build, which may take a while; select all three modules to include and click OK.
  1. Deal with strange messages
  1. You will probably see several errors at the bottom of the screen. Click the update or install option for each, one at a time, until these errors go away.
  2. You may see three messages once the project is done building: “Unregistered VCS root detected,” “Frameworks detected,” and “Android Gradle Plugin Update Recommended.” Click “Update “ for the Android Gradle message. This should also make the VCS root message go away; I don’t know why. If you click on the gray background of the window, the frameworks message should go away too. Again, no idea why.
  3. Once you’ve dealt with all the errors and messages, restart Android Studio.
  4. When Android Studio loads again you should see information at the bottom of the screen about the “Gradle build.” This will happen each time you open the project, and it’s best to let it finish before you try to do anything.
  5. If you still see errors or messages, repeat parts a and d until they are all resolved.
  1. Do a test commit and push
  1. After the Gradle build is done, click the “1: Project” button on the left side of the window. This should bring up an area where you can navigate the project and open files. There are several different views you can try; I prefer the “Project” view or the “Android” view. The “FtcRobotController” module contains sample programs that you can try, and internal code that you shouldn’t edit. The “TeamCode” is where you put your team’s code to run it.
  2. Select and open the “org.firstinspires.ftc.teamcode” folder within TeamCode (and within subfolders, depending on which view you’re using). This is where your team’s code goes. If you see a file with “test” in its name and ending with “.txt”, open it (by double-clicking). If not, create one for your team with File → New → File
  3. Write something in the test file so that you will have some changes to commit and push to GitHub.
  4. In the toolbar at the top of the window, click the green up arrow with “VCS” written above it.
  5. In the box for “Commit Message,” write a short message that describes what you have done (something informative like “add to the test file to test version control”).
  6. Hover over the “Commit” button, but instead of clicking it choose “Commit and Push….” Committing only affects the Git repository on your computer; “pushing” synchronizes your local changes with the repository on GitHub’s servers. It is generally helpful to do both at the same time.
  7. If Android Studio finds warnings, click “Commit” anyway. If it finds errors, click “Cancel” or “Review” and go fix the errors.
  8. Click “Push” when that window comes up.
  9. You should see a green box that says “Push successful.”
  10. If you go to the GitHub repository online, navigate to TeamCode/src/main/java/org/firstinspires/ftc/teamcode, and open the test file, you should see the changes you made.
  1. Do a test update
  1. Have someone else make a change and push it to GitHub, so you can test the “update” function.
  2. Click the down arrow next to the commit arrow you clicked before. Choose “Merge” and “Using Stash”
  3. This should give you a green box saying either what files have been updated or that “all files are up-to-date,” which means that you were already synchronized.

That’s it! Continue to use the commit and update buttons frequently and your team will never again have to worry about which version of code someone has.