最新的Xposed模塊編寫教程

前言:最近看了不少的Xposed的入門教程,根據的個人實踐,寫此教程,但願對新手有所幫助。java

1.編寫一個被Hook的App.代碼android

 1 package com.example.hookdemo;  2 
 3 import android.os.Bundle;  4 import android.support.design.widget.FloatingActionButton;  5 import android.support.design.widget.Snackbar;  6 import android.support.v7.app.AppCompatActivity;  7 import android.support.v7.widget.Toolbar;  8 import android.view.View;  9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.widget.TextView; 12 
13 public class MainActivity extends AppCompatActivity { 14 
15  @Override 16     protected void onCreate(Bundle savedInstanceState) { 17         super.onCreate(savedInstanceState); 18  setContentView(R.layout.activity_main); 19         Toolbar toolbar = findViewById(R.id.toolbar); 20  setSupportActionBar(toolbar); 21 
22         final TextView tv = findViewById(R.id.tv_hook); 23 
24         FloatingActionButton fab = findViewById(R.id.fab); 25         fab.setOnClickListener(new View.OnClickListener() { 26  @Override 27             public void onClick(View view) { 28  tv.setText(sRes()); 29  } 30  }); 31  } 32 
33  String sRes() { 34         return "你未被劫持"; 35  } 36 }

運行效果以下。api

2.配置 xposed 文件app

此處咱們用的到是Xposed Android 4.0.3 to Android 4.4 的版本。ide

1) 安裝Xposed 到真機/虛擬機裏 gradle

adb install E:\APK\xposed4.0-4.4.apkui

2)新建一個Android 安卓工程,寫Xposed插件。spa

  在AndroidMainfest.xml 文件中增長以下代碼插件

<meta-data
android:name="xposedmodule"
android:value="true" />
<meta-data
android:name="xposeddescription"
android:value="這是一個Xposed模塊" />
<meta-data
android:name="xposedminversion"
android:value="54" />

 

在app/src/build.gradle 文件下添加如下代碼,  compileOnly 'de.robv.android.xposed:api:82:sources' 可添可不添,其做用是查看文檔。code

compileOnly 'de.robv.android.xposed:api:82'
compileOnly 'de.robv.android.xposed:api:82:sources'
添加完以後點擊一下 sync project with Gradle file(按照Gradle文件同步引用庫),做用是把Xposed "de.robv.android.xposed:api:82","de.robv.android.xposed:api:82:sources"用到的jar包同步project中

 

 

編寫Xposed插件代碼,新建一個java類

java類代碼:

 1 package com.example.myapplication;  2 
 3 
 4 import de.robv.android.xposed.IXposedHookLoadPackage;  5 import de.robv.android.xposed.XC_MethodHook;  6 import de.robv.android.xposed.XposedBridge;  7 import de.robv.android.xposed.XposedHelpers;  8 import de.robv.android.xposed.callbacks.XC_LoadPackage;  9 
10 public class HookDemo  implements IXposedHookLoadPackage { 11 
12 
13  @Override 14     public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable { 15 
16 
17         if (lpparam.packageName.equals("com.example.hookdemo")) 18  { 19             XposedBridge.log(" HOOK Test"); 20 
21             Class clazz=lpparam.classLoader.loadClass("com.example.hookdemo.MainActivity"); 22 
23             XposedHelpers.findAndHookMethod(clazz, "sRes", new XC_MethodHook() { 24  @Override 25                 protected void afterHookedMethod(MethodHookParam param) throws Throwable { 26                     super.afterHookedMethod(param); 27 
28                     param.setResult("你已被劫持"); 29 
30  } 31  }); 32 
33 
34  } 35         
36  } 37 }

 

 在app/src/main 新建Assets Folder 

 

 

 建立xposed_init 文件

 

xposed_init內容以下:

com.example.myapplication.HookDemo 

填寫Xposed的入口類的完整類名。

配置到此結束 。

 

 

3。安裝到真機/虛擬機

相關文章
相關標籤/搜索