In this lesson we'll address how to integrate Git and GitHub into Android studio, to continue using them as the version control system for your Android projects. As usual, setup will differ slightly depending on whether you're working independently, or working with a partner.
Conveniently, Android Studio makes it easy to sync a project to GitHub by using a series of options built directly into their GUI (graphical user interface). This section will walk through using those options to create a Git repo, add and commit changes, create a remote repository on GitHub, and push your project to the remote repo.
First, before adding anything to Git, we'll want to sync our project with gradle. This will ensure our module files are included in the project. When you first create a project, these files have likely not yet been created. So, we'll manually tell Android Studio to sync our project with Gradle.
You can do this by selecting the "Sync Project Files with Gradle" icon in the menu. This icon looks like this:
If we click this button, our project will sync with Gradle.
Next, we need to tell Android Studio to enable version control for this project. We can do this by visiting the VCS option in the top menu, and selecting Enable Version Control Integration:
Selecting this option will result in a pop-up that allows us to select which VCS our project should use. We'll select Git from the dropdown menu:
...and click OK. This will create a local Git repository in the project folder.
As you learned in your Java course, we don't need to add every single file to our Git repository. For instance, we don't need to track our build files because their contents are simply compiled from our source code. When someone clones down our project, they'll re-compile their own build files on their own machine using the source code. So, we can use a .gitignore file to exclude this content from our Git repository.
However, as soon as we set our VCS to Git, you'll find that Android Studio will make us a .gitignore file by default.
If you're currently in the "Android" view, you may not yet see this .gitignore file. However, if you switch to "Project" view, you should see it in the filetree on the left-hand side. You can switch between view types by simply clicking the dropdown menu directly above the filetree:
Once you're in "Project" view, you should notice a .gitignore file in the top-level of your project directory:
If you open it, you'll see that all file types we need to ignore are already present, including the /build directory:
*.iml .gradle /local.properties /.idea/workspace.xml /.idea/libraries .DS_Store /build /captures
Note: In Android Studio "Project" view simply displays all files in a project directory, whereas "Android" view only displays the files you'll edit most while developing an Android project. You're welcome to switch between the two freely at any point.
Once Git is set up, you should be able to add and commit changes as usual using the Terminal located within Android Studio.
Once you're ready to share your project on GitHub, you may use options built directly into the Android Studio GUI to do so. Once again, select the VCS menu option, then Import into Version Control and Share Project on Github:
After selecting this option, you should be prompted to provide your GitHub login information:
Then, after logging in, you should be provided another popup window. Here, you can choose your GitHub repository's name and description, as well as the name of the remote URL:
Whenever you're ready, select Share to push the project to GitHub.
Do note that you may also use the Terminal in Android Studio to use the same Git commands you've been executing in the command line to track projects for previous courses. (ie:
git remote add,
git push, etc.)
You are not required to use the Android GUI options to create, configure, and push to a GitHub repository, as described above. This is simply another option provided by Android Studio.
While at Epicodus we will continue to use pivotal-git-scripts to give both partners credit for their commits. In order to do this, locate the "Terminal" option in the lower-left corner of Android Studio. Select this option to reveal a command line terminal right inside Android Studio:
In this terminal we may include and configure our .pairs file, and run commands to commit as a pair, as we've done in previous courses at Epicodus.