Search code examples
androidgitgithubrepositorygit-submodules

Adding a repository to my android project as a submodule and keeping my modifications in it in sync with main repo


I've been having issues adding a submodule to my android project. I've tried all tutorials and some solutions available online but none exactly says what to do add a submodule and also keep your commits on it in sync with the main repo.

Solutions I've tried:

  1. Git submodule add repo-link ISSUE: it adds the submodule to my project but when I click on it to create a new activity in the submodule, it doesn't show an option to create an activity. creating an activity in added submodule

TO FIX THIS: I added implementation project(':Tools_Demo_App') in my build.gradle dependancies, then it shows this error. error

Then, if I add include ':Tools_Demo_App' to my settings.gradle. This error goes away.unable to add activity But still, I'm not able to add an activity. It says "Java file outside of source root".

  1. Another solution I tried is to import a module in Project Structure Add Module Assume that before I clone my repo that I wanted to add as a submodule already, and now I'm adding it as a module in my project. It solves that problem of not being able to add an activity. But here, the problem is it doesn't get added as a submodule so all changes I make doesn't get reflected in my main repo.

No matter what I do, so far none of the tutorials available online has helped. Please suggest something, if you've worked with submodules and was able to work with them keeping everything in sync with main repo.

What actually I want to do:

Main Project: A submodule: demo I want to keep demo repo as a base app to keep all my designs, libraries and templates so that I can just add this submodule to all my projects and just pick reusable stuff from here instead of rewriting everything everytime.


Solution

  • I figured out the ideal solution to this. My project name: A submodule name: x

    terminal

    git submodule add github-link-to-submodule-x
    git submodule update --init --recursive
    

    settings.gradle

    include ':app'
    rootProject.name = "TutorialKotlin"
    include ':Tools_Demo_App'
    project(':Tools_Demo_App').projectDir = new File('Tools_Demo_App/app')
    

    build.gradle

     implementation project(':Tools_Demo_App')
    

    build.gradle of library module

    change

    apply plugin: 'com.android.application'
    

    to apply plugin: 'com.android.library'

    remove applicationId

    make sure there is no launcher activity in AndroidManifest.xml

    sync the project and it works fine.