(4.6.14) Android plug-in basic concept plugging or Swap

Source: Internet
Author: User

Objective:
1, this article selected from the network, only introduce the basic concept of the insert pile
2, to achieve the insertion of piles requires a series of anti-compilation and packaging tools, will be introduced in another article

The concept of the pile is to modify the third-party code in a static way, that is, from the compilation stage, the source code (intermediate code) to compile, and then re-packaging, is static tampering;
The hook does not need to re-compile phase to modify the third-party source or intermediate code, is at runtime through the reflection of the way to modify the call, is a dynamic tampering

The concept of inserting piles:

A plug-in is a code that inserts a piece of our own custom code.

The purpose of inserting piles:

Compile the program into the executable by inserting our custom code into it.
As the program runs, our custom code is executed to achieve the functional requirements that we want to add.

Android Insert Pile Introduction:

In the case of Android, the insertion pile usually refers to the official original ROM of a mobile phone to obtain the intermediate code by the way of anti-compilation, and then through the modification of the intermediate code to add a custom function, to achieve the purpose of the ROM added.

The advantages of this are:

    1. The functionality, features, or optimizations in the original manufacturer's ROM will not be lost.
    2. The adaptation work is very simple, because the official ROM itself is capable of normal operation, as long as the plug to ensure that their own code is inserted correctly.

The disadvantages are:

    1. It takes a lot more effort to implement the function of the middle code insert than to modify the Origin code.
    2. Maintenance is also relatively troublesome, the various models are difficult to reuse, need to make some specific changes.
When to use the insert pile:

Source code development is own a complete set of sources, want to realize the new functional requirements just directly in the source modification.
For example, Xiaomi's own production of mobile phone ROM, is bound to be in a complete set of source code to constantly modify the perfect.
The Patchrom project, which is used by Xiaomi to fit into other manufacturers ' handsets, is modified by inserting piles.

Intermediate code. Smali and source code. Java

For example, we look at Xiaomi Nexus 5 MIUIV6 Frame pack

The file here is the version of the intermediate code that was obtained after the official original ROM of the Nexus 5 was recompiled and added to Miui's own features.
Let's just open up a less complicated file, like Asyncresult.smali, and what you see is the so-called intermediate code.

. ClassPublic Landroid/os/asyncresult;. SuperLjava/lang/object;. SOURCE "Asyncresult.java"# instance fields. FieldPublic exception:ljava/lang/throwable;. FieldPublic Result:ljava/lang/object;. FieldPublic Userobj:ljava/lang/object;# Direct Methods. MethodPublic constructor <init> (ljava/lang/object; Ljava/lang/object; ljava/lang/throwable;) V    . Locals 0    . paramP1,"UO"    # ljava/lang/object;    . paramP2,"R"    # ljava/lang/object;    . paramP3,"Ex"    # ljava/lang/throwable;    . Prologue    . Line  theInvoke-direct {p0}, Ljava/lang/object;-><init> () V    . Line  -Iput-object P1, P0, Landroid/os/asyncresult;->userobj:ljava/lang/object;    . Line  $Iput-object P2, P0, Landroid/os/asyncresult;->result:ljava/lang/object;    . Line  theIput-object P3, P0, Landroid/os/asyncresult;->exception:ljava/lang/throwable;    . Line  theReturn-void. EndMethod. Methodpublic static formessage (landroid/os/message;) Landroid/os/asyncresult;    . Locals 3    . paramP0,"M"    # landroid/os/message;    . Prologueconst/4V2,0x0    . Line  -New-instance V0, Landroid/os/asyncresult;Iget-object v1, P0, landroid/os/message;->obj:ljava/lang/object;Invoke-direct {v0, V1, V2, v2}, Landroid/os/asyncresult;-><init> (ljava/lang/object; Ljava/lang/object; ljava/lang/throwable;) V    . Line  -    . LocalV0,"ret": Landroid/os/asyncresult;Iput-object V0, P0, Landroid/os/message;->obj:ljava/lang/object;    . Line  $Return-object V0. EndMethod. Methodpublic static formessage (landroid/os/message; Ljava/lang/object; ljava/lang/throwable;) Landroid/os/asyncresult;    . Locals 2    . paramP0,"M"    # landroid/os/message;    . paramP1,"R"    # ljava/lang/object;    . paramP2,"Ex"    # ljava/lang/throwable;    . Prologue    . Line  +New-instance V0, Landroid/os/asyncresult;Iget-object v1, P0, landroid/os/message;->obj:ljava/lang/object;Invoke-direct {v0, v1, p1, p2}, Landroid/os/asyncresult;-><init> (ljava/lang/object; Ljava/lang/object; ljava/lang/throwable;) V    . Line  the    . LocalV0,"ret": Landroid/os/asyncresult;Iput-object V0, P0, Landroid/os/message;->obj:ljava/lang/object;    . Line  -Return-object V0. EndMethod

