2

I have created a Library Project which I import into another project. In that Library Project at some point I retrieve it's android:versionName To do that you need to supply the package name. The problem arises when that code is executed when the Library Project is included within another project, then it seems that that code throws an exception : 10-04 10:15:36.987: WARN/System.err(1407): getSoftwareVersion(), Caught Exception : android.content.pm.PackageManager$NameNotFoundException: mobilaria.android.LandenPlayerCodeBase.baseplayer

Thats the package name of the package of the Project Library... it seems it cannot find it even though the same code that is executing that call is part of the Library itself...

Does anyone have experienced something like this or has an idea on how to solve this ?

TiGer
  • 5,879
  • 6
  • 35
  • 36

2 Answers2

2

As far as I know android library project manifest is ignored at the moment, manifest is not merged into end application when you reference a library. Hence you cant extract any data from the library's manifest.

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • hhmmm ok, that would explain it I guess... But is there any official statement or anyone who knows this for sure ? For example someone who has already worked with a Library Project extensively before ? – TiGer Oct 04 '10 at 10:16
  • 1
    Does anyone know whats the take on this at the moment? Is it possible to retrieve library's version from its AndroidManifest? If not, then where/how to put library's version code/name? – bianca Feb 25 '13 at 23:24
  • @bianca, see my answer below for the way to get it now within Android Studio's Gradle Build system. – JCricket Jun 01 '15 at 23:38
1

I just tried something similar. I tried to add a method getLibraryVersion() to my custom Application class. So I would be able to call

MyLibrary.getLibraryVersion()

from within the code that included that library. But it seems that you can not access the String resources via getText() or getString() like this:

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

Because the onCreate() method seems not to be called, the instance is always null!

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:

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 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.

But how often do you change the library name or version? Hope this can help somebody and save time!

PS: some of the above code is based on this: http://blog.tomgibara.com/post/126377651/global-application-state-in-android

Matthias B
  • 5,523
  • 3
  • 45
  • 47