Using ChatGPT to generate boilerplate code and enhance productivity
Unless you have been living under a rock, you must have heard of this new Chatbot tool developed by OpenAI called ChatGPT. ChatGPT is a variant of the GPT (Generative Pre-trained Transformer) language model that was specifically designed to support natural language conversation. It is able to generate human-like responses taking into account the context of the conversation. You can use ChatGPT here: https://chat.openai.com/chat
In this article, we are going to use ChatGPT for setting up the library dependencies that we usually need in an Android project. We will also use ChatGPT to generate boilerplate code for us which would help us in improving overall development productivity.
As we are starting our Android project from scratch, I have listed below three basic third-party dependencies that we generally need in Android projects. So we are going to ask ChatGPT to help us in setting up below mentioned dependencies:
- Setting up any DI (Dagger or Koin) framework (We will go with Koin)
- Setting up retrofit for making network calls
- Setting up an Image Loader framework. We will setup Glide
1. Setting up the Koin framework in Android
Koin is a pragmatic lightweight dependency injection framework for Kotlin developers. Written in pure Kotlin using functional resolution only.
Here’s the prompt that I asked for:
Prompt to ChatGPT:
Set up Koin in Android project
Response:
It listed down all the necessary steps required for setting up Koin in the Android project. This includes adding the dependency to build.gradle, creating a dummy module, injecting a ViewModel in an Activity, and a sample code to start the Koin framework from the Application class.
Here are the screenshots of the reply that we received from ChatGPT for the query:
Obviously, the code has to be updated to suit the requirement of the individual project. But this will definitely accelerate the development process when you are writing basic boilerplate code
2. Setting up retrofit for making network calls
Next up we will try and set up retrofit in the Android app. Retrofit is a popular HTTP client library for Android that allows you to make network requests and parse responses from web servers. It simplifies the process of working with HTTP requests and responses in your Android apps and makes it easy to consume RESTful web services.
Prompt to ChatGPT:
Set up Retrofit in Kotlin Android project
Response:
In reply to our query, we have got the dependency that we will have to add for adding retrofit in the Android project. Then we have got some sample code to set up the base URL in retrofit, dummy API requests, and a code snippet for making a network request.
Here are the screenshots of the reply that we received from ChatGPT for the query:
These responses will often be helpful when a developer is setting up these dependencies for the first time in a new project. But the developers have to be cautious while referring to these guides as the steps mentioned here may have been outdated or it may be making use of some deprecated APIs that shouldn’t be used
3. Setting up an Image Loader framework (Glide)
Glide is an image-loading library for Android that allows you to efficiently load, cache, and display images. It is an open-source library developed by Google and is widely used in Android apps to display images from a variety of sources, such as local storage, network, or file system.
Query to ChatGPT:
Set up Glide in Kotlin Android Project
Response:
In response to our query, we have got the dependency that we need to add to the build.gradle file for including Glide. We have also got some sample code to start with the implementation
Here are the screenshots of the reply that we received from ChatGPT for the query:
This was simple and clear. It concluded Glide integration in four steps which should be good enough to kick-start the integration
Conclusion
Now try it out for yourself. Ask queries that are a little more complex like:
Implement recyclerview to download and display images from unsplash.com
I tried the above query and the result was impressive. It was able to write a significant amount of code by itself.
I hope you find this article interesting. And since you have made it this far in this article, do not forget to add a comment below and share the article
Want to connect?
My LinkedIn profile.
I always look forward to your posts. This one, like the others, did not disappoint. Fantastic job!