And the source code is more readable than the intermediate code, we open the AOSP project in the same file Source view
Https://github.com/android/platform_frameworks_base/blob/android-4.4_r1.2.0.1/core/java/android/os/AsyncResult.java

/* Copyright (C) 2006 the Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "Licen Se "); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by appli Cable law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without Warranties or CONDITIONS of any KIND, either express OR implied. * See the License for the specific language governing permissions and * limitations under the License. */ PackageAndroid.os;ImportAndroid.os.Message;/** [url=home.php?mod=space&uid=398] @hide [/url] * / Public  class AsyncResult{    /*************************** Instance Variables **************************/    //Expect either exception or result to IS null     PublicObject Userobj; PublicThrowable exception; PublicObject result;/***************************** Class Methods *****************************/    /** Saves and sets M.obj * /     Public StaticAsyncResultFormessage(Message m, Object R, Throwable ex)        {AsyncResult ret; RET =NewAsyncResult (M.obj, R, ex); M.obj = ret;returnRet }/** Saves and sets M.obj * /     Public StaticAsyncResultFormessage(Message m)        {AsyncResult ret; RET =NewAsyncResult (M.obj,NULL,NULL); M.obj = ret;returnRet }/** Please note that this sets M.obj-to is this * /     Public     AsyncResult(Object uo, Object R, Throwable ex)        {userobj = UO;        result = R;    exception = ex; }}

Read here you should have a basic understanding of the difference between the middle code and the source code of the insertion pile.
Patchrom Open Source is not open source code at all, because the anti-compilation of intermediate code to get any of us through the Anti-compilation tool can do, this is only a layman's things

Simple example of inserting piles

Next, we use an example program to demonstrate how to add functionality to a program by inserting piles and modifying the source code.
This sample program is simple and only shows a paragraph on the phone after it is opened:

And we want to achieve is to use the plug-in and the source code to modify the two methods to achieve another line and then display "After more popular science, to improve the level of novice players to avoid being this kind of people pit!" ”
If it is the source code, only need a very simple in the source to add a section of "\ n After more popular science, to improve the level of novice players to avoid this kind of people pit!" ”
The following is the revised source code:

 PackageCom.example.appdemo;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.widget.TextView; Public  class mainactivity extends Activity {    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView Mtextview = (TextView) Findviewbyid (R.id.helloworld); Mtextview.settext ("Moonlight-roms The Moon packing big God is a take someone else code packing earn promotion fee, wanton fabricate all kinds of no common sense words deceive new users cheat!\n After more popular science, upgrade novice play machine level, avoid everyone is this person pit! "); }}

If you are inserting a pile, you want to decompile the program and then

. Line. Local V0, "Mtextview": Landroid/widget/textview; Const-string v1, "moonlight-roms\u6708\u4eae\u6253\u5305\u5927\u795e\u5c31\u662f\u4e2a\u62ff\u522b\u4eba\u4ee3\u7801\u6253\u5305\u8d5a\u63a8\u5e7f\u8d39\uff0c\u8086\u610f\u80e1\u7f16\u4e71\u9020\u5404\u79cd\u6ca1\u6709\u5e38\u8bc6\u7684\u8bdd\u6b3a\u9a97\u65b0\u7528\u6237\u7684\u9a97\u5b50! "

Modified to:

. Line. Local V0, "Mtextview": Landroid/widget/textview; Const-string v1, "moonlight-roms\u6708\u4eae\u6253\u5305\u5927\u795e\u5c31\u662f\u4e2a\u62ff\u522b\u4eba\u4ee3\u7801\u6253\u5305\u8d5a\u63a8\u5e7f\u8d39\uff0c\u8086\u610f\u80e1\u7f16\u4e71\u9020\u5404\u79cd\u6ca1\u6709\u5e38\u8bc6\u7684\u8bdd\u6b3a\u9a97\u65b0\u7528\u6237\u7684\u9a97\u5b50!\ n\u4ee5\u540e\u591a\u53d1\u79d1\u666e\uff0c\u63d0\u5347\u65b0\u624b\u73a9\u673a\u6c34\u5e73\uff0c\u907f\u514d\u5927\u5bb6\u88ab\u8fd9\u79cd\u4eba\u5751\uff01 "

(4.6.14) Android plug-in basic concept plugging or Swap

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.