4

I am working on an Android application that requires more information than what is available in the PackageManager (such as intent filters). I have created a parser for reading the AndroidManifest file, but I cannot find the location of the AndroidManifest.xml for any installed application. The code I am using is:

int flags = PackageManager.GET_ACTIVITIES
                       //| PackageManager.GET_CONFIGURATIONS
                       //| PackageManager.GET_DISABLED_COMPONENTS
                       //| PackageManager.GET_GIDS
                       //| PackageManager.GET_INSTRUMENTATION
                       //| PackageManager.GET_INTENT_FILTERS //Not needed (see BUG: http://code.google.com/p/android/issues/detail?id=3217)
                       | PackageManager.GET_META_DATA
                       | PackageManager.GET_PERMISSIONS
                       | PackageManager.GET_PROVIDERS
                       | PackageManager.GET_RECEIVERS
                       | PackageManager.GET_RESOLVED_FILTER
                       | PackageManager.GET_SERVICES
                       | PackageManager.GET_SHARED_LIBRARY_FILES
                       | PackageManager.GET_SIGNATURES
                       | PackageManager.GET_UNINSTALLED_PACKAGES
                       | PackageManager.GET_URI_PERMISSION_PATTERNS;
final PackageInfo pkg = pm.getPackageInfo(info.packageName, flags);
File manifest = new File(pkg.applicationInfo.publicSourceDir + "/AndroidManifest.xml");
if (manifest.exists()) {
    //Code never reaches this point
}

According to the documentation, ApplicationInfo.publicSourceDir is:

Full path to the location of the publicly available parts of this package (i.e. the primary resource package and manifest). For non-forward-locked apps this will be the same as {@link #sourceDir).

What am I doing wrong, or how can I do this right?

Phil
  • 35,852
  • 23
  • 123
  • 164
  • I suppose that manifest are stored in dex archive. Can you see what files are in `pkg.applicationInfo.publicSourceDir`? – Jin35 Dec 20 '11 at 07:14
  • @Jin35, when I did `new File(pkg.applicationInfo.publicSourceDir).list()`, it returns null. – Phil Dec 20 '11 at 07:44

3 Answers3

1

Here the code:

AssetManager am = yourContext.createPackageContext("packageName", 0).getAssets();

XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");

but then you need to parse it.

gwvatieri
  • 5,133
  • 1
  • 29
  • 29
  • this doesn't work for me. Do you have an example of this code in context? – Phil Dec 20 '11 at 14:23
  • I use this code in my app for reading the 'installLocation' attribute, it works well. What happens with you? – gwvatieri Dec 21 '11 at 07:42
  • I get a `FileNotFoundException`, which is strange since `am.list()` displays the *AndroidManifest.xml*, along with a few other assets. – Phil Dec 21 '11 at 22:13
1

It took a long time, but I figured it out. Assets, such as the manifest, are compressed - see this post. If it had not been compressed, the code provided by @gwa (or something similar to it) would have done the trick.

Community
  • 1
  • 1
Phil
  • 35,852
  • 23
  • 123
  • 164
0

I don't think your app have enough privilege to read it. Try it on an rooted device.

pinxue
  • 1,736
  • 12
  • 17