当前位置:   article > 正文

13.源码阅读(启动一个没有注册的Activity为何会抛出异常-have you declared this activity in your AndroidManifest.xml?--andro...

have you declared this activity in your androidmanifest.xml?

app中每一个activity都要在AndroidManifest文件中配置,否则启动会抛出异常

Unable to find explicit activity class ..; have you declared this activity in your AndroidManifest.xml?

那么我们是否可以启动一个没有注册的activity呢?这就是今天看源码的目的

系统如何检查AndroidManifest中是否注册有一个activity?

在文章03.源码阅读(Activity启动流程--android api 23)中我们已经通过源码知道,启动一个activity调用startActivity后,会进入Instrumentation的方法中

  1. public ActivityResult execStartActivity(
  2. Context who, IBinder contextThread, IBinder token, Activity target,
  3. Intent intent, int requestCode, Bundle options) {
  4. ......
  5. int result = ActivityManagerNative.getDefault()
  6. .startActivity(whoThread, who.getBasePackageName(), intent,
  7. intent.resolveTypeIfNeeded(who.getContentResolver()),
  8. token, target != null ? target.mEmbeddedID : null,
  9. requestCode, 0, null, options);
  10. //通过result值进行一些列的异常抛出,上边的那个就是这里抛出的,
  11. checkStartActivityResult(result, intent);
  12. ......
  13. }

已经知道ActivityManagerNative.getDefault()(api26叫ActivityManager.getService())这行代码已经看过多次,但是这次还是要贴出来说一说,因为很重要,它得到的是IActivityManager这个接口的一个实现类----ActivityMangagerService,最终还是要去看ActivityMangagerService中的startActivity

  1. /**
  2. * Retrieve the system's default/global activity manager.
  3. */
  4. static public IActivityManager getDefault() {
  5. return gDefault.get();
  6. }
  7. private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {
  8. protected IActivityManager create() {
  9. IBinder b = ServiceManager.getService("activity");
  10. if (false) {
  11. Log.v("ActivityManager", "default service binder = " + b);
  12. }
  13. IActivityManager am = asInterface(b);
  14. if (false) {
  15. Log.v("ActivityManager", "default service = " + am);
  16. }
  17. return am;
  18. }
  19. };
  20. public abstract class Singleton<T> {
  21. private T mInstance;
  22. protected abstract T create();
  23. public final T get() {
  24. synchronized (this) {
  25. if (mInstance == null) {
  26. mInstance = create();
  27. }
  28. return mInstance;
  29. }
  30. }
  31. }

IActivityManager这个接口是hook拦截activity创建的关键,这里先放着
进入ActivityManagerService的startActivity中,一路点进入会来到ActivityStackSupervisor类中的startActivityLocked方法

  1. final int startActivityLocked(IApplicationThread caller,
  2. Intent intent, String resolvedType, ActivityInfo aInfo,
  3. IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
  4. IBinder resultTo, String resultWho, int requestCode,
  5. int callingPid, int callingUid, String callingPackage,
  6. int realCallingPid, int realCallingUid, int startFlags, Bundle options,
  7. boolean ignoreTargetSecurity, boolean componentSpecified, ActivityRecord[] outActivity,
  8. ActivityContainer container, TaskRecord inTask) {
  9. //activity启动的返回结果
  10. int err = ActivityManager.START_SUCCESS;
  11. ......
  12. if (err == ActivityManager.START_SUCCESS && aInfo == null) {
  13. // 就是在这个返回值的情况下会抛出activity未注册的异常
  14. err = ActivityManager.START_CLASS_NOT_FOUND;
  15. }
  16. ......
  17. }

看一下什么情况下会返回这个,err == ActivityManager.START_SUCCESS 一上来就设置了,所以关键看aInfo,它什么时候会为null,那么现在要回退过去了,看看aInfo是何时赋值的,找到ActivityStackSupervisor中的方法startActivityMayWait

  1. final int startActivityMayWait(IApplicationThread caller, int callingUid,
  2. String callingPackage, Intent intent, String resolvedType,
  3. IVoiceInteractionSession voiceSession, IVoiceInteractor voiceInteractor,
  4. IBinder resultTo, String resultWho, int requestCode, int startFlags,
  5. ProfilerInfo profilerInfo, WaitResult outResult, Configuration config,
  6. Bundle options, boolean ignoreTargetSecurity, int userId,
  7. IActivityContainer iContainer, TaskRecord inTask) {
  8. ......
  9. ActivityInfo aInfo =
  10. resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
  11. ......
  12. }
  1. ActivityInfo resolveActivity(Intent intent, String resolvedType, int startFlags,
  2. ProfilerInfo profilerInfo, int userId) {
  3. ......
  4. ActivityInfo aInfo;
  5. try {
  6. ResolveInfo rInfo =
  7. AppGlobals.getPackageManager().resolveIntent(
  8. intent, resolvedType,
  9. PackageManager.MATCH_DEFAULT_ONLY
  10. | ActivityManagerService.STOCK_PM_FLAGS, userId);
  11. //ActivityInfo是从ResolveInfo中的activityInfo赋值得到的,所以需要找到ResolveInfo如何获取的
  12. aInfo = rInfo != null ? rInfo.activityInfo : null;
  13. } catch (RemoteException e) {
  14. aInfo = null;
  15. }
  16. ......
  17. }

AppGlobals.getPackageManager()最终得到的是IPackageManager的实现类PackageManagerService,来到这里的resolveIntent方法->chooseBestActivity->findPreferredActivity->getActivityInfo

  1. @Override
  2. public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) {
  3. if (!sUserManager.exists(userId)) return null;
  4. //检查users-permission
  5. enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "get activity info");
  6. synchronized (mPackages) {
  7. PackageParser.Activity a = mActivities.mActivities.get(component);
  8. if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a);
  9. if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) {
  10. PackageSetting ps = mSettings.mPackages.get(component.getPackageName());
  11. if (ps == null) return null;
  12. return PackageParser.generateActivityInfo(a, flags, ps.readUserState(userId),
  13. userId);
  14. }
  15. if (mResolveComponentName.equals(component)) {
  16. return PackageParser.generateActivityInfo(mResolveActivity, flags,
  17. new PackageUserState(), userId);
  18. }
  19. }
  20. return null;
  21. }

