I just tried something similar. I tried to add the getLibraryVersion () method to my own application class. So I could call
MyLibrary.getLibraryVersion()
from the code that includes this library. But it seems that you cannot access String resources via getText() or getString() as follows:
public class MyLibrary extends Application { @Override public void onCreate() { super.onCreate();
Since the onCreate () method does not seem to be called, the instance is always null!
Since this method did not work, and as you saw, you cannot access the version the way you tried, I just encoded the version and library name into my own application class as follows:
public class MyLibrary extends Application { public String getLibraryName() { return "org.yourCompany.android.lib.YourLibName"; } public String getLibraryVersion() { return "1.0.0"; } }
I know this is a kind of dirty solution, and I would prefer a cleaner version of the encoding, and these strings are stored as String resources in the strings.xml file, but I don't know a better way. Therefore, you just need to change the name and version of the library in your manifest or better strings.xml AND in the Application class.
But how often do you change the name or version of the library? Hope this helps someone and save some time!
PS: some of the above code is based on the following: http://blog.tomgibara.com/post/126377651/global-application-state-in-android
source share