Get android: versionName from library project

I created a library project that I am importing into another project. In this library project, at some point I retrieve it android: versionName For this you need to specify the package name. The problem occurs when this code is executed, when the library project is included in another project, then this code seems to throw an exception: 10-04 10: 15: 36.987: WARN / System.err (1407): getSoftwareVersion (), Caught Exception: android.content.pm.PackageManager $ NameNotFoundException: mobilaria.android.LandenPlayerCodeBase.baseplayer

This is the name of the package of the project library package ... it seems that he cannot find it, even if the same code that makes this call is part of the library itself ...

Has anyone experienced something similar or have an idea on how to solve this?

+2
source share
2 answers

As far as I know, the manifest of the android library project is being ignored at the moment, the manifest is not combined into the final application when you reference the library. Therefore, you cannot extract any data from the library manifest.

+2
source

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(); // 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(); } ... } 

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 { /** * @return the library name */ public String getLibraryName() { return "org.yourCompany.android.lib.YourLibName"; } /** * @return the library version */ 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

+1
source

All Articles