Cannot call method from another Module : Android studio

erluxman picture erluxman · May 26, 2017 · Viewed 9.6k times · Source

I have two modules in android studio

  1. Standard android app module
  2. domain module

The domain module in been added to both settings.gradle and build.gradle

include ':mobile', ':domain' & compile project(':domain') respectively like this

Inside domain module I have a class like this :

public class DomainUtils {

    Context mContex;
    public DomainUtils(Context context){
        this.mContex = context;
    }
    public  void toast(String string){
        Toast.makeText(mContex, string,Toast.LENGTH_LONG).show();
    }
    public String returnHi(){
        return "hi";
    }
}

But when i try to call new DomainUtils(context).toast("hi");

from a class inside App module :

  1. The method inside DomainUtils does not execute
  2. Program flow does not continue to next line in the calling class ( program flow stops"
  3. I do not see any error logs in logcat.

------------BUT ----------

When I run the method returnHi() It works fine .

Answer

Sreehari picture Sreehari · May 26, 2017

First inside Main Project Folder's settings.gradle mention the library

include ':app', ':domain'

And include versions if available as well , like

include ':app', ':library-2.19.0'

Now inside app folder under path MainProject>app>build.gradle include

dependencies {
  ..........
  compile project(':domain')
}

Again include version details if available. Check out this for more details

Based on the comments, you can do one more verification if library is properly included or not. Clean and rebuild should configure properly, but still just make sure the following is updated by Android Studio .

Check in app.iml if the module has got included or not

MainProject > app > app.iml

There should be an entry in <component> tag like below

<orderEntry type="module" module-name="domain" exported="" />

Edit :

Try to run your Toast message inside runOnUiThread. It should solve the error.