dev.farmer
dev.farmer

Reputation: 2779

How to use android.util.Log in the extra module of Android?

I am developing an Android app with Clean Architecture. So I separated the modules to 'app', 'data', 'domain'.

But what I want to do is using an android.utils.Log in the module 'data', 'domain'.

I cannot find it in the 'data', 'domain' modules.

Below is my 'domain' module's gradle file.

apply plugin: 'java-library'

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "7"
targetCompatibility = "7"

Should I add something in here?

Or should I just use "System.out.println()" method?

Upvotes: 0

Views: 982

Answers (1)

ror
ror

Reputation: 3500

First of all I have to confirm your intentions are good :)

You need to do the following to achieve what you want:

  1. Define final Log class + interface defining log, both in your domain:

    public final class Log {
     private static LogInterface logInterface;
    
     public static void d(String tag, String message) {
        logInterface.d(tag, message);
     }
    
     public static void setLogInterface(LogInterface logInterface) {
         Log.logInterface = logInterface;
     }
    
     public interface LogInterface {
        void d(String tag, String message);
        //...
     }
    }
    

Notice all of the above are pure java, nothing bound to android.

  1. In any of your android modules, create and implement android logger:

    public class AndroidLog implements Log.LogInterface {
     public void d(String tag, String message) {
        android.util.Log.d(tag, message);
     }
    }
    
  2. Probably in the same module as p2, initialize (initialization likely should happen when app is created) :

    Log.setLogInterface(new AndroidLogger());
    

Now you can use your domain's Log like this: Log.d(...) - all around in your pure java modules.

Upvotes: 1

Related Questions