Trying to build an old Android app that has fallen out of date. Doing this always brings up some weird errors.

The version of Gradle being used was woefully out of date, so I starting upgrading it to the latest minor version used by the project (it was 3.1.0 to 3.2.x). This required changes to the distributionUrl in gradle-wrapper.properties. After making these changes, I was getting 502 errors when Android Studio was trying to get the binaries from jcenter.bintray.com. I feared that the version of Gradle I was hoping to use was so out of date the binaries may no longer be available: it wouldn’t be the first time Java binaries disappeared as various sites changed URLs or shutdown. Fortunately the problem was transitory, and I managed to get a 3.2.x version of Gradle downloaded.

The various squiggly lines in the .gradle files disappeared, but I was still unable to build the app. Attempting to do so brought up the following error:

Unsupported class file major version 55

This was weird. The only version of Java I had installed was Java 11, and I confirmed that Gradle was using the specific install by checking it in Preferences → Build, Execution, Deployment → Build Tools → Gradle. So I couldn’t see why there would be a major version conflict. Was Gradle using a bundled version of Java 8 somewhere to build the app?

What I think resolved this, along with upgrading Gradle, the SDK and the build tools, was explicitly setting the source and target capability of the app to the same version of Java I was using. From within “build.gradle”, I added the following:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_11
    targetCompatibility JavaVersion.VERSION_11
}

Might be that the version of Gradle I was using was so old it defaulted to building Java 1.8 byte code if these weren’t specified.

This error disappeared and the app was stating to build. But the next weird thing I was then getting was that the compiler was not seeing any of the Android SDK classes, like android.app.ListActivity. What seemed to work here was to upgrade Gradle all the way to the latest version (7.3.3), clearing the cache used by Android Studio, and making sure this line was added to the app module dependencies:

implementation fileTree(dir: 'libs', include: ['*.jar'])

This finally got the build working again, and I was able to run the app on my phone.

I don’t know which of these actions solved the actual problem. It’s working now so I rather not investigate further. The good news is I didn’t need to change anything about the app itself. All the changes were around the manifest and Gradle build files. Those Android devs really do a great job maintaining backwards compatibility.