Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access private strings of the Android OS programmatically?

Background

I would like to get all of the strings of the Android OS (including all of them) programmatically, including those that are considered private.

For example, I would like to get those of the packageManager app, as found here .

The problem

Using android.R.string only returns a small portion of the strings.

What I've tried

I've found this link, which shows the next code, but I'm not sure what to put into the parameters:

private String GetAttributeStringValue(Context context, AttributeSet attrs, String namespace, String name, String defaultValue) 
    {
        //Get a reference to the Resources
        Resources res = context.getResources();
        //Obtain a  String from the attribute
        String stringValue = attrs.getAttributeValue(namespace, name);
        //If the String is null 
        if(stringValue == null)
        {
            //set the return String to the default value, passed as a parameter
            stringValue = defaultValue;
        }
        //The String isn't null, so check if it starts with '@' and contains '@string/'
        else if( stringValue.length() > 1 && 
                 stringValue.charAt(0) == '@' && 
                 stringValue.contains("@string/") ) 
        {
            //Get the integer identifier to the String resource
            final int id = res.getIdentifier(context.getPackageName() + ":" + stringValue.substring(1), null, null);
            //Decode the string from the obtained resource ID
            stringValue = res.getString(id);
        } 
        //Return the string value
        return stringValue;
    }

I've seen some apps in the past that can list various resources of other apps, including of the system itself (example here).

Later I've found how to get a string from any app you wish, but it assumes you know the name of the identifier, and doesn't offer you to list them:

fun getStringFromApp(context: Context, packageName: String, resourceIdStr: String, vararg formatArgs: Any): String? {
    try {
        val resources = context.packageManager.getResourcesForApplication(packageName)
        val stringResId = resources.getIdentifier(resourceIdStr, "string", packageName)
        if (stringResId == 0)
            return null
        return resources.getString(stringResId, *formatArgs)
    } catch (e: Exception) {
        return null
    }
}

For example, if you want to get the string of "more" (key is "more_item_label"), you can use :

val moreString = getStringFromApp(this,"android", "more_item_label")

The question

Is such a thing possible? If not, is it possible to do it with root?

like image 671
android developer Avatar asked Mar 26 '13 11:03

android developer


1 Answers

Your link show how get resources from preferences screen and say :

 The goal of this example was to show how to obtain the correct String resource outside the Android XML namespace

Link describe that you can get resources by alternative way ,not only by R.string.

I create a demo project with another custom namespace and custom button Label,but unfortunately i cannot show string resources from one project to another.

like image 115
Yahor10 Avatar answered Oct 21 '22 23:10

Yahor10