进入PackageParser中,可以看到,只要Activity a不为null,ActivityInfo就不会为null,那么到了这里又要回退过去看看Activity什么时候为null的

  1. public static final ActivityInfo generateActivityInfo(Activity a, int flags,
  2. PackageUserState state, int userId) {
  3. if (a == null) return null;
  4. if (!checkUseInstalledOrHidden(flags, state)) {
  5. return null;
  6. }
  7. if (!copyNeeded(flags, a.owner, state, a.metaData, userId)) {
  8. return a.info;
  9. }
  10. // Make shallow copies so we can store the metadata safely
  11. ActivityInfo ai = new ActivityInfo(a.info);
  12. ai.metaData = a.metaData;
  13. ai.applicationInfo = generateApplicationInfo(a.owner, flags, state, userId);
  14. return ai;
  15. }

来到PackageManagerService类中,可以看到activity是从集合中取出来的

  1. @Override
  2. public ActivityInfo getActivityInfo(ComponentName component, int flags, int userId) {
  3. if (!sUserManager.exists(userId)) return null;
  4. enforceCrossUserPermission(Binder.getCallingUid(), userId, false, false, "get activity info");
  5. synchronized (mPackages) {
  6. PackageParser.Activity a = mActivities.mActivities.get(component);
  7. if (DEBUG_PACKAGE_INFO) Log.v(TAG, "getActivityInfo " + component + ": " + a);
  8. if (a != null && mSettings.isEnabledLPr(a.info, flags, userId)) {
  9. PackageSetting ps = mSettings.mPackages.get(component.getPackageName());
  10. if (ps == null) return null;
  11. return PackageParser.generateActivityInfo(a, flags, ps.readUserState(userId),
  12. userId);
  13. }
  14. if (mResolveComponentName.equals(component)) {
  15. return PackageParser.generateActivityInfo(mResolveActivity, flags,
  16. new PackageUserState(), userId);
  17. }
  18. }
  19. return null;
  20. }

PackageParser.Activity a = mActivities.mActivities.get(component);这个集合以ComponentName为key,以这个Activity为value,ComponentName肯定是类的全类名,包名+类名的形式,那么能不能从这个集合中取出这个activity关键就是看这个activity有没有被加入集合了

  1. private final ArrayMap<ComponentName, PackageParser.Activity> mActivities
  2. = new ArrayMap<ComponentName, PackageParser.Activity>();

可以在PackageManagerService类中找到这个方法

  1. public final void addActivity(PackageParser.Activity a, String type) {
  2. final boolean systemApp = a.info.applicationInfo.isSystemApp();
  3. mActivities.put(a.getComponentName(), a);
  4. ......
  5. }

这里可以猜测一下,app启动后(或者app安装时就已经扫描保存了)应该会扫描AndroidManifest文件,然后将activity都加入到这个集合,然后启动一个activity的时候如果从这个集合中找不到这个activity,就抛出异常activity未在AndroidManifest中注册,所以肯定有一个扫描AndroidManifest文件的过程,这个过程我们放到下一篇博客中说,今天看到这里基本上已经达到我们的目的了,总结一下

所有在AndroidManifest中注册的activity会被加入一个集合中,这个集合以这个activity的包名+类型为key值存储这个activity,当调用startActivity启动一个activity的时候,会根据这个activity的全类名去这个集合中查找,如果查找不到就表明这个activity没有在AndroidManifest中注册,抛出异常(这里省略了系统扫描AndroidManifest配置文件的过程,以app启动时,activity已经被加入集合为前提)

再次回到我们最初的问题,我们是否可以启动一个没有注册的activity?通过看源码也了解到了,之所以没有注册的activity启动会被抛出异常,是因为有一个监测的过程,如果能绕过这个监测过程,岂不是就可以启动这个activity了

这里采用偷梁换柱的方式来实现,通过动态代理hook Activity的启动过程,启动activity时,系统通过Intent中传递的Activity的ComponentName去集合中查找,只要查找得到就会躲过检查,那么我们的思路是这样的,当我们要启动一个未注册的activity时,传递一个已经注册的activity的componentname作为傀儡,当监测通过的时候,真正要启动这个activity的时候再将这个未注册的替换过去,达到偷梁换柱的目的

具体实现见下一篇博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/670608
推荐阅读
相关标签
  

闽ICP备14008679号