Android 系統安裝 apk 時解壓 so 的邏輯問題
0X0 前言
在 Android 系統中,當我們安裝apk檔案的時候,lib 目錄下的 so 檔案會被解壓到 app 的原生庫目錄,一般來說是放到 /data/data/<package-name>/lib 目錄下,而根據系統和CPU架構的不同,其拷貝策略也是不一樣的,在我們測試過程中發現不正確地配置了 so 檔案,比如某些 app 使用第三方的 so 時,只配置了其中某一種 CPU 架構的 so,可能會造成 app 在某些機型上的適配問題。所以這篇文章主要介紹一下在不同版本的 Android 系統中,安裝 apk 時,PackageManagerService 選擇解壓 so 庫的策略,並給出一些 so 檔案配置的建議。
0x1 Android4.0以前
當 apk 被安裝時,執行路徑雖然有差別,但最終要呼叫到的一個核心函式是 copyApk,負責拷貝 apk 中的資源。
參考2.3.6的 Android 原始碼,它的 copyApk 其內部函式一段選取原生庫 so 邏輯:
public static int listPackageNativeBinariesLI(ZipFile zipFile, List> nativeFiles) throws ZipException, IOException {
String cpuAbi = Build.CPU_ABI;
int result = listPackageSharedLibsForAbiLI(zipFile, cpuAbi, nativeFiles);
/*
* Some architectures are capable of supporting several CPU ABIs
* for example, ‘armeabi-v7a‘ also supports ‘armeabi‘ native code
* this is indicated by the definition of the ro.product.cpu.abi2
* system property.
*
* only scan the package twice in case of ABI mismatch
*/
if (result == PACKAGE_INSTALL_NATIVE_ABI_MISMATCH) {
final String cpuAbi2 = SystemProperties.get("ro.product.cpu.abi2", null);
if (cpuAbi2 != null) {
result = listPackageSharedLibsForAbiLI(zipFile, cpuAbi2, nativeFiles);
}
if (result == PACKAGE_INSTALL_NATIVE_ABI_MISMATCH) {
Slog.w(TAG, "Native ABI mismatch from package file");
return PackageManager.INSTALL_FAILED_INVALID_APK;
}
if (result == PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES) {
cpuAbi = cpuAbi2;
}
}
/*
* Debuggable packages may have gdbserver embedded, so add it to
* the list to the list of items to be extracted (as lib/gdbserver)
* into the application‘s native library directory later.
*/
if (result == PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES) {
listPackageGdbServerLI(zipFile, cpuAbi, nativeFiles);
}
return PackageManager.INSTALL_SUCCEEDED;
}
這段程式碼中的 Build.CPU_ABI 和 "ro.product.cpu.abi2" 分別為手機支援的主 abi 和次 abi 屬性字串,abi 為手機支援的指令集所代表的字串,比如 armeabi-v7a、armeabi、x86、mips 等,而主 abi 和次 abi 分別表示手機支援的第一指令集和第二指令集。程式碼首先呼叫 listPackageSharedLibsForAbiLI 來遍歷主 abi 目錄。當主 abi 目錄不存在時,才會接著呼叫 listPackageSharedLibsForAbiLI 遍歷次 abi 目錄。
private static int listPackageSharedLibsForAbiLI(ZipFile zipFile, String cpuAbi, List> libEntries) throws IOException, ZipException {
final int cpuAbiLen = cpuAbi.length();
boolean hasNativeLibraries = false;
boolean installedNativeLibraries = false;
if (DEBUG_NATIVE) {
Slog.d(TAG, "Checking " + zipFile.getName() + " for shared libraries of CPU ABI type " + cpuAbi);
}
Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
// skip directories
if (entry.isDirectory()) {
continue;
}
String entryName = entry.getName();
/*
* Check that the entry looks like lib//lib.so
* here, but don‘t check the ABI just yet.
*
* - must be sufficiently long
* - must end with LIB_SUFFIX, i.e. ".so"
* - must start with APK_LIB, i.e. "lib/"
*/
if (entryName.length() < MIN_ENTRY_LENGTH || !entryName.endsWith(LIB_SUFFIX) || !entryName.startsWith(APK_LIB)) {
continue;
}
// file name must start with LIB_PREFIX, i.e. "lib"
int lastSlash = entryName.lastIndexOf(‘/‘);
if (lastSlash < 0 || !entryName.regionMatches(lastSlash + 1, LIB_PREFIX, 0, LIB_PREFIX_LENGTH)) {
continue;
}
hasNativeLibraries = true;
// check the cpuAbi now, between lib/ and /lib.so
if (lastSlash != APK_LIB_LENGTH + cpuAbiLen || !entryName.regionMatches(APK_LIB_LENGTH, cpuAbi, 0, cpuAbiLen))
continue;
/*
* Extract the library file name, ensure it doesn‘t contain
* weird characters. we‘re guaranteed here that it doesn‘t contain
* a directory separator though.
*/
String libFileName = entryName.substring(lastSlash+1);
if (!FileUtils.isFilenameSafe(new File(libFileName))) {
continue;
}
installedNativeLibraries = true;
if (DEBUG_NATIVE) {
Log.d(TAG, "Caching shared lib " + entry.getName());
}
libEntries.add(Pair.create(entry, libFileName));
}
if (!hasNativeLibraries)
return PACKAGE_INSTALL_NATIVE_NO_LIBRARIES;
if (!installedNativeLibraries)
return PACKAGE_INSTALL_NATIVE_ABI_MISMATCH;
return PACKAGE_INSTALL_NATIVE_FOUND_LIBRARIES;
}
listPackageSharedLibsForAbiLI 中判斷當前遍歷的 apk 中檔案的 entry 名是否符合 so 命名的規範且包含相應 abi 字串名。如果符合則規則則將 so 的 entry 名加入 list,如果遍歷失敗或者規則不匹配則返回相應錯誤碼。
拷貝 so 策略:
遍歷 apk 中檔案,當 apk 中 lib 目錄下主 abi 子目錄中有 so 檔案存在時,則全部拷貝主 abi 子目錄下的 so;只有當主 abi 子目錄下沒有 so 檔案的時候即 PACKAGE_INSTALL_NATIVE_ABI_MISMATCH 的情況,才會拷貝次 ABI 子目錄下的 so 檔案。
策略問題:
當 so 放置不當時,安裝 apk 時會導致拷貝不全。比如 apk 的 lib 目錄下存在 armeabi/libx.so , armeabi/liby.so , armeabi-v7a/libx.so 這3個 so 檔案,那麼在主 ABI 為 armeabi-v7a 且系統版本小於4.0的手機上, apk 安裝後,按照拷貝策略,只會拷貝主 abi 目錄下的檔案即 armeabi-v7a/libx.so,當載入 liby.so 時就會報找不到 so 的異常。另外如果主 abi 目錄不存在,這個策略會遍歷2次 apk,效率偏低。
0x2 Android 4.0-Android 4.0.3
參考4.0.3的 Android 原始碼,同理,找到處理 so 拷貝的核心邏輯( native 層):
static install_status_t iterateOverNativeFiles(JNIEnv *env, jstring javaFilePath, jstring javaCpuAbi, jstring javaCpuAbi2, iterFunc callFunc, void* callArg) {
ScopedUtfChars filePath(env, javaFilePath);
ScopedUtfChars cpuAbi(env, javaCpuAbi);
ScopedUtfChars cpuAbi2(env, javaCpuAbi2);
ZipFileRO zipFile;
if (zipFile.open(filePath.c_str()) != NO_ERROR) {
LOGI("Couldn‘t open APK %s\n", filePath.c_str());
return INSTALL_FAILED_INVALID_APK;
}
const int N = zipFile.getNumEntries();
char fileName[PATH_MAX];
for (int i = 0; i < N; i++) {
const ZipEntryRO entry = zipFile.findEntryByIndex(i);
if (entry == NULL) {
continue;
}
// Make sure this entry has a filename.
if (zipFile.getEntryFileName(entry, fileName, sizeof(fileName))) {
continue;
}
// Make sure we‘re in the lib directory of the ZIP.
if (strncmp(fileName, APK_LIB, APK_LIB_LEN)) {
continue;
}
// Make sure the filename is at least to the minimum library name size.
const size_t fileNameLen = strlen(fileName);
static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN;
if (fileNameLen < minLength) {
continue;
}
const char* lastSlash = strrchr(fileName, ‘/‘);
LOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName);
// Check to make sure the CPU ABI of this file is one we support.
const char* cpuAbiOffset = fileName + APK_LIB_LEN;
const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset;
LOGV("Comparing ABIs %s and %s versus %s\n", cpuAbi.c_str(), cpuAbi2.c_str(), cpuAbiOffset);
if (cpuAbi.size() == cpuAbiRegionSize
&& *(cpuAbiOffset + cpuAbi.size()) == ‘/‘
&& !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) {
LOGV("Using ABI %s\n", cpuAbi.c_str());
} else if (cpuAbi2.size() == cpuAbiRegionSize
&& *(cpuAbiOffset + cpuAbi2.size()) == ‘/‘
&& !strncmp(cpuAbiOffset, cpuAbi2.c_str(), cpuAbiRegionSize)) {
LOGV("Using ABI %s\n", cpuAbi2.c_str());
} else {
LOGV("abi didn‘t match anything: %s (end at %zd)\n", cpuAbiOffset, cpuAbiRegionSize);
continue;
}
// If this is a .so file, check to see if we need to copy it.
if ((!strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN)
&& !strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)
&& isFilenameSafe(lastSlash + 1))
|| !strncmp(lastSlash + 1, GDBSERVER, GDBSERVER_LEN)) {
install_status_t ret = callFunc(env, callArg, &zipFile, entry, lastSlash + 1);
if (ret != INSTALL_SUCCEEDED) {
LOGV("Failure for entry %s", lastSlash + 1);
return ret;
}
}
}
return INSTALL_SUCCEEDED;
}
拷貝 so 策略:
遍歷 apk 中所有檔案,如果符合 so 檔案的規則,且為主 ABI 目錄或者次 ABI 目錄下的 so,就解壓拷貝到相應目錄。
策略問題:
存在同名 so覆蓋,比如一個 app 的 armeabi 和 armeabi-v7a 目錄下都包含同名的 so,那麼就會發生覆蓋現象,覆蓋的先後順序根據 so 檔案對應 ZipFileR0 中的 hash 值而定,考慮這樣一個例子,假設一個 apk 同時有 armeabi/libx.so 和 armeabi-v7a/libx.so,安裝到主 ABI 為 armeabi-v7a 的手機上,拷貝 so 時根據遍歷順序,存在一種可能即 armeab-v7a/libx.so 優先遍歷並被拷貝,隨後 armeabi/libx.so 被遍歷拷貝,覆蓋了前者。本來應該載入 armeabi-v7a 目錄下的 so,結果按照這個策略拷貝了 armeabi 目錄下的 so。
apk 中檔案 entry 的雜湊計算函式如下:
unsigned int ZipFileRO::computeHash(const char* str, int len)
{
unsigned int hash = 0;
while (len--)
hash = hash * 31 + *str++;
return hash;
}
/*
* Add a new entry to the hash table.
*/
void ZipFileRO::addToHash(const char* str, int strLen, unsigned int hash)
{
int ent = hash & (mHashTableSize-1);
/*
* We over-allocate the table, so we‘re guaranteed to find an empty slot.
*/
while (mHashTable[ent].name != NULL)
ent = (ent + 1) & (mHashTableSize-1);
mHashTable[ent].name = str;
mHashTable[ent].nameLen = strLen;
}
0x3 Android 4.0.4以後
以4.1.2系統為例,遍歷選擇 so 邏輯如下:
static install_status_t iterateOverNativeFiles(JNIEnv *env, jstring javaFilePath, jstring javaCpuAbi, jstring javaCpuAbi2, iterFunc callFunc, void* callArg) {
ScopedUtfChars filePath(env, javaFilePath);
ScopedUtfChars cpuAbi(env, javaCpuAbi);
ScopedUtfChars cpuAbi2(env, javaCpuAbi2);
ZipFileRO zipFile;
if (zipFile.open(filePath.c_str()) != NO_ERROR) {
ALOGI("Couldn‘t open APK %s\n", filePath.c_str());
return INSTALL_FAILED_INVALID_APK;
}
const int N = zipFile.getNumEntries();
char fileName[PATH_MAX];
bool hasPrimaryAbi = false;
for (int i = 0; i < N; i++) {
const ZipEntryRO entry = zipFile.findEntryByIndex(i);
if (entry == NULL) {
continue;
}
// Make sure this entry has a filename.
if (zipFile.getEntryFileName(entry, fileName, sizeof(fileName))) {
continue;
}
// Make sure we‘re in the lib directory of the ZIP.
if (strncmp(fileName, APK_LIB, APK_LIB_LEN)) {
continue;
}
// Make sure the filename is at least to the minimum library name size.
const size_t fileNameLen = strlen(fileName);
static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN;
if (fileNameLen < minLength) {
continue;
}
const char* lastSlash = strrchr(fileName, ‘/‘);
ALOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName);
// Check to make sure the CPU ABI of this file is one we support.
const char* cpuAbiOffset = fileName + APK_LIB_LEN;
const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset;
ALOGV("Comparing ABIs %s and %s versus %s\n", cpuAbi.c_str(), cpuAbi2.c_str(), cpuAbiOffset);
if (cpuAbi.size() == cpuAbiRegionSize
&& *(cpuAbiOffset + cpuAbi.size()) == ‘/‘
&& !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) {
ALOGV("Using primary ABI %s\n", cpuAbi.c_str());
hasPrimaryAbi = true;
} else if (cpuAbi2.size() == cpuAbiRegionSize
&& *(cpuAbiOffset + cpuAbi2.size()) == ‘/‘
&& !strncmp(cpuAbiOffset, cpuAbi2.c_str(), cpuAbiRegionSize)) {
/*
* If this library matches both the primary and secondary ABIs,
* only use the primary ABI.
*/
if (hasPrimaryAbi) {
ALOGV("Already saw primary ABI, skipping secondary ABI %s\n", cpuAbi2.c_str());
continue;
} else {
ALOGV("Using secondary ABI %s\n", cpuAbi2.c_str());
}
} else {
ALOGV("abi didn‘t match anything: %s (end at %zd)\n", cpuAbiOffset, cpuAbiRegionSize);
continue;
}
// If this is a .so file, check to see if we need to copy it.
if ((!strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN)
&& !strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)
&& isFilenameSafe(lastSlash + 1))
|| !strncmp(lastSlash + 1, GDBSERVER, GDBSERVER_LEN)) {
install_status_t ret = callFunc(env, callArg, &zipFile, entry, lastSlash + 1);
if (ret != INSTALL_SUCCEEDED) {
ALOGV("Failure for entry %s", lastSlash + 1);
return ret;
}
}
}
return INSTALL_SUCCEEDED;
}
拷貝 so 策略:
遍歷 apk 中檔案,當遍歷到有主 Abi 目錄的 so 時,拷貝並設定標記 hasPrimaryAbi 為真,以後遍歷則只拷貝主 Abi 目錄下的 so,當標記為假的時候,如果遍歷的 so 的 entry 名包含次 abi 字串,則拷貝該 so。
策略問題:
經過實際測試, so 放置不當時,安裝 apk 時存在 so 拷貝不全的情況。這個策略想解決的問題是在 4.0 ~ 4.0.3 系統中的 so 隨意覆蓋的問題,即如果有主 abi 目錄的 so 則拷貝,如果主 abi 目錄不存在這個 so 則拷貝次 abi 目錄的 so,但程式碼邏輯是根據 ZipFileR0 的遍歷順序來決定是否拷貝 so,假設存在這樣的 apk, lib 目錄下存在 armeabi/libx.so , armeabi/liby.so , armeabi-v7a/libx.so 這三個 so 檔案,且 hash 的順序為 armeabi-v7a/libx.so 在 armeabi/liby.so 之前,則 apk 安裝的時候 liby.so 根本不會被拷貝,因為按照拷貝策略, armeabi-v7a/libx.so 會優先遍歷到,由於它是主 abi 目錄的 so 檔案,所以標記被設定了,當遍歷到 armeabi/liby.so 時,由於標記被設定為真, liby.so 的拷貝就被忽略了,從而在載入 liby.so 的時候會報異常。
0x4 64位系統支援
Android 在5.0之後支援64位 ABI,以5.1.0系統為例:
public static int copyNativeBinariesWithOverride(Handle handle, File libraryRoot, String abiOverride) {
try {
if (handle.multiArch) {
// Warn if we‘ve set an abiOverride for multi-lib packages..
// By definition, we need to copy both 32 and 64 bit libraries for
// such packages.
if (abiOverride != null && !CLEAR_ABI_OVERRIDE.equals(abiOverride)) {
Slog.w(TAG, "Ignoring abiOverride for multi arch application.");
}
int copyRet = PackageManager.NO_NATIVE_LIBRARIES;
if (Build.SUPPORTED_32_BIT_ABIS.length > 0) {
copyRet = copyNativeBinariesForSupportedAbi(handle, libraryRoot,
Build.SUPPORTED_32_BIT_ABIS, true /* use isa specific subdirs */);
if (copyRet < 0 && copyRet != PackageManager.NO_NATIVE_LIBRARIES &&
copyRet != PackageManager.INSTALL_FAILED_NO_MATCHING_ABIS) {
Slog.w(TAG, "Failure copying 32 bit native libraries; copyRet=" +copyRet);
return copyRet;
}
}
if (Build.SUPPORTED_64_BIT_ABIS.length > 0) {
copyRet = copyNativeBinariesForSupportedAbi(handle, libraryRoot,
Build.SUPPORTED_64_BIT_ABIS, true /* use isa specific subdirs */);
if (copyRet < 0 && copyRet != PackageManager.NO_NATIVE_LIBRARIES &&
copyRet != PackageManager.INSTALL_FAILED_NO_MATCHING_ABIS) {
Slog.w(TAG, "Failure copying 64 bit native libraries; copyRet=" +copyRet);
return copyRet;
}
}
} else {
String cpuAbiOverride = null;
if (CLEAR_ABI_OVERRIDE.equals(abiOverride)) {
cpuAbiOverride = null;
} else if (abiOverride != null) {
cpuAbiOverride = abiOverride;
}
String[] abiList = (cpuAbiOverride != null) ?
new String[] { cpuAbiOverride } : Build.SUPPORTED_ABIS;
if (Build.SUPPORTED_64_BIT_ABIS.length > 0 && cpuAbiOverride == null &&
hasRenderscriptBitcode(handle)) {
abiList = Build.SUPPORTED_32_BIT_ABIS;
}
int copyRet = copyNativeBinariesForSupportedAbi(handle, libraryRoot, abiList,
true /* use isa specific subdirs */);
if (copyRet < 0 && copyRet != PackageManager.NO_NATIVE_LIBRARIES) {
Slog.w(TAG, "Failure copying native libraries [errorCode=" + copyRet + "]");
return copyRet;
}
}
return PackageManager.INSTALL_SUCCEEDED;
} catch (IOException e) {
Slog.e(TAG, "Copying native libraries failed", e);
return PackageManager.INSTALL_FAILED_INTERNAL_ERROR;
}
}
copyNativeBinariesWithOverride 分別處理32位和64位 so 的拷貝,內部函式 copyNativeBinariesForSupportedAbi 首先會根據 abilist 去找對應的 abi。
public static int copyNativeBinariesForSupportedAbi(Handle handle, File libraryRoot, String[] abiList, boolean useIsaSubdir) throws IOException {
createNativeLibrarySubdir(libraryRoot);
/*
* If this is an internal application or our nativeLibraryPath points to
* the app-lib directory, unpack the libraries if necessary.
*/
int abi = findSupportedAbi(handle, abiList);
if (abi >= 0) {
/*
* If we have a matching instruction set, construct a subdir under the native
* library root that corresponds to this instruction set.
*/
final String instructionSet = VMRuntime.getInstructionSet(abiList[abi]);
final File subDir;
if (useIsaSubdir) {
final File isaSubdir = new File(libraryRoot, instructionSet);
createNativeLibrarySubdir(isaSubdir);
subDir = isaSubdir;
} else {
subDir = libraryRoot;
}
int copyRet = copyNativeBinaries(handle, subDir, abiList[abi]);
if (copyRet != PackageManager.INSTALL_SUCCEEDED) {
return copyRet;
}
}
return abi;
}
findSupportedAbi 內部實現是 native 函式,首先遍歷 apk,如果 so 的全路徑中包含 abilist 中的 abi 字串,則記錄該 abi 字串的索引,最終返回所有記錄索引中最靠前的,即排在 abilist 中最前面的索引。
static int findSupportedAbi(JNIEnv *env, jlong apkHandle, jobjectArray supportedAbisArray) {
const int numAbis = env->GetArrayLength(supportedAbisArray);
Vector supportedAbis;
for (int i = 0; i < numAbis; ++i) {
supportedAbis.add(new ScopedUtfChars(env,
(jstring) env->GetObjectArrayElement(supportedAbisArray, i)));
}
ZipFileRO* zipFile = reinterpret_cast(apkHandle);
if (zipFile == NULL) {
return INSTALL_FAILED_INVALID_APK;
}
UniquePtr it(NativeLibrariesIterator::create(zipFile));
if (it.get() == NULL) {
return INSTALL_FAILED_INVALID_APK;
}
ZipEntryRO entry = NULL;
char fileName[PATH_MAX];
int status = NO_NATIVE_LIBRARIES;
while ((entry = it->next()) != NULL) {
// We‘re currently in the lib/ directory of the APK, so it does have some native
// code. We should return INSTALL_FAILED_NO_MATCHING_ABIS if none of the
// libraries match.
if (status == NO_NATIVE_LIBRARIES) {
status = INSTALL_FAILED_NO_MATCHING_ABIS;
}
const char* fileName = it->currentEntry();
const char* lastSlash = it->lastSlash();
// Check to see if this CPU ABI matches what we are looking for.
const char* abiOffset = fileName + APK_LIB_LEN;
const size_t abiSize = lastSlash - abiOffset;
for (int i = 0; i < numAbis; i++) {
const ScopedUtfChars* abi = supportedAbis[i];
if (abi->size() == abiSize && !strncmp(abiOffset, abi->c_str(), abiSize)) {
// The entry that comes in first (i.e. with a lower index) has the higher priority.
if (((i < status) && (status >= 0)) || (status < 0) ) {
status = i;
}
}
}
}
for (int i = 0; i < numAbis; ++i) {
delete supportedAbis[i];
}
return status;
}
舉例說明,在某64位測試手機上的abi屬性顯示如下,它有2個 abilist,分別對應該手機支援的32位和64位 abi 的字串組。
當處理32位 so 拷貝時, findSupportedAbi 索引返回之後,若返回為0,則拷貝 armeabi-v7a 目錄下的 so,如果為1,則拷貝 armeabi 目錄下 so。
拷貝 so 策略:
分別處理32位和64位 abi 目錄的 so 拷貝, abi 由遍歷 apk 結果的所有 so 中符合 abilist 列表的最靠前的序號決定,然後拷貝該 abi 目錄下的 so 檔案。
策略問題:
策略假定每個 abi 目錄下的 so 都放置完全的,這是和2.3.6一樣的處理邏輯,存在遺漏拷貝 so 的可能。
0x5 建議
針對 Android 系統的這些拷貝策略的問題,我們給出了一些配置 so 的建議:
- 1)針對 armeabi 和 armeabi-v7a 兩種 ABI
方法1:由於 armeabi-v7a 指令集相容 armeabi 指令集,所以如果損失一些應用的效能是可以接受的,同時不希望保留庫的兩份拷貝,可以移除 armeabi-v7a 目錄和其下的庫檔案,只保留 armeabi 目錄;比如 apk 使用第三方的 so 只有 armeabi 這一種 abi 時,可以考慮去掉 apk 中 lib 目錄下 armeabi-v7a 目錄。
方法2:在 armeabi 和 armeabi-v7a 目錄下各放入一份 so;
- 2)針對x86
目前市面上的x86機型,為了相容 arm 指令,基本都內建了 libhoudini 模組,即二進位制轉碼支援,該模組負責把 ARM 指令轉換為 X86 指令,所以如果是出於 apk 包大小的考慮,並且可以接受一些效能損失,可以選擇刪掉 x86 庫目錄, x86 下配置的 armeabi 目錄的 so 庫一樣可以正常載入使用;
- 3)針對64位 ABI
如果 app 開發者打算支援64位,那麼64位的 so 要放全,否則可以選擇不單獨編譯64位的 so,全部使用32位的 so,64位機型預設支援32位 so 的載入。比如 apk 使用第三方的 so 只有32位 abi 的 so,可以考慮去掉 apk 中 lib 目錄下的64位 abi 子目錄,保證 apk 安裝後正常使用。
0x6 備註
其實本文是因為在 Android 的 so 載入上遇到很多坑,相信很多朋友都遇到過 UnsatisfiedLinkError 這個錯誤,反應在使用者的機型上也是千差萬別,但是有沒有想過,可能不是 apk 邏輯的問題,而是 Android 系統在安裝 APK 的時候,由於 PackageManager 的問題,並沒有拷貝相應的 SO 呢?可以參考下面第4個連結,作者給出瞭解決方案,就是當出現 UnsatisfiedLinkError 錯誤時,手動拷貝 so 來解決的。
標籤:android class style log com http si it 使用
原文:http://www.cnblogs.com/SA-Jim/p/5430168.html