1

I'm building a plugin for Flutter and I want to access the Android app resource strings from the plugin.

I'm trying using:

@Override
    public void onMethodCall(MethodCall call, Result result) {
        switch (call.method) {
            case "example":
                String value = context.getString(R.string.name);
                result.success(value);
                break;
            default:
                result.notImplemented();
                break;
        }
    }

But R.string points to the plugin resources, not the app resources.

Regards

4
  • Isn't R.string pointing to strings.xml file? Commented May 30, 2019 at 13:40
  • No, its pointing to the plugin resoures class which is not what I want. I want to access to the app resources Commented May 30, 2019 at 13:40
  • But for me it is pointing to strings.xml. Commented May 30, 2019 at 13:42
  • @CopsOnRoad do you have an example? Commented May 30, 2019 at 14:44

1 Answer 1

5

OK I found a solution. I got this function from the Auth0 Android repo

So basically to get app resources from a plugin they use this function

private static String getResourceFromContext(@NonNull Context context, String resName) {
        final int stringRes = context.getResources().getIdentifier(resName, "string", context.getPackageName());
        if (stringRes == 0) {
            throw new IllegalArgumentException(String.format("The 'R.string.%s' value it's not defined in your project's resources file.", resName));
        }
        return context.getString(stringRes);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.