5

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 .

erluxman
  • 18,155
  • 20
  • 92
  • 126

3 Answers3

6

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.

Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • I think I have already done what you are suggesting but no luck – erluxman May 26 '17 at 09:23
  • Is the location where you edited build.gradle the wright one? There are more than one . You might be aware but , just in case you missed out.. – Sreehari May 26 '17 at 09:28
  • yeah first is settings.gradle, second one is build.gradle inside app directory I do not have versionings so The second snippt is not useful for me – erluxman May 26 '17 at 09:39
  • Check out my edit , if it gives any clue. Or else I dont see any exceptions. :( – Sreehari May 26 '17 at 09:52
  • Yeah I can see that app.iml things you have mentioned – erluxman May 26 '17 at 09:56
  • 1
    Hope your debugger reaches till Toast message line. If then try with Toast message inside runOnUiThread ... Its just a wild guess . May be an absolute mistake also... :) – Sreehari May 26 '17 at 10:01
  • 1
    As I told in previous comment Just try with runOnUiThread inside your DomainUtils – Sreehari May 26 '17 at 10:13
  • You are amazing I was mistakely calling that method from BG thread . Now it works fine as expected.. Please edit your answer to point that I might be running in background ... that has to be the accepted answer – erluxman May 26 '17 at 10:21
  • or lets delete this question coz the problem , solution and the title are irreverent – erluxman May 26 '17 at 10:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/145185/discussion-between-erluxman-and-stallion). – erluxman May 26 '17 at 10:25
  • vote to close this question please ... The problem was not directly related to the question, It was rather due to side effect of another piece of code – erluxman May 26 '17 at 10:30
3

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");
Nikhil
  • 1,212
  • 13
  • 30
Sujith Ks
  • 360
  • 2
  • 10
-1

From file settings.gradle add full path module.

include ':your_module'

project(':your_module').projectDir = new File(settingsDir, 'absolute_path').

absolute_path start from folder container settings.gradle

Dungnbhut
  • 176
  • 5
  • I can easily access the class and even auto complete is working , the problem is in runtime – erluxman May 26 '17 at 09:13
  • vote to close this question please ... The problem was not directly related to the question, It was rather due to side effect of another piece of code – erluxman May 26 '17 at 10:30