原起:
看到流傳一張截圖

知乎鏈接 https://www.zhihu.com/people/simonjun-2
B站驗真視頻(舊版鴻蒙?鴻蒙曾經對安卓進行全域文本替換?圖片真偽驗證!)
https://www.bilibili.com/video/av585643478/?p=1,
驗證:
我試了一下確實有這個東西:andorid 工程分別安裝到安卓和鴻蒙系統


分析
Android 此彈窗邏輯分析:
在 AppWarnings 中 onStartActivity中會根據系統支持的版本進行判斷:
package com.android.server.wm;
public void onStartActivity(ActivityRecord r) {
showUnsupportedCompileSdkDialogIfNeeded(r);
showUnsupportedDisplaySizeDialogIfNeeded(r);
showDeprecatedTargetDialogIfNeeded(r);
}
此彈出主要邏輯
public class DeprecatedTargetSdkVersionDialog {
public DeprecatedTargetSdkVersionDialog(final AppWarnings manager, Context context,
ApplicationInfo appInfo) {
mPackageName = appInfo.packageName;
final CharSequence message = context.
//彈出字串就在這里
getString(R.string.deprecated_target_sdk_message);
final AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setPositiveButton(R.string.ok, (dialog, which) ->
manager.setPackageFlag(
mPackageName, AppWarnings.FLAG_HIDE_DEPRECATED_SDK, true))
.setMessage(message)
.setTitle(label);
// If we might be able to update the app, show a button.
}
其中deprecated_target_sdk_messagen內容就是彈出的字串,
/core/res/res/values-zh-rCN/strings.xml
<string name="deprecated_target_sdk_message" msgid="5203207875657579953">
"此應用專為舊版 Android 打造,因此可能無法正常運行,請嘗試檢查更新或與開發者聯系,"
</string>
來源:
https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values-zh-rCN/strings.xml
Android此彈窗結論
android系統確實有此字串: 此應用專為舊版 Android 打造,因此可能無法正常運行,請嘗試檢查更新或與開發者聯系
鴻蒙彈窗分析
鴻蒙應用安裝包格式
這里有一點需要大家清楚的是,鴻蒙系統之所以會彈出這個彈窗,很重要的一點就是,
直接給鴻蒙系統安裝的.apk檔案,而.apk是安卓安裝包的格式,那鴻蒙系統的默認的安裝包格式默認也是.apk嗎?
答案是no,通過使用DevEco Studio 打包一個安裝包 entry-debug-unsigned.hap,格式確是.hap,解壓縮看一下
解壓縮entry-debug-unsigned.hap檔案,可以看到有dex,吃驚的是還有一個entry_signed_entry.apk檔案

用jadx打開entry_signed_entry.apk看一下,里邊只有ShellMyApplication和MainAbilityShellActivity,我在鴻蒙代碼里還有一個MainAbility并沒有出現在.apk中,那MainAbility跑哪了呢?
entry_signed_entry代碼結構

MainAbility代碼如下:
public class MainAbility extends Ability {
private DirectionalLayout myLayout = new DirectionalLayout(this);
@Override
public void onStart(Intent intent) {
super.onStart(intent);
DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_PARENT);
myLayout.setLayoutConfig(config);
ShapeElement element = new ShapeElement();
element.setRgbColor(new RgbColor(255, 255, 255));
myLayout.setBackground(element);
Text text = new Text(this);
text.setLayoutConfig(config);
text.setText("Hello 鴻蒙");
text.setTextColor(new Color(0xFF000000));
text.setTextSize(50);
text.setTextAlignment(TextAlignment.CENTER);
myLayout.addComponent(text);
super.setUIContent(myLayout);
}
}
在剛才解壓縮entry-debug-unsigned.hap時,除了有entry_signed_entry.apk外,還有一個dex檔案,做過安卓開發應該都知道,安卓虛擬機運行的格式是.dex,java虛擬機運行的是.class檔案,那就來看一下classes.dex,
MainAbility確實在classes.dex里邊,

Hello 鴻蒙 字串在這里
.line 26
.local v2, "text":Lohos/agp/components/Text;
invoke-virtual {v2, v0}, Lohos/agp/components/Text;->setLayoutConfig(Lohos/agp/components/ComponentContainer$LayoutConfig;)V
.line 27
const-string v3, "Hello \u9e3f\u8499" //Hello 鴻蒙
hap里邊有一個.apk但是只有ShellMyApplication和MainAbilityShellActivity類,鴻蒙的其它代碼在.hap 根目錄下的classes.dex中,當然我的這個demo簡單,本質就是一部分代碼發到.hap 根目錄下的classes.dex下,一部分放到entry_signed_entry.apk內,
鴻蒙為什么要這么做呢?
鴻蒙系統支持.apk安裝包,支持.apk是為了能兼容安卓, 但DevEco Studio 生成的安裝包卻是.hap格式的,但是2種安裝包中都有.apk、dex,dex和apk應該是能兼容安卓的關鍵所在.一種可能是如果安裝的是.apk檔案那么直接按加載apk的邏輯來進行,如果是.hap則以.apk為入口,動態加載.hap 根目錄下的classes.dex檔案,當然這只是猜想,現在沒法看到鴻蒙相關原始碼暫時無法分析,
再來說說舊版鴻蒙彈窗字串
現在沒法看到鴻蒙加載安裝包相關代碼,沒法查看該彈窗邏輯,但是這里有一點需要大家清楚的是,鴻蒙系統之所以會彈出這個彈窗,很重要的一點就是,直接給鴻蒙系統安裝的.apk檔案,安卓的apk肯定會安裝安卓的邏輯來判斷,這個邏輯沒錯,至于使用“舊版鴻蒙”是否合適,確實值得商榷,但是 只是簡單的說 鴻蒙 就是 Android.replace(“Android”, “鴻蒙”) 那就把鴻蒙看的太簡單了,
總結
首先分析了一下安卓的彈窗邏輯,本來想再看看鴻蒙的彈窗邏輯,現在看到鴻蒙相關代碼,意外識訓是看了一下鴻蒙的安裝包格式,簡單的猜想了一下鴻蒙兼容安卓的邏輯,其實還有很多的疑問鴻蒙怎么加載安裝包的?鴻蒙用虛擬機了嗎?用的什么虛擬機?鴻蒙兼容安卓的具體是怎么實作的?我想隨著鴻蒙的進一步開源,這些問題終將找到答案,
如果你有更好的見解歡迎給我留言.

參考資料:
應用彈窗“此應用專為舊版Android打造,因此可能無法正常運行…”的原因
https://blog.csdn.net/lwzhang1101/article/details/106075692
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/238092.html
標籤:其他
