Is it possible to get application's context in an Android Library Project?

AndroidAndroid ContextAndroid Library

Android Problem Overview


I would like to get the context of application which has reference/hosted my library at run-time inside one class of my library project. Is it possible? If yes, how?

Thanks

Update I don't want my user to pass context in parameter to my library project because it is possible that my library project will be called through JNI and I have no idea how I can get context in JNI and pass it to Java layer.

Android Solutions


Solution 1 - Android

There is one more way, add application class in your library project:

/**
 * Base class for those who need to maintain global application state.
 */
public class LibApp extends Application {

  /** Instance of the current application. */
  private static LibApp instance;

  /**
   * Constructor.
   */
  public LibApp() {
    instance = this;
  }

  /**
   * Gets the application context.
   * 
   * @return the application context
   */
  public static Context getContext() {
    return instance;
  }

}

Then in your regular project make the real application class extend LibApp:

/**
 * Base class for those who need to maintain global application state.
 */
public class App extends LibApp {

  @Override
  public void onCreate() {
    super.onCreate();
  }

}

Make sure that you have "Name" defined in AndroidManifest:

<application android:name="App" ...>

and that your App class is in the base package.

You can then use LibApp.getContext() your library project to get the application context of the application that is using the library.

This may not be good solution but it works for me. I am sharing it because it might be useful to somebody else.

Solution 2 - Android

> Is it possible?

Yes.

> If yes, how?

Pass it in as a parameter.

> I don't want my user to pass context in parameter to my library project because it is possible that my library project will be called through JNI and I have no idea how I can get context in JNI and pass it to Java layer.

Then figure out "how [you] can get context in JNI and pass it to Java layer". I would imagine that you would pass it like any other object. As @Blundell noted, you do not really have any other option.

Solution 3 - Android

There's another way to get context in jni, neither passing a parameter nor saving context by yourself, but by using android api. I found that there's a class named:

  • android.app.AppGlobals

in the source code. And the static function

  • getInitialApplication

can return an Application object. But it must be called in main thread, and the class is hidden. Anyway you can use it by reflecting in java. And you can just use FindClass() and FindStaticObjectMethod() to find out the method, and use it. Hope that helps.

Solution 4 - Android

I would pass it as a parameter or pass it a singleton in that library.

Having the main app application extend the library's application class is a bad idea coz in java you can only extend once from a class. If your application requires to pass to another library you will be in trouble.

Solution 5 - Android

According to this post you can let the library auto-initialize itself with the application context by the aid of a ContentProvider.

Be careful anyway, as described in the post comments, there may be drawbacks concerning loading time and instant run, as well as crashes on multi-process apps.

HTH

Solution 6 - Android

Kotlin version. In my case, this post helped me. The main app module passed the context to the library companion object context var.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaris HasanView Question on Stackoverflow
Solution 1 - AndroidpecepsView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidLamView Answer on Stackoverflow
Solution 4 - AndroidSayooj ValsanView Answer on Stackoverflow
Solution 5 - AndroidDario FiumicelloView Answer on Stackoverflow
Solution 6 - AndroidOlegView Answer on Stackoverflow