'How can I sandbox a third-party library within my Android application?

I include an AAR in my app which comes from a third party. My app declares lots of permissions, and has access to APIs that could potentially be used to access sensitive info or perform malicious actions. As I understand it, this means that the third-party library can access (and potentially abuse) all these same APIs, even if it does not declare the required permissions in its manifest.
How can I restrict/sandbox third party libraries in my app so they have access to the APIs needed for their stated purposes, and no more?



Solution 1:[1]

You can't. The OS doesn't know what parts of your app are a library and what parts are written by you. When running an app there's no difference between a library class and one of your own. If you don't trust a library, you shouldn't use it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabe Sechan