0、背景
自从Android 9.0后,Android就已经开始着手阻止app开发调用非sdk的api,也就是被标记为@hide的变量、函数、类不可以通过反射调用,否则会提示NoSuchMethod异常
1、对atlas影响
atlas有许多调用都是通过反射系统api来完成的,其中不乏被标记为@hide的类,比如用来处理插件资源的android.content.res.AssetManager#addAssetPath,还有用来处理动态部署的启动新activity的android.app.ActivityThread
/**
* Add an additional set of assets to the asset manager. This can be
* either a directory or ZIP file. Not for use by applications. Returns
* the cookie of the added asset, or 0 on failure.
* {@hide} WARM,该方法被标记为隐藏
*/
public final int addAssetPath(String path) {
synchronized (this) {
int res = addAssetPathNative(path);
makeStringBlocks(mStringBlocks);
return res;
}
}
/**
* This manages the execution of the main thread in an
* application process, scheduling and executing activities,
* broadcasts, and other operations on it as the activity
* manager requests.
*
* {@hide} 高能,整个类都标记为隐藏
*/
public final class ActivityThread {
如果android.content.res.AssetManager#addAssetPath不能用,资源在插件中就无法使用,如果android.app.ActivityThread整个类不让反射,那动态部署中无法添加新的activity、service
2、android p 处理非sdk调用
但情况并没有想象的糟糕,android p 阻止调用非sdk并不是绝对的,并不是标记了hide就一定会抛异常。还有个X因素——light-greylist,如果被hide的api在里面列出,就允许被反射调用。该列表如下:
https://github.com/aosp-mirror/platform_frameworks_base/blob/master/config/hiddenapi-p-light-greylist.txt
https://github.com/aosp-mirror/platform_frameworks_base/blob/master/config/hiddenapi-light-greylist.txt
乍一看addAssetPath和ActivityThread都在里面,看来atlas还是有希望的,不过atlas用的反射有许多,让我们在Android p模拟器上运行下atlas试一试
3、死而后生,atlas的适配
现在官网默认的master分支上的demo还没法运行,那是因为demo里反射调用了hide api且light-greylist没生命的api——android.app.ActivityThread#performRestartActivity,所以现在要运行demo请clone分支android_p
git clone https://github.com/alibaba/atlas.git -b android_p
运行后发现没有了之前的异常,但会有如下提示
05-27 04:37:31.077 2972-2972/com.taobao.demo W/com.taobao.dem: Accessing hidden field Landroid/app/ApplicationPackageManager;->mPM:Landroid/content/pm/IPackageManager; (light greylist, reflection)
05-27 04:37:31.077 2972-2972/com.taobao.demo W/com.taobao.dem: Accessing hidden method Landroid/content/pm/ParceledListSlice;-><init>(Ljava/util/List;)V (light greylist, linking)
05-27 04:37:31.115 2972-2972/com.taobao.demo W/com.taobao.dem: Accessing hidden method Landroid/content/pm/PackageParser;-><init