Thursday, December 11, 2014

Migrating Git repo from Eclipse to Android Studio

I've been tracking the Android Studio releases from afar for awhile and have been looking for an excuse to truly give it a shot. Besides the fact that Java 7 for IntelliJ wasn't really supported on the Mac for some time, the transition from the Eclipse build system to Gradle seemed a bit invasive.

One of the biggest barriers in my mind was migrating my Git repository to the new Gradle structure without losing file history. Android Studio will migrate an Eclipse project to an Android Studio, Gradle-based one, which involved moving a number of folders as well as replacing Eclipse- with Gradle-specific configuration files. I wanted to use the Android Studio migration tool but was unsure whether Git would identify these changes as file moves or treat them as entirely new files, which would make tracking file history more cumbersome.

Some have found that Git will identify these files as simply moved/renamed, but unfortunately I did not experience that. Instead, I found that I could run the migration tool in a separate location and manually recreate the moves on the original repository. Here's how I did that, in case anyone else is trying the make the move as well.
  1. Create a new branch in the Eclipse project location. This will make it easier to delete any changes if migration gets messy.
  2. Start the Android Studio migration tool. To get it running, I closed any open Android Studio project, and the wizard popped up. I then chose to import a non-Android Studio project and pointed to the Eclipse project location. For the new project, I chose a distinct outside folder location.
  3. Migrate the project! There were some errors upon actually building the project, but at least for me the migration itself went smoothly.
  4. Mirror the major folder moves. Back in the original Eclipse project location, I started moving folders via git to mirror the new structure. The key is that there are really only a few folders to move, so it's not as painful as it looks. Here are the main commands I ran, with help from this post:
    mkdir -p app/src/main/java
    git mv src/com app/src/main/java
    git mv res app/src/main
    git mv assets app/src/main
    git mv AndroidManifest.xml app/src/main

    "src/org" can be changed to whatever your source sub-root folder is, and "assets" or other folders may need to be in/excluded according to your project files.
  5. Commit these changes in the new branch. Now they are ready to be picked up in your new Gradle-based project!
  6. Copy the Eclipse project's .git folder into the new Gradle-based project location. This will make the Git repo with all the major moves available in your Gradle location, including the new branch where all the dirty work is being done.
  7. Turn on VCS for the new project. Android Studio has a dedicated VCS menu with the option to turn on various versioning systems. Once it's activated, you can show changes and confirm that Git sees all those moved files as being in the right place.
  8. Add all the new Gradle-based files. The nice thing about using the Android Studio VCS is that it knows not to show a bunch of Gradle build files that shouldn't be version controlled. It also deletes a bunch of Eclipse-specific project files from Git simply because they weren't migrated. The challenge however is that it doesn't by default offer to add all the files you do in fact need. Here are the files that appear to be key, which I learned from viewing the Android Samples project structure:
    app/build.gradle
    gradle
    build.gradle
    gradlew
    gradlew.bat
    settings.gradle
  9. Commit all changes to Git. If you've pushed them, you should be able to pull them on another machine right from within Android Studio to access your project there.
From my brief taste of Android Studio so far, it's been quite a treat!