日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | ActivityThread 源码分析 )

發(fā)布時間:2025/6/17 Android 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | ActivityThread 源码分析 ) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、ActivityThread 源碼分析
  • 二、ActivityThread 部分代碼示例



dex 解密時 , 需要將 代理 Application 替換為 真實 Application ; 替換 Application 首先要理解系統(tǒng)如何注冊應用的 Application 的 ;





一、ActivityThread 源碼分析



參考源碼 : /frameworks/base/core/java/android/app/ActivityThread.java


Zygote 進程孵化器 fork 出應用進程后 , 就會執(zhí)行 ActivityThread 中的 main 函數(shù) ;

在 main 函數(shù)中 , 調(diào)用了 Looper.prepareMainLooper() 方法 , 初始化了 Looper , 在 main 方法最后 , 執(zhí)行了 Looper.loop() , 開啟了無限循環(huán)獲取 Message 并執(zhí)行 ;

準備完 Looper 之后 , ActivityThread thread = new ActivityThread() 創(chuàng)建了 ActivityThread 對象 , 并調(diào)用了該對象的 attach 函數(shù) , thread.attach(false) ;


public static void main(String[] args) {Looper.prepareMainLooper();ActivityThread thread = new ActivityThread();thread.attach(false);Looper.loop();}

在 attach 函數(shù)中 , private void attach(boolean system) , 傳入的參數(shù)是 false , 進入第一個分支 if (!system) { ;

在該分支中 , final IActivityManager mgr = ActivityManagerNative.getDefault() , 通過 ActivityManager 拿到了 Binder 對象 ;

通過調(diào)用該 Binder 對象的 attachApplication 方法 , mgr.attachApplication(mAppThread) , 將本 ApplicationThread 對象傳送出去 ;


private void attach(boolean system) {sCurrentActivityThread = this;mSystemThread = system;if (!system) {RuntimeInit.setApplicationObject(mAppThread.asBinder());final IActivityManager mgr = ActivityManagerNative.getDefault();try {mgr.attachApplication(mAppThread);} catch (RemoteException ex) {// Ignore}// Watch for getting close to heap limit.} else {// Don't set application object here -- if the system crashes,// we can't display an alert, we just want to die die die.}}

在 main 函數(shù)中創(chuàng)建 ActivityThread 對象時 , 會自動創(chuàng)建其內(nèi)部的成員屬性 , 包括主線程的 Handler , final H mH = new H() 成員 , H 類型就是 Handler 的子類 , private class H extends Handler ;


利用 Binder 調(diào)用 ActivityThread 的 bindApplication 方法 , public final void bindApplication , 在 bindApplication 方法中 , 接收 ActivityManagerService 發(fā)送來的參數(shù) , 最后發(fā)送一條 Message 給 H mH 對象 , 即 Hander 子類對象 , sendMessage(H.BIND_APPLICATION, data) ;

最終由 H 對象 , private class H extends Handler 類型 , 處理上述邏輯 , 最終調(diào)用 handleBindApplication 方法處理 , private void handleBindApplication(AppBindData data) ;


final H mH = new H();private class H extends Handler {public void handleMessage(Message msg) {switch (msg.what) {case BIND_APPLICATION:Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");AppBindData data = (AppBindData)msg.obj;handleBindApplication(data);Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);break;}}

在 ActivityThread 的 handleBindApplication 方法中就是進行的 Application 準備工作 , 構(gòu)建 Application , 并調(diào)用 Application 中的 onCreate 等生命周期函數(shù) ;

在 handleBindApplication 方法 中 , Application app = data.info.makeApplication(data.restrictedBackupMode, null) , 此處創(chuàng)建 Application ;

應用 Application 的具體創(chuàng)建方法 , 需要查看該 data.info.makeApplication 方法 , makeApplication 是 data.info 對象中的函數(shù) , data.info 對象類型是 android.app.LoadedApk 類型 , data.info 是調(diào)用 getPackageInfoNoCheck 獲取的 , data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo) ;

private void handleBindApplication(AppBindData data) {try {// If the app is being launched for full backup or restore, bring it up in// a restricted environment with the base application class.Application app = data.info.makeApplication(data.restrictedBackupMode, null);mInitialApplication = app;} finally {StrictMode.setThreadPolicy(savedPolicy);}}



二、ActivityThread 部分代碼示例



ActivityThread 部分代碼示例 :

public final class ActivityThread {final ApplicationThread mAppThread = new ApplicationThread();final Looper mLooper = Looper.myLooper();final H mH = new H();private class H extends Handler {public void handleMessage(Message msg) {switch (msg.what) {case BIND_APPLICATION:Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");AppBindData data = (AppBindData)msg.obj;handleBindApplication(data);Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);break;}}private void handleBindApplication(AppBindData data) {try {// If the app is being launched for full backup or restore, bring it up in// a restricted environment with the base application class.Application app = data.info.makeApplication(data.restrictedBackupMode, null);mInitialApplication = app;} finally {StrictMode.setThreadPolicy(savedPolicy);}}public final void bindApplication(String processName, ApplicationInfo appInfo,List<ProviderInfo> providers, ComponentName instrumentationName,ProfilerInfo profilerInfo, Bundle instrumentationArgs,IInstrumentationWatcher instrumentationWatcher,IUiAutomationConnection instrumentationUiConnection, int debugMode,boolean enableOpenGlTrace, boolean isRestrictedBackupMode, boolean persistent,Configuration config, CompatibilityInfo compatInfo, Map<String, IBinder> services,Bundle coreSettings) {sendMessage(H.BIND_APPLICATION, data);} private void attach(boolean system) {sCurrentActivityThread = this;mSystemThread = system;if (!system) {RuntimeInit.setApplicationObject(mAppThread.asBinder());final IActivityManager mgr = ActivityManagerNative.getDefault();try {mgr.attachApplication(mAppThread);} catch (RemoteException ex) {// Ignore}// Watch for getting close to heap limit.} else {// Don't set application object here -- if the system crashes,// we can't display an alert, we just want to die die die.}}public static void main(String[] args) {Looper.prepareMainLooper();ActivityThread thread = new ActivityThread();thread.attach(false);Looper.loop();} }

總結(jié)

以上是生活随笔為你收集整理的【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | ActivityThread 源码分析 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。