1

My Application class code:

import android.app.Application;
import android.os.Process;

import com.squareup.leakcanary.LeakCanary;

public class SampleApplication extends Application {

    public SampleApplication() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //noinspection ConstantConditions
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
        // Normal app init code...
    }

    @Override
    public void onTerminate() {
        System.exit(0);
        Process.killProcess(Process.myPid());

        super.onTerminate();
    }
}

Android Studio 3.1 Canary 7 --> Analyze --> Inspect Code --> OK:

Declaration access can be weaker inspection

1 warning: SampleApplication can be package-private

Can Application class be package-private or it's lint error?

Alexander Savin
  • 1,952
  • 1
  • 15
  • 30
  • Yes it can be package private – UdeshUK Jan 06 '18 at 07:24
  • @UdeshUK, [Application](https://developer.android.com/reference/android/app/Application.html): "The Application class, or your subclass of the Application class, is instantiated **before any other class** when the process for your application/package is created". Does it mean that Application class must be public? – Alexander Savin Jan 06 '18 at 10:53
  • As referenced by Alex, from Java documentation - "A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package" – UdeshUK Jan 06 '18 at 11:04

2 Answers2

3

Yes it can be package private. Check out this question in Stack overflow and also read this to learn how to control access to members of a class.

For a further explanation: inhering from Application alows you to start the app without a main method (the class which inherits acts as the main class). It needs to be public if you're accessing other packages with it. If not, you can set it as package-private.

Alex Cuadrón
  • 638
  • 12
  • 19
  • [Application](https://developer.android.com/reference/android/app/Application.html): "The Application class, or your subclass of the Application class, is instantiated **before any other class** when the process for your application/package is created". Does it mean that Application class must be public? – Alexander Savin Jan 06 '18 at 10:53
  • 1
    @AlexanderSavin Inhering from Application alows you to start the app without a main method (the class which inherits acts as the main class). It needs to be public if you're accessing other packages with it. If not, you can set it as package-private. – Alex Cuadrón Jan 06 '18 at 11:00
  • Alex Cuadrón thanks, I accepted your answer, please add your comment to your answer – Alexander Savin Jan 06 '18 at 11:09
  • @AlexanderSavin done, thanks for selecting as answer :D – Alex Cuadrón Jan 06 '18 at 11:11
0

Just add the public modifier to your inaccessible class

m'hd semps
  • 564
  • 4
  • 14