Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Application class in library project

Tags:

android

I have an android library project being referenced by an application project. The library project has a custom class extending the base Application class where I have done some initializations. Now the problem is that this class doesn't get called automatically even though I have registered it correctly in the manifest file of the library project. I even tried to call it using

 MyApplication app = new MyApplication();
 app.onCreate();

But this gives me a null pointer exception.

What I think is that application class does not get called in the library project automatically. If this is true then what's a better way to call it manually from with in the library project?

like image 785
Sayed Jalil Hassan Avatar asked May 22 '13 14:05

Sayed Jalil Hassan


People also ask

Can Android library have application class?

You can extend the library's Application class in the application project and provide any additional implementation. If you do this, you will have to use this extended Application in the manifest.

What is lib in Android?

An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest.

What is AAR file in Android?

In addition to JAR files, Android uses a binary distribution format called Android Archive(AAR). The . aar bundle is the binary distribution of an Android Library Project. An AAR is similar to a JAR file, but it can contain resources as well as compiled byte-code.


1 Answers

This is a limitation of Android library projects. The AndroidManifest.xml of a library project is effectively ignored in a project that uses that library.

Thus you need to put any <activity>, <service>, and other such tags in the end application's manifest.

Note: While the above was true in 2013 (and is still true if you are using Ant as your build system), the Android Gradle plugin will merge manifests from AAR libraries into your manifest automatically.

like image 80
Bryan Herbst Avatar answered Oct 12 '22 23:10

Bryan Herbst