Cydia for Android, cydiaforandroid

Source: Internet
Author: User
Tags getcolor

Cydia for Android, cydiaforandroid

Prerequisites for using the Cydia framework on Android devices

1. The Android device must be ROOT.  
2. Install substrate.apk on the Android device  

 

Prerequisites for compiling the Java-layer cydia plug-in

1. Build an Android Development Environment
2. Download The substrate-api.jar and import it to the Android Project

 

1. Declare a metadata under the application node and set the name field to com. saurik. substrate. the main and value fields are set to the full name (including the package name and class name) of the classes used to hook others' APIs)

1     <application android:label="@string/app_name">2         <meta-data android:name="com.saurik.substrate.main" android:value=".Main" />3     </application>

 

2XX indicates the substrate.apk user permission.

1 <uses-permission android:name="cydia.permission.SUBSTRATE" />

 

3. Create a class and an initialize function with no static parameters and no returned values

public static void initialize()

 

4. Implement the initialize function, set the classes to be hooked, and hook the corresponding APIs after the classes are loaded.

 1       MS.hookClassLoad("com.example.usertest.MainActivity", new ClassLoadHook() { 2          3         @Override 4         public void classLoaded(Class<?> arg0) { 5             Method toast; 6             try { 7                 toast=arg0.getMethod("toast", String.class);                 8             } catch (NoSuchMethodException e) { 9                 // TODO Auto-generated catch block10                 e.printStackTrace();11                 return ;12             }13             MS.hookMethod(arg0, toast, new MS.MethodAlteration() {14 15                 @Override16                 public Object invoked(Object arg0, Object... arg1)17                         throws Throwable {18                     19                     invoke(arg0, "hook before");20                     invoke(arg0, arg1);21                     invoke(arg0, arg1);22                     invoke(arg0, "hook end");23                     return null;24                 }25             });26         }27     });
Com. example. usertest. MainActivity is the package name + Class Name of the class to be hooked.
The ClassLoadHook callback interface is used to HOOK
To complete the HOOK operation, follow these steps:
1. Use the Java reflection technology to obtain a method of the classes to be hooked.
2. Call the MS. hookMethod method to hook the specified class, method, and hook interface.
3. Implement the specific hook interface and call the specified method of the specified class using the invoke method. The arg1 parameter array is the original parameter list of the original API, such as the toast method, the original parameter is "toast start", and arg1 [0] is "toast start ".
The MS. MethodAlteration interface is extended to the MS. MethodPointer class and the MethodHook interface. You do not need to create a MethodPointer object and use it to call the original API. The method to call the original API is to directly call the invoke method.
We recommend that you use the MethodAlteration interface instead of the MethodPointer class + MethodHook interface, because it is easier to use and reduces errors caused by the use of MethodPointer objects.

5. HOOK class source code
1     @Override2     protected void onCreate(Bundle savedInstanceState) {3         super.onCreate(savedInstanceState);4         setContentView(R.layout.activity_main);5         toast("toast start"); 6     }7     public void toast(String str){8         Toast.makeText(this, str, Toast.LENGTH_LONG).show();9     }

Complete source code is attached.

Cydia plug-in source code:

Main. java
1 package com. example. cydiaexample; 2 3 import java. lang. reflect. field; 4 import java. lang. reflect. method; 5 import java. lang. reflect. type; 6 7 import android. util. log; 8 import android. widget. toast; 9 10 import com. saurik. substrate. MS; 11 import com. saurik. substrate. MS. classLoadHook; 12 13 // hook method 14 15 public class Main {16 17 public static void initialize () {18 19 20 // set the class to be hooked for 21 MS. hookClassLo Ad ("android. content. res. Resources", new MS. ClassLoadHook () {22 public void classLoaded (Class <?> Resources) {23 //... code to modify the class when loaded24 25 // define Method 26 Method getColor; 27 28 try {29 // hook Method for obtaining color 30 getColor = resources. getMethod ("getColor", Integer. TYPE); 31} catch (NoSuchMethodException e) {32 33 getColor = null; 34} 35 36 if (getColor! = Null) {37 // create a MethodPointer object. In the hookMethod, 38 final MS is used. methodPointer old = new MS. methodPointer (); 39 40 // start the hook method and write the data you want to change to 41 MS. hookMethod (resources, getColor, new MS. methodHook <Object, Object> () {42 public Object invoked (Object resources, Object... args) 43 throws Throwable44 {45 46 int color = (Integer) old. invoke (resources, args); 47 48 // return color 49 // return color &~ 0x0000ff00 | 0x00ff0000; 50 return color; 51} 52}, old); 53 54} 55} 56}); 57 58 59 MS. hookClassLoad ("com. example. usertest. mainActivity ", new ClassLoadHook () {60 61 @ Override62 public void classLoaded (Class <?> Arg0) {63 Method toast; 64 try {65 toast = arg0.getMethod ("toast", String. class); 66} catch (NoSuchMethodException e) {67 // TODO Auto-generated catch block68 e. printStackTrace (); 69 return; 70} 71 MS. hookMethod (arg0, toast, new MS. methodAlteration () {72 73 @ Override74 public Object invoked (Object arg0, Object... arg1) 75 throws Throwable {76 77 invoke (arg0, "hook before"); 78 invoke (arg0, arg1); 79 invoke (arg0, arg1); 80 invoke (arg0, "hook end"); 81 82 return null; 83} 84}); 85} 86}); 87 88} 89}
                                                     Manifest.xml
1 <?xml version="1.0" encoding="utf-8"?>2 <manifest android:versionCode="1" android:versionName="1.0" package="com.example.cydiaexample"3 xmlns:android="http://schemas.android.com/apk/res/android">4 <application android:label="@string/app_name">5 <meta-data android:name="com.saurik.substrate.main" android:value=".Main" />6 </application>7 8 <uses-permission android:name="cydia.permission.SUBSTRATE" />9 </manifest>

Source code for HOOK:

                                                                                      MainActivity.java 
1 package com.example.usertest; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Menu; 6 import android.view.MenuItem; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 public class MainActivity extends Activity {11 12 @Override13 protected void onCreate(Bundle savedInstanceState) {14 super.onCreate(savedInstanceState);15 setContentView(R.layout.activity_main);16 TextView mTextView=(TextView) findViewById(R.id.info);17 mTextView.setText(android.os.Build.ID);18 toast("toast start"); 19 }20 public void toast(String str){21 Toast.makeText(this, str, Toast.LENGTH_LONG).show();22 }23 @Override24 public boolean onCreateOptionsMenu(Menu menu) {25 // Inflate the menu; this adds items to the action bar if it is present.26 getMenuInflater().inflate(R.menu.main, menu);27 return true;28 }29 30 @Override31 public boolean onOptionsItemSelected(MenuItem item) {32 // Handle action bar item clicks here. The action bar will33 // automatically handle clicks on the Home/Up button, so long34 // as you specify a parent activity in AndroidManifest.xml.35 int id = item.getItemId();36 if (id == R.id.action_settings) {37 return true;38 }39 return super.onOptionsItemSelected(item);40 }41 }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.