Preserving History while Converting from Eclipse to Android Studio
As shown in Recipe 4998
, the Android Studio 'import' mechanism creates a new project in a new directory, causing a break in your revision control history. You want instead to convert a project permanently from Eclipse to Android Studio, but without losing years of valuable source control history.
One approach is to use the source control program's "move" command to restructure the project in place, into a form that Studio/Gradle can understand. Eclipse can no longer process the project after it's been rearranged this much; there doesn't seem to be a way to tell it the new location for the Android Manifest and the resource files.
The process will vary greatly depending on which source code management (SCM) system you have used. One of the oldest widely-used SCMs was CVS, the Concurrent Versions System. CVS did not support moving of files, so if your project is still in CVS (or RCS or SCCS, two even older SCMs), you will want to convert it into Git first, and learn to use Git, if you really want to preserve history. I know this process can work well because my public github.com repositories contain some files with modification dates a full ten years before git
was written. Accordingly, this example assumes you have your Eclipse Android project in the git
SCM. And I'll give the steps in the form of Unix/Linux/MacOS command line steps because that's the most concise format. Understand that this is only general guidance; your mileage will certainly vary! You will also want to have a variety of both Eclipse and Android Studion projects to compare with and to copy missing bits from; the examples from this book (see examples
, or `git clone https://github.com/IanDarwin/Andr
oidCookbookExamples`) would be a good resource to have at hand. I used this approach to convert a privately-funded Android app consisting of 30 Java files and numerous resource files, from Eclipse format to Android Studio format, and got it going in an hour or two, with its revision history intact. First, create a copy, so that if the conversion messes up too badly, you can delete the whole thing and start over. A git
purist would argue that you should just use a branch
, but this is my way of doing it.
cp -r ~/git/myproject /tmp/myproject
Now we need to convert the structure of the project. Eclipse used a simple src
folder, whereas modern build tools like Maven Recipe 4137
and Gradle Recipe 5000
use a structure like src/main/java
. If you have standalone (pure Java, non-Android) unit tests, they may be in a folder called test
, which has to become src/test/java
. Also the resources folder (res
) and the android manifest file have to move to src/main
rm -fr bin build gen target # start with a cleanup
mkdir -p src/main/java
mkdir -p src/test/java
git mv src/com src/main/java
git mv test/com src/test/java/
git mv res src/main/
git mv AndroidManifest.xml src/main/
The next step is to convert the dependency information in your .classpath or pom.xml file into the build.gradle file.
cat ../__SomeExistingStudioProject__/build.gradle pom.xml > build.gradle
vi build.gradle # Merge the dependencies by hand.
git rm -f pom.xml
git add build.gradle
Create a local.properties
file, and tell git
to ignore it, containing the path to the Android SDK on your dev machine.
cp ../__SomeExistingStudioProject__/local.properties .
echo 'sdk.dir=/Users/ian/android-sdk-macosx' > local.properties
Now you will need to copy a few files from an existing Android Studio project: at least gradlew (for Unix/Linux/MacOS X) and/or gradlew.bat (for Windows cmd.exe) If your project doesn't have a .gitignore
, create one; if it does; consider the changes we've made here. echo local.properties >> .gitignore Now try building the project, either by typing `gradlew build` or by opening it in Android Studio. Then hack at it until it works. When you're comfortable, `git commit`. When you're really happy, `git push`. An alternate approach would be as follows. I have not tested this approach but it seems simpler.
- Create a new project using the Studio New Project wizard;
- Copy files from your existing project into the new project, using the move commands above as a guide;
- Copy your revision history into the new project, which won't have one yet: `cp -r oldProject/.git newProject/.git`
- After verifying that the project is reasonably sane, save the changes, assuming that git will recognize the moved files (it usually will): `git commit -m "Reorganize project for Android Studio"`
- Hack at it as needed until it works properly.
- `git commit` any last-minute changes, and `git push`. It is probably possible to create a build.gradle file with all the file and directory paths set to the locations that Eclipse uses. This way you should be able to build using either Eclipse or Android Studio! I did this once several years ago, when Studio was still in Alpha or Beta status, but I no longer have access to the repository that this work was done in. Try it and, if it works, why not create a new recipe on the Android Cookbook site!