Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – Multiple Application classes in app’s and library module’s manifest files

Posted on November 11, 2022 By

Solution 1 :

There is a trick that you can use.
We imagine your main application class is Application1 and your library application class is Application2.
Now extend your Application1 from Application2. Just remember to call onCreate of Application2 in Application1‘s onCreate

public class Application1 extend Application2{
@Override
    public void onCreate() {
        super.onCreate();

        //your codes


}

Solution 2 :

Unfortunately, it’s not possible to have multiple Application classes, but there are workarounds, depending on what you’re trying to do.

An Android application can only have a single Application subclass that’s used as the application context.

This is because every Context in the app has a reference to it that can be retrieved by getApplicationContext(). There’s no provision for multiple application contexts, and no way to specify whether you want the “library application context” or the “main application context.”

Assuming that what you want to do is initialize your library on app startup, you can simply create an empty ContentProvider that’s android:exported="false" and do your initialization in its onCreate method. This will be called before the app’s Application.onCreate.

<provider
    android_name="com.my.library.MyLibraryInitializer"
    android_authorities="${applicationId}.mylibrary-initializer"
    android_exported="false"/>
public class MyLibraryInitializer extends ContentProvider {
    @Override
    public boolean onCreate() {
        // perform your initialization here
        return true;
    }

    // Everything below here is just boilerplate to implement the abstract methods
    // in ContentProvider, no need to change it

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        return 0;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }
}

This is, for example, what WorkManager does to initialize itself: it declares a provider in its manifest and performs its initialization within the onCreate method of that ContentProvider.

Problem :

In the library module’s manifest, I need to use an Application class. Is it possible to do this in Android?

READ  [FIXED] android - How do I get a Livedata object to update on a fragment using Room?
Powered by Inline Related Posts

Manifest:

<application
    android_name="com.compnay.app.App"
    android_allowBackup="true"
    android_label="@string/app_name"
    >
</application>

Module library Manifest:

<application
    android_name="com.example.mymodule.App"
    android_allowBackup="true"
    android_label="@string/app_name"
    >
</application>

Comments

Comment posted by stackoverflow.com/q/39506352/9365212

Did you tried any errors did you got any errors? I think it’s possible not tried personally… Check this also

Comment posted by user3742866

in com.example.mymodule.The app I have some for dagger initialization app module and com.compnay.app.App also more code for dagger initialization for the library . so I need to use two Application classes.

Comment posted by user3742866

Merging Errors: Error: Attribute [email protected] value=(com.compnay.app.App.AppController) from AndroidManifest.xml:12:9-52 is also present at AndroidManifest.xml:43:14-77

Comment posted by AgentP

Did you checked @RyanM answer … you can ask doubt to him if you have… in comments

Comment posted by user3742866

Thanks, fo you suggest. Can I use ContentProvider to library startup run code at the Application’s startup? Can you share any example ?

Comment posted by Ryan M

Yes, that’s exactly what my answer describes how to do. I added a skeleton example and also linked to a real-world example.

Comment posted by user3742866

@Override public boolean onCreate() { // perform your initialization here AppController appController = new AppController(getContext()); appController.onCreate(); return true; } Thanks a lot, it is working. But it is possible to get any memory error. ContentProvider when destroying in the application ? This is no ContentProvider lifecycle available on the internet. Can tell us details about the ContentProvider lifecycle and when destroy it .

Comment posted by Ryan M

As far as memory issues, it’s exactly the same as an

Comment posted by Ryan M

@user3742866 You note that this is working – are there any additional issues preventing you from accepting this answer to mark it as correct?

READ  [FIXED] android - Insert on Room DB does not trigger Observer on Activity
Powered by Inline Related Posts

Android Tags:android, android-application-class, android-library

Post navigation

Previous Post: [FIXED] How to get Firebase auth token in Android login
Next Post: [FIXED] android – what should be the input and output forom of variables for float object detection model

Related Posts

[FIXED] android – Why my recyclerview item clones when i call edit item in adapter? Android
[FIXED] android – Exoplayer set mediaSource using ContentResolver#openFileDescriptor(Uri, String) , instead of Uri.parse(MediaStore.Audio.Media.DATA) Android
[FIXED] Android Google Maps – Wrong endpoint coordinates after changing the origin Android
[FIXED] java – android studio Viewpager OnClickListener not working Android
[FIXED] android – The argument type ‘Widget Function(BuildContext)’ cant be assigned to the parameter type ‘Widget Function(BuildContext, Widget)’ Error in Flutter Android
[FIXED] java – firebase phone number authentication getting null reference Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme