Using Git Sub Module to reduce development time
Most experienced software developers should be familiar with SOLID. Especially with the most important principle of object-oriented design.
I’m talking about Open/Closed Principle. Bertrand Meyer explained the Open/Closed Principle as:
Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification.
In this article I will not explain the definition of the principle of open and close. Shortly it tells you that you should add new functionalities to your entities without changing the existing one. So, if you need to change one of your classes you don’t have to adapt all depending classes.
We do not want to copy & paste and reuse common codes when developing applications. So the idea is using libraries for common needs of projects. Sometimes we need to make changes at the common library project. But we don’t want our applications affected by this changes.
At this stage git submodule helps us to have control in both common library and applications at the same time. Both of library and applications will be maintained by git.
First we need to make some changes to define our project as a library.
- Comment below lines at the AndroidManifest.xml, because library projects don’t have launcher activity.
2. Comment applicationId field at app gradle folder and replace com.android.application with ‘com.android.library’.
Now click Sync Now and your project is ready to use as a library.
Ok now create a repo and publish the library project to git. And copy the clone url.
Next step is to create an application project at Android Studio. Create a folder named subModule (or something else to host the library in it) at your root project.
I use Sourcetree to manage version control things. So I will show you how to add subModule to your project through Sourcetree.
And paste the library repo url below you copied before. Locale relative path is subModule folder at your application project. Give its path below.
Now go back to Android Studio and you will see common library project code folders under the subModule folder. But we need to say our project that subModule is a library project. And add dependencies.
Go to File -> Project Structure -> Modules
Click + button, choose Import Gradle Project, show subModule folder as Source Directory.
One more thing is adding implementation project(‘::yourlibraryname’) line to the dependencies scope as below.
Ok now click Sync Now. Latest view must be seen as below at Project window, if you followed steps correctly.
Please !! Update sub module only from within its own repo. Otherwise, it may disrupt all other projects already using the library. If you do not use sub module carefully, you may mix things up badly.
Yay! You are ready to use your custom git library as a subModule of your project. Enjoy!
Stay Healthy!
Use git sub module! ;)