Darwin's Theories Blog

New Theories for a New Time

Modernising old Android Apps

2022-07-26
Always in Motion the Android tooling is

I recently needed to make minor code changes to an Android app that was part of the code examples collection from the Android Cookbook. I found, to no great surprise, that the build tool configuration files were too old for the modern Android Studio (any version beginning 2021 or later) to recognize. I wound up making the following changes to get things working. Note that the IDE recommended updating Gradle from the really old Gradle version 2.2.2 to an almost-as-old version 3.0.something. It then offered to upgrade that to 4.8.something, when I gave up.

For starters, I have a separate project called AndroidTemplate that is a stripped-down (relatively) sample application. It’s basically what you get with the Android Studio New Application wizard, minus some of the fancier stuff that isn’t needed.

To work on an app today, you’ll probably want to be using AndroidX libraries, so this process includes that.

Here’s the set of changes I wound up making:

build.gradle
	replace from AndroidTemplate
gradle.properties
	add android.useAndroidX=true
settings.gradle
	copied from AndroidTemplate, updated rootProject.name
app/build.gradle
	Copied from AndroidTemplate, restored applicationId in defaultConfig
app/src/main/AndroidManifest.xml
	add android:exported="true" to <activity> and any other components with intent-filter
	N.B. Security: Set exported false for activities behind a login activity!
app/src/main/res/values/styles.xml
	Change `app/res/values/styles.xml` to extend Theme.AppCompat (or a subtheme thereof). I used:
	<style name="AppTheme" parent="Theme.AppCompat"> ... Note: No 'android:' at front.
gradle/wrapper/gradle-wrapper.properties
	Updated distributionUrl filename part to latest (currently gradle-7.3.3-bin.zip)

Then you can open the project in Android Studio. You will have to manually repair the imports, which probably had old libraries, and maybe re-add libraries into app/build.gradle (but do use the Module Settings→Dependencies to find them).

With those changes, I had a working project. Then I was able to add the new feature code I had started out with. Now that it’s documented, this process shouldn’t take more than ten or fifteen minutes per old project.

A simpler plan might be to create a new project in Android Studio and just move the source files into it. The way I have it outlined here makes it easier to preserve the project history, if that matters to you.

Once you get here, you can run your tests; if they pass, run the app. See if it’s been affected by any other breaking changes, of the kind Android is famous for. Watch in logcat for exceptions being thrown, app misbehavour, etc. Track down any aberrations here before you start modifying the code. And, of course, update your tests at the same time as you update the code.

There’s probably more attention needed to make an app presentable today - you need to study up on the current UI standards like Material You, the new ConstraintLayout, and much more. But the steps here should at least get you started by enabling compilation and running with the current (2022) Android Studio build tools.