不知道你们在设置abi的时候,有没有考虑过这个问题。不一样的abi之间有什么区别。32位so又是如何运行在64位机器上的。这个是如何兼容的呢?java
相信有了解过系统源码的同窗都知道这是啥玩意。首先咱们看下init进程在启动zygote进程是如何作的。以6.0源码为例。android
import /init.environ.rc
import /init.usb.rc
import /init.${ro.hardware}.rc
import /init.${ro.zygote}.rc
import /init.trace.rc
复制代码
在init.rc文件中,会根据ro.zygote的值去启动响应支持相应位数的进程。那么,ro.zygote有几种取值呢?bash
如图所示app
以小米6为例。属性值以下。socket
如今看一下init_zygote64_32.rm文件ui
service zygote /system/bin/app_process64 -Xzygote /system/bin --zygote --start-system-server --socket-name=zygote
class main
socket zygote stream 660 root system
onrestart write /sys/android_power/request_state wake
onrestart write /sys/power/state on
onrestart restart media
onrestart restart netd
service zygote_secondary /system/bin/app_process32 -Xzygote /system/bin --zygote --socket-name=zygote_secondary
class main
socket zygote_secondary stream 660 root system
onrestart restart zygote
复制代码
能够很清楚的看到,系统在启动的时候,会启动两个Zygote,一个64位一个32位。spa
有了解过进程启动的同窗应该看到过以下代码。rest
synchronized(mLock) {
return zygoteSendArgsAndGetResult(openZygoteSocketIfNeeded(abi), argsForZygote);
}
复制代码
private ZygoteState openZygoteSocketIfNeeded(String abi) throws ZygoteStartFailedEx {
Preconditions.checkState(Thread.holdsLock(mLock), "ZygoteProcess lock not held");
if (primaryZygoteState == null || primaryZygoteState.isClosed()) {
try {
primaryZygoteState = ZygoteState.connect(mSocket);
} catch (IOException ioe) {
throw new ZygoteStartFailedEx("Error connecting to primary zygote", ioe);
}
maybeSetApiBlacklistExemptions(primaryZygoteState, false);
maybeSetHiddenApiAccessLogSampleRate(primaryZygoteState);
}
if (primaryZygoteState.matches(abi)) {
return primaryZygoteState;
}
// The primary zygote didn't match. Try the secondary. if (secondaryZygoteState == null || secondaryZygoteState.isClosed()) { try { secondaryZygoteState = ZygoteState.connect(mSecondarySocket); } catch (IOException ioe) { throw new ZygoteStartFailedEx("Error connecting to secondary zygote", ioe); } maybeSetApiBlacklistExemptions(secondaryZygoteState, false); maybeSetHiddenApiAccessLogSampleRate(secondaryZygoteState); } if (secondaryZygoteState.matches(abi)) { return secondaryZygoteState; } throw new ZygoteStartFailedEx("Unsupported zygote ABI: " + abi); } 复制代码
那个,ZygoteState中所存储的abi分类是怎么来的呢?具体细节不说。这里的值是经过读取ro.product.cpu.abilist64,ro.product.cpu.abilist32这两个属性得来的。code
#if defined(__LP64__)
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist64";
static const char ZYGOTE_NICE_NAME[] = "zygote64";
#else
static const char ABI_LIST_PROPERTY[] = "ro.product.cpu.abilist32";
static const char ZYGOTE_NICE_NAME[] = "zygote";
#endif
if (property_get(ABI_LIST_PROPERTY, prop, NULL) == 0) {
LOG_ALWAYS_FATAL("app_process: Unable to determine ABI list from property %s.",
ABI_LIST_PROPERTY);
return 11;
}
String8 abiFlag("--abi-list=");
abiFlag.append(prop);
复制代码
将这些值写入参数中,而后跟着ZygoteInit#main方法传入到java层。具体经过socket查询这个值的过程这里就不说了。cdn
如今,看下小米6手机这几个值是多少。
所以,启动的是64位仍是32位,重点在于启动进程时的参数abi.
仔细想想,这个ABI是怎么来的呢?首先,一个经过Launcher启动的应用进程,确定是不知道应用的ABI的,那么,只能是PMS了,PMS会在开机或者是安装apk的过程当中,分析app的abi,而后就能够在启动的时候,fork相应位数的进程。
在PMS#installPackageLI方法中,调用了derivePackageAbi去分析abi.
这个方法的具体逻辑就很少说了,感兴趣的能够去了解下。