3

I have a Jar that I use in my Android app (it's in the libs folder). The Jar doesn't contains any Android resources or anything like that, but just simple Java classes.

Now, I want the Jar to know which version it currently has in the manifest so that in case of an exception occurring in the Jar, I know what version of the Jar caused the exception.

For some reason, I can't seem to be able to do it... I've seen this similar question and many others, and while this works when I use the Jar in a simple Java app, it doesn't when used in an android app. I've tried getting the manifest by doing this

Thread.currentThread().getContextClassLoader().getResources("META-INF/MANIFEST.MF");

or this

MyClass.class.getClassLoader().getResources("META-INF/MANIFEST.MF");

but I never get the manifest for my jar.

Any help will be greatly appreciated.

Community
  • 1
  • 1
jeremy
  • 904
  • 7
  • 15
  • While you can perhaps obtain the version of the APK, it's not clear how you would get the version of the jar, since the jar does not survive as a distinct entity in the compiled package. Can you embed it as some sort of constant field? – Chris Stratton Apr 16 '14 at 17:23

1 Answers1

-1

I put this in my subclassed android.app.Application but you can use it anywhere you have a context. Just change getPackageManager() to context.getPackageManager().

public int getVersion() {
    int v = 0;
    try {
        v = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
        // Huh? Really?
    }
    return v;
}

You can use the above method to get the version of your Application from the manifest.

ShihabSoft
  • 835
  • 6
  • 15