Tip on working on 2 dependent git repos

Image for post
Image for post

In bigger organization, different group has their own repo. And the main app will take in the other’s work (from another department) as library from the library server as shown below.

Image for post
Image for post

The challenge

Imagine if we want to make good interfaces, debug, test and experiment between the two, how would we do it? We could make a change on the Dependent Library, upload to the server, and use the Main App to download that latest version to test it.

Doesn’t sound too fun I guess… especially if you have to do this every single change. Besides, we don’t want to pollute our server with lots of SnapShot unnecessarily.

Simple solution…

Well, we have a simple solution. As we are in the same organization, we could download both the repo, and use the import module feature in Android Studio.

Image for post
Image for post

This sounds straightforward. But it is not ideal, as what it actually does is to copy a version of the other Dependent Library code into the Main App code space, and had the new module then, as shown below.

Image for post
Image for post

Why is this not ideal?

The reason is it does mess up with you Main App work space, and when you want to check in something, you have to be careful not to pick the Dependent Library code.

Similarly, when you change the Dependent Library code, you are not making the change to it’s repo space, instead to the copy of it (that is in Main App space). So you have to do the extra work of transferring the changes to the original Dependent Library space, before checking it in.

So how?

Below is what is preferred, where each code base is still stay on it’s own repo, without need to copy over.

Image for post
Image for post

This can be achieve by a little trick, where we could include a module outside of one working space. Follow the steps below.

Manually add the module (from another folder)

In your Main App’s root settings.gradle file, add the module manually.

include ':dependent-module'
project(':dependent-module').projectDir = new File(settingsDir, '../dependent-module-project/dependent-module')

Be careful that dependent-module name might not be the same as your original import module name as seen in the main App’s build.gradle dependent list. The package could have change the name before exporting as Library.

To 100% confirm, you could use the Android Studio import module command to pick that up, the name will be shown to you.

(p/s: this issue wasted my one hour figuring what’s wrong)

Correct dependent-module build.gradle files

After doing the above step, and you perform a gradle sync, you are most likely to face error as below.

Could not get unknown property ‘XXXX’ for object of type ...
Open File

Don’t worry. This is expected as it is now reading the build.gradle from the dependent-module. The dependent-module's build.gradle variable would most likely differs from the Main-App. Hence just need to fix those, mainly from android section. Comment out all unused section (e.g. part that makes and package the library etc).

Point to the newly added `dependent-module`

For all your Main-App's build.gradle (as you might have several modules) that points to the dependent-library, change it to point to your newly added dependent-module. E.g.

implementation(group: "lib-grp", name: "name", version: "$version) {
implementation(project(':ads')) {
exclude group: 'com.android.support'
exclude group: 'com.squareup.okhttp3'
exclude group: 'com.google.android.gms'


implementation(project(':dependent-module')) {
exclude group: 'com.android.support'
exclude group: 'com.squareup.okhttp3'
exclude group: 'com.google.android.gms'

Make sure you retain all the exclude

Finally (but not finish)

Now you could sync and compile. All should work, and you have 2 repo still intact, not corrupting the git diff. You could now make edit and debug happily.

But some further notes below worth check out.

An error that cause me 1.5 days

For my case, I’m less fortunate. I face as error below.

Multiple dex files define <package>/BuildConfig

It took my 1.5 days before I discover that my dependent-module package is also defined in another module in the Main-App. Renaming it fix the problem. The package name is all defined in AndroidManifest.xml. You could use the global search for the package name that is in your dependent-module and see if it is defined else where.

Remember to not check in change in dependent build.gradle

As you recall, we make changes to

  1. dependent-module's build.gradle to be in sync with the Main App build variable
  2. the Main-App's setting.gradle.

So when you check in changes for your dependent-module and Main-App, remember to discard all those changes above, and not check them in.

I hope you appreciate this post and it’s helpful for you.

You could check out my other interesting topics here.

Follow me on medium, Twitter or Facebook for little tips and learning on Android, Kotlin etc related topics. ~Elye~

Written by

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store