Ryan Ryan - 2 months ago 17
Android Question

Android - How to extend application class mid-project for LeakCanary

TLDR:
I have to extend the application class but don't know where to start.

Ok... I know this is a dumb one but I have to ask it.

I need to install LeakCanary and I see in their documentation that it requires me to put code in the Application Class.

I understand how to make a java class that extends the application class, but I have some questions before I do this.

SITUATION:


  1. I have 9 classes in my app already.

  2. The entry point to my app is MainActivity.class (figures, right?)

  3. I have not had to extend the Application Class yet.

  4. App name is "CST-ALERT"



QUESTIONS:


  1. Do I just create a new Java class in my java source folder and name it the same as my overall
    package name? What do I name this new class?

  2. Do I need to adjust the manifest now to have this as the entry point
    instead of MainActivity? Which I imagine will change things quite a
    bit for me.

  3. Do I have to change anything else in the manifest? I assume I will
    at least have to declare it in there.



Hopefully my situation and questions will give you an idea of what I am asking.

Answer

Do I just create a new Java class in my java source folder

Yes.

and name it the same as my overall package name?

If by "it", you mean the Java package, you already have a directory for your existing Java code. Put your subclass of Application in there, unless you have a clear reason not to.

In this trivial sample app, I put my Application subclass in the same directory as my launcher Activity, which is the directory for my Java package (com.commonsware.android.button).

What do I name this new class?

That is up to you. I called it CanaryApplication. If you think you leak a lot, call it CoalMine. So long as it is a legal Java class name, it doesn't matter.

Do I need to adjust the manifest now to have this as the entry point instead of MainActivity?

Um, well, I wouldn't describe MainActivity as the entry point. Regardless, MainActivity is left alone. You need to add an android:name attribute to the <application> element, identifying your Application subclass.

Do I have to change anything else in the manifest?

No, that should suffice.