“Programming is about managing complexity: the complexity of the problem, laid upon the complexity of the machine. Because of this complexity, most of our programming projects fail.” ~ Bruce Eckel
We all have that nightmare, an old project coming back from the grave for some reason.
Some days ago, I got a call from a customer, that never paid for maintenance, asking for “a simple edit”. And it was easy, I just had to edit a regular expression and trigger a build.
And there, lying behind that simple edit, waiting, was the Android Studio Build. Once I pressed that button, all my problems started.
The last project commit was from September ’19, more than a year ago. And according to npm repositories, for that project were: 20 major updates, 12 minor updates and 6 patches.
Updating everything wasn’t an option: the customer didn’t want to pay for that. So I had to find a way to build the project in the fastest possible way.
Rebuilding September ’19 environment
This was the challenge, rebuild the exact setup I had on September ’19 without breaking my current setup.
Recently, less than 3 months ago, I started using docker. I really wanted to learn how Docker and containerization work, but I didn’t have enough time, so I embraced the philosophy “use first, learn later”.
I never considered using it for Android development, but… why not?
I’m not a dockerist, so I started searching on Google and I found an interesting Github repository from react-native-community.
I grabbed the
Dockerfile from the repository and put that on my project.
That was a starting point. Now I needed tweaking:
- I didn’t need Buck, so I just removed it.
- I didn’t need the NDK, trashed that too.
- Dependency versions weren’t the ones I needed, so I changed them.
After these tweaks, I didn’t change anything.
Now build the container and run it!
docker build -t project-android-build .
In my environment, 4 cores and 2048MB of RAM on WSL2, it took 816.3s.
docker run --name=running_container -v F:\Projects\my-app:/app -it project-android-build
I had my September ’19 environment!
It’s time for a build
Everything was set up and ready. I needed to install dependencies with
yarn install and to trigger the build for my project
cd android && ./gradlew clean && cd .. && npx jetify && cd android && ./gradlew bundleRelease
If you’re lucky as I was, you’ll finally have your bundle ready.
A final thought on Docker
Well, if I have to summarize what I think in just a few words I would say “It’s amazing”.
Docker provides a simple throwaway development environment that you can spin up, use anytime you need it and finally, thrash and spin up again when needed. It’s amazing because even if the project keeps collecting dust for a while, the next time I need to edit another regex, I can just spin the container up again and rebuild. I can even delete the image and rebuild it if every mirror stays up. That’s, in my opinion, the real power of docker, set your environment, change your project!