Redwarp
Redwarp

Reputation: 3231

Reading manifest file: list <uses-library>

I'm developing a library, and let say this library relies on Google map for exemple.

When it's used by someone in an Android app, I want to make sure that, in his manifest, he declares in the application tag, the following tag:

<uses-library android:name="com.google.android.maps" android:required="true" />

I'm looking for a way to read the manifest, so that I can display a Toast or a Log that says "hey, dude, you forgot to include "uses-library" in your manifest ! Please correct that, or don't ask me questions, I won't support you" (Basically)

So far, I found ways to read a bunch of stuff (permissions, etc etc), but nothing regarding uses-library tags.

(It is an exemple, I'm not actually using maps v1)

Upvotes: 1

Views: 308

Answers (1)

Michal Harakal
Michal Harakal

Reputation: 1035

I was writing an instrumentation test, where I wanted to check that a particular library isn't used anymore. Just by coincidence it was "com.google.android.maps".

Google's official documentaion states

To check for a library, you can use reflection to determine if a particular class is available.

But there is an other way to read "uses-library" too. Following has to be considered:

  • a flag PackageManager.GET_SHARED_LIBRARY_FILES has to be used for obtaining shared libraries information
  • returned list contains a full path to a linked library with the filename identical to library requested in statement

Following code works for me:

@SmallTest
@Test
public void testUsesNoV1MapLibrary() {
    packageInfo = context.getPackageManager().getPackageInfo(
        PackageManager.GET_SHARED_LIBRARY_FILES
    ); 

    final ApplicationInfo applicationInfo = packageInfo.applicationInfo;
    String[] sharedLibraries = applicationInfo.sharedLibraryFiles;

    if (sharedLibraries != null) {
        for (String libraryPath : sharedLibraries) {
            assertThat("no com.google.android.maps.jar in use", libraryPath.contains("com.google.android.maps"), is(false));
        }            
    }
}

Upvotes: 1

Related Questions