Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call method from another Module : Android studio

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 .

like image 557
erluxman Avatar asked May 26 '17 09:05

erluxman


2 Answers

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.

like image 115
Sreehari Avatar answered Oct 03 '22 10:10

Sreehari


Before you build the project don't forget to add the module inside your build.gradle.

dependencies {

  compile project(':domain')
}

Then while calling the method pass the current class context like as below.

 new DomainUtils(YourClassName.this).toast("hi");
like image 24
Sujith Ks Avatar answered Oct 03 '22 10:10

Sujith Ks