1.Androi P中Api限制
在Android P上,google对部分系统Api进行了分类,对部分api标记为 灰色,深灰和黑色,
这些被标记的api原则上是不能使用的,尤其是针对反射调用的,会根据api被标记的颜色发出警告或者抛出异常
2.反射调用系统api的流程
Class getDeclaredMethod的时序图如下:
image.png
shouldDenyAccessToMember这个函数是处理hiddenapi的主要逻辑,如果hiddenapi可以使用,会返回false,如果不能使用,会返回true重点看一下ShouldDenyAccessToMember这个函数
template
inline bool ShouldDenyAccessToMember(T* member,
const std::function& fn_get_access_context,
AccessMethod access_method)
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(member != nullptr);
// Get the runtime flags encoded in member's access flags.
// Note: this works for proxy methods because they inherit access flags from their
// respective interface methods.
const uint32_t runtime_flags = GetRuntimeFlags(member);
// Exit early if member is public API. This flag is also set for non-boot class
// path fields/methods.
if ((runtime_flags & kAccPublicApi) != 0) {
return false;
}
// Determine which domain the caller and callee belong to.
// This can be *very* expensive. This is why ShouldDenyAccessToMember
// should not be called on every individual access.
const AccessContext caller_context = fn_get_access_context();
const AccessContext callee_context(member->GetDeclaringClass());
// Non-boot classpath callers should have exited early.
DCHECK(!callee_context.IsApplicationDomain());
// Check if the caller is always allowed to access members in the callee context.
if (caller_context.CanAlwaysAccess(callee_context)) {
return false;<