9

I'm hoping to modularize an Android project so it will be easy to maintain in future use. I want to know whether its possible or not, to make an Android project used as a module within another Android project?

For example, let's say in my main android project I have three buttons for login, signup, forget password. and I have three separate projects for login, signup and forget password with their own views, activities and libraries. How can I use these separate android projects in the main Android project so when I click any of the three options in the main activity view so it will load the relevant view from those three module projects? (click the login button in the main view and load login view from the separate login project activities). I have tried to fiddle with build files and library module importing but couldn't do it. help me with saying if this approach is possible or not? if possible how to achieve it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pixxu Waku
  • 423
  • 8
  • 21
  • Does this answer your question? [How to update an imported module in Android Studio?](https://stackoverflow.com/questions/32579557/how-to-update-an-imported-module-in-android-studio) – hata Jul 11 '21 at 07:27

2 Answers2

16

settings.gradle in root project include each project.

include ':loginApp'
include ':signupApp'
include ':resetpasswordApp'

project(':loginApp').projectDir = file('path/to/LoginApp')
project(':signupApp').projectDir = file('path/to/SignupApp')
project(':resetpasswordApp').projectDir = file('path/to/ResetpasswordApp')

build.gradle Of main module

implementation project(':loginApp')
implementation project(':signupApp')
implementation project(':resetpasswordApp')
Khaled Lela
  • 7,831
  • 6
  • 45
  • 73
  • 1
    Thanks @Khaled, i did include modules and given project dependencies seperately. and included project implementations too. but from my main project i couldnt fire up the module project or the relevant view. – Pixxu Waku Oct 18 '18 at 06:48
  • When you implement other project in `build.gradle` that will enable you to reference to any file just import package then easy use it. – Khaled Lela Oct 18 '18 at 06:54
  • 1
    okay understood. ill import with the package and refer it to use. – Pixxu Waku Oct 18 '18 at 06:56
6

Yes, it's possible. Your current project is basically a module too. See the app folder.

An easy way is to create a new module (android library) with a different name from the current one which by default is app. Let's call it bigapp. This module will have all properties just like your app and you can select to run it from the debug configuration too.

Go to settings.gradle and ensure the file reads include ':app', ':bigapp'

To use the bigapp and call its functions, import it in your dependency. This will be the build.gradle file of the app module.

dependencies {
    implementation project(":bigapp")
}

Lets now start the MainActivity from our app module

startActivity(new Intent(this, com.lucemanb.bigapp.MainActivity.class));

You can also import it at the top and simply call the MainActivity

import com.lucemanb.bigapp.MainActivity;

---

private void startActivity(){
    startActivity(new Intent(this, MainActivity.class));
}
Lucem
  • 2,912
  • 3
  • 20
  • 33
  • Hi lucem, and thanks for the reply, yes i did that and it build successfully. but what i cant get around is to launch an activity from the library/module project when i click an option button from the main project activity. how can i tell my main project activity for an example, to show the view and activity for login from the module when i click the login button – Pixxu Waku Oct 18 '18 at 06:41
  • Wonderful. i tried to call through an Intent, but recieve nothing in return. – Pixxu Waku Oct 18 '18 at 06:45
  • @PixxuWaku take a look – Lucem Oct 18 '18 at 06:51
  • got it. i'll check it out – Pixxu Waku Oct 18 '18 at 06:54
  • if it solved your issue, mark it as the correct answer @PixxuWaku – Lucem Oct 18 '18 at 06:56