Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just tried something similar. I tried to add a method getLibraryVersion() to my custom Application class. So I would be able to call</p> <pre><code>MyLibrary.getLibraryVersion() </code></pre> <p>from within the code that included that library. But it seems that you can not access the String resources via <code>getText()</code> or <code>getString()</code> like this:</p> <pre><code>public class MyLibrary extends Application { @Override public void onCreate() { super.onCreate(); // provide an instance for our static accessors MyLibrary.instance = this; } private static void checkInstance() { if (MyLibrary.instance == null) { throw new IllegalStateException("Application not created yet!"); } } /** * @return the library name */ public String getLibraryName() { MyLibrary.checkInstance(); return MyLibrary.instance.getString(R.string.app_project_name).toString(); } ... } </code></pre> <p>Because the onCreate() method seems not to be called, the instance is always null!</p> <p>As this way was not working out, and as you saw you cannot access the version the way you tried, I just hard coded the version and the library name, into my custom application class like this:</p> <pre><code>public class MyLibrary extends Application { /** * @return the library name */ public String getLibraryName() { return "org.yourCompany.android.lib.YourLibName"; } /** * @return the library version */ public String getLibraryVersion() { return "1.0.0"; } } </code></pre> <p>I know that this is kind of a dirty solution, and I would prefer a cleaner version of coding, with these Strings stored as String resources in strings.xml but I don't know any better way. So you just have to change the library name and version in your manifest or better the strings.xml AND in the Application class.</p> <p>But how often do you change the library name or version? Hope this can help somebody and save time!</p> <p>PS: some of the above code is based on this: <a href="http://blog.tomgibara.com/post/126377651/global-application-state-in-android" rel="nofollow">http://blog.tomgibara.com/post/126377651/global-application-state-in-android</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload