Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Package string from Android manifest

This should be simple but I can't find any info on this...

I simply want to read the package value in the android manifest...

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="THIS"

the reason is simple I have to call context.getResources().getIdentifier(...) and I need the package.

since this code will be reused in other apps I want to make it fool proof when I export it and therefore not have to change the value each time.

anybody knows how to do this ?

like image 833
Jason Rogers Avatar asked Sep 14 '10 02:09

Jason Rogers


2 Answers

Within an Activity, you can simply call getPackageName(). If you should happen to need additional data from the manifest, you can use the PackageInfo class: http://developer.android.com/reference/android/content/pm/PackageInfo.html

Example of setting a TextView to your app version:

    try {
        PackageManager pm = getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
        TextView version = (TextView) findViewById(R.id.version);
        version.setText(packageInfo.versionName);
    } catch (NameNotFoundException e) {}
like image 147
Ian G. Clifton Avatar answered Oct 05 '22 17:10

Ian G. Clifton


From your "main" Activity class:

String package = this.getClass().getPackage().getName();
like image 24
Tyler Avatar answered Oct 05 '22 15:10

Tyler