Anyone who's developed Android apps in Android Studio will understand this metaphor immediately. You've been working on an app, it's all going well, and you hear about a new version of Android Studio. So you download it, fire it up and admire its shiny new UI as you open up your Android project. But then of course, Gradle comes along to spoil your fun and complain that it failed to sync the project. Android Studio will probably pipe up with one of its helpful suggestions, like "Update to the latest Gradle and resync" which you try with naive hopefulness, but it only results in a new opaque error message such as "Unable to find method DependencyHandler.registerTransform". This heralds the start of an entire day spent trawling Google looking for solutions, but each one just leads to more and more meaningless error messages.
Finally you've had enough, it has broken your spirit and with a defeated sob, you wipe the vomit and snot off your shirt and decide to just create a whole new project skeleton and copy over your files from the old project. This does work, but it has the drawback of losing all your git history - all those detailed and helpful commit messages you spent so long crafting (right?).
But there is a way to keep all the history - you just need to copy the .git subfolder from the old project to the new one, tidy everything up and you should be good to go. Here are the complete steps that I followed:
- Create a new empty project.
My app is a libGDX game, so I fired up the setup wizard (now called GDX-Liftoff), entered the same project name and package as the existing project, selected the relevant add-ons and extensions, and finally chose a newer version of Java (I went with Java 17 as it's stable and fully compatible with the latest Android Gradle Plugin). - Open a terminal and navigate to the new project directory. If you run git status now, it will say it's not a git repository - all well and good.
- Copy the .git subfolder from the old project like so:cp -Rp /path/to/old/project/.git .Now when you run git status, you should get a long list of deleted and modified files.
- Use git restore to restore only those files which you want in the new project - all your source Java and Kotlin files, assets and any other resources and config files you might be using:git restore assets/ git restore android/src/com/rdeeson/ git restore core/src/com/rdeeson/ git restore any/other/files/you/need/to/keepYou might find it helpful to pipe git status into grep to just show Java files, deleted files etc - git status | grep deleted
- In my case, the directory structure for libGDX projects had changed, with a new pair of folders main/java/ in the file paths, so that e.g. core/src/com/rdeeson/worddrops/BoxFlingControl.java now needs to be at core/src/main/java/com/rdeeson/worddrops/BoxFlingControl.java. This can be fixed with git mv in order to keep the git history. You will probably have to delete the target directory first as git seems to be unable to merge them:rm -rf core/src/main/java/com git mv core/src/com core/src/main/javaIt's a similar story with the android files e.g. android/src/com/rdeeson/worddrops/AndroidLauncher.java now needs to be at android/src/main/java/com/rdeeson/worddrops/android/AndroidLauncher.java - this time the package is also different (note the extra android at the end of the path):git mv android/src/com/rdeeson/worddrops/*.java android/src/main/java/com/rdeeson/worddrops/android/Again you might need to delete Java files in the destination if they have the same name - or you can try using the -f flag for git mv.
- Once you're happy that git status is showing everything correctly, it's time to open the new project in Android Studio (the shiny new version of course). This time there shouldn't be any complaints from Gradle. If you had to move files as I did, you might have some errors due to package changes but these are easy for Android Studio to fix.