How to modify the content in the Jar in Java, and modify the content in the Jar in Java

Source: Internet
Author: User

How to modify the content in the Jar in Java, and modify the content in the Jar in Java
I. Summary

I haven't written a blog for a long time. I changed my company before. It means the work is more effective, but it is a pity that there is no time to write articles. During this time, I encountered many problems, but I recorded them and did not take the time to study and solve them. However, this problem has caused me to move on. It must be recorded. To be used by future generations. Enter the topic.


Ii. Prerequisites

1. Learn about GA (google)

2. PairCampaignTrackingReceiverClass. When downloading from GP and installing an app, it sends a broadcast and carries some data in Intent, which is generally a Refer value, here, we can tell where to download it. A is A simple example: application A is an application that I want to publish to GP, but we may promote application A in various channels, therefore, we may need to add channel numbers for statistics. Therefore, we need to add A CampaignTrackingReceiver broadcast receiver in application A, process the Intent content in the received broadcast, and parse the specific channel number, reporting. So it is strange to say that GP sends the broadcast. It sends the broadcast and receives the broadcast after we install A and run it, he will wait for a receiver to accept him.

3. The tools required in this article: Compile.


Iii. Problem Description

The project is connected to GA statistics (an app statistics SDK provided by Google ), however, we may need to calculate the Statistics downloaded by the app from GP (Here we generally register a CampaignTrackingReceiver broadcast), but the problem is that the ga sdk already contains the CampaignTrackingReceiver class, at that time, I entered a misunderstanding: I think that if the app wants to receive the broadcast. The package name of the broadcast receiver must be com. google. analytics. tracking. android. Class Name: CampaignTrackingReceiver, similar to the following registration code:

<receiverandroid:name="com.google.analytics.tracking.android.CampaignTrackingReceiver"android:exported="true"><intent-filter>    <action android:name="com.android.vending.INSTALL_REFERRER" /></intent-filter></receiver>
However, this is not needed. You only need to use the same package name. In fact, you can know the broadcast sending mechanism from Android. The class name does not matter, but it cannot be tested at the time (you need to publish a test app to GP, and the time is not allowed. You can only listen to the predecessors ). Finally, I published a test app to test it. I don't need the same class name. This is also a kind of achievement. If the class name is different, there is no problem here. It won't repeat with the class in GA. However, I used another method to solve this problem without explaining this misunderstanding. Since GA has this broadcast receiving class, we cannot define it. you can insert a piece of code in this broadcast class in its SDK: Send a broadcast and bring the data in the Intent. Now, let's take a look at the specific operations:


Iv. Technical Introduction

The content below is based on the above misunderstanding that has not been explained, and the focus is not a misunderstanding. But how to modify the content in Jar

First, let's talk about the three roles in this process:Jar, dex, smali

Four Tools: dx. bat, dex2jar. bat, baksmali. jar, smali. jar

The diagram is as follows:

Here we need to modify the code in jar,

First of all, there are many methods to modify the code in jar:

1. Open the class file in jar directly with the compressed package tool (unless you are familiar with the instruction set, I am not willing to try it anyway)

2. Use the jd-gui tool to open the jar and modify it. (Although you can understand the code, the problem is that if the code is obfuscated, the difficulty is not as good as the first method, so I didn't try either)

Well, the third method is to modify the smali file. The advantage of this file is that the command is simple, and it does not matter if it is obfuscated. For instructions on smail commands, You can google them on your own. This is simple and I will not explain it here.


So the question is, how can we change jar to smali? There is no direct conversion tool between them, so the method of saving the nation is achieved.

First, set jar ==> dex ==> smali

Then modify the content in smail.

After modification, it will become a jar

Smail ==> dex ==> jar

It is equivalent to dex as a transfer station.


V. Project Demonstration

After the technical implementation instructions, let's take a look at the Demo below:

ReceiverLib Project

1. BtnReceiver. java

package com.example.receiverdemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BtnReceiver extends BroadcastReceiver{private String action = "demo.action.myreceiver";@Overridepublic void onReceive(Context context, Intent intent) {Log.i("demo", "action:"+intent.getAction());}}
Receive the broadcast and print the log.

2. mycycler. java

package com.example.receiverdemo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class MyReceiver extends BroadcastReceiver{private String action = "demo.action.myreceiver";@Overridepublic void onReceive(Context context, Intent intent) {Log.i("demo", "action:"+intent.getAction());}}


3. Utils. java

package com.example.receiverdemo;import android.content.Context;import android.content.Intent;public class Utils {public static void sendBroadcast(Context context,String action){Intent intent = new Intent();intent.setAction(action);context.sendBroadcast(intent);}}
Note: BtnReceiver is a simulated broadcast sent after clicking the Button, which is equivalent to the CampaignTrackingReceiver class to be modified above. MyReceiver is the broadcast receiver we need to add ourselves.

Project download: http://download.csdn.net/detail/jiangwei0910410003/8679113


ReceiverDemo Project (jar exported by ReceiverLib needs to be imported)

package com.example.receiverdemo;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.view.View;import android.view.View.OnClickListener;public class MainActivity extends ActionBarActivity {private String action = "demo.action.btnreceiver";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViewById(R.id.btn).setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v) {Utils.sendBroadcast(MainActivity.this, action);}});}}
Simulate sending a broadcast

Project download: http://download.csdn.net/detail/jiangwei0910410003/8679123

Effect:

After clicking the Button, the broadcast is sent, and BtnReceiver receives the broadcast.


Then, insert the code in BtnReceiver. java and send a MyReceiver

First, use the dx command to convert the jar exported from ReceiverLib to the dex file:

Usage of the dx command: dx -- dex -- output C: \ er. dex receiver. jar

Then convert er. dex to smali:

Use of baksmali. jar: java-jar baksmali-2.0.5.jar-o c: \ classout/c: \ javaser. dex

We can view the smali file, and focus on the BtnReceiver. smali file, because we need to insert code here:

.class public Lcom/example/receiverdemo/BtnReceiver;.super Landroid/content/BroadcastReceiver;.source "BtnReceiver.java"# direct methods.method public constructor <init>()V    .registers 1    .prologue    .line 8    invoke-direct {p0}, Landroid/content/BroadcastReceiver;-><init>()V    return-void.end method# virtual methods.method public onReceive(Landroid/content/Context;Landroid/content/Intent;)V    .registers 7    .param p1, "context"    # Landroid/content/Context;    .param p2, "intent"    # Landroid/content/Intent;    .prologue    .line 12    invoke-virtual {p2}, Landroid/content/Intent;->getAction()Ljava/lang/String;    move-result-object v0    .line 13    .local v0, "action":Ljava/lang/String;    const-string v1, "demo"    new-instance v2, Ljava/lang/StringBuilder;    const-string v3, "action:"    invoke-direct {v2, v3}, Ljava/lang/StringBuilder;-><init>(Ljava/lang/String;)V    invoke-virtual {v2, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;    move-result-object v2    invoke-virtual {v2}, Ljava/lang/StringBuilder;->toString()Ljava/lang/String;    move-result-object v2    invoke-static {v1, v2}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I    const-string v4, "demo.action.myreceiver"    invoke-static {p1, v4}, Lcom/example/receiverdemo/Utils;->sendBroadcast(Landroid/content/Context;Ljava/lang/String;)V    const-string v5, "sendbroadcast"    invoke-static {v1, v5}, Landroid/util/Log;->i(Ljava/lang/String;Ljava/lang/String;)I    .line 14    return-void.end method

Smali command self-search on the Internet, it is very simple, we need to insert a line of code is:

Utils. sendBroadcast method:


If there is no difficulty in this process, I will not explain it.

Below we need to restore to jar:

Use the smali. jar tool to change samli to dex

Usage: java-jar smali-2.0.5.jar c: \ classout/-o c: \ receiver. dex

Run the dex2jar command to convert dex to jar.

Usage: dex2jar receiver. dex

In this case, the modified jar is generated. We replace this jar with the jar in ReceiverDemo, and then run the result:


Successfully displayed. Our MyReceiver receives the broadcast sent from BtnReceiver.


Problem:

Some commands may cause problems in this process:


This is because the class version is incorrect. You need to modify the Java compiler version in Eclipse to compile and export the jar.

I have not encountered any other problems here. If you have any questions during development, please feel free to reply and try to answer them ~~


Vi. Summary

1. The above mentioned problem is the duplicate class in the GA package. I will explain it again, which is a misunderstanding. We can also customize a recycler, the class name does not need to be: CampaignTrackingReceiver. If you use this class, remember not to enter this misunderstanding.

2. There are still many purposes for modifying the content in jar, but it is not a formal solution. This is a bit biased towards cracking. This is not in line with the development principles, it is explained here to provide a solution to the problem and supplement the knowledge of the reverse field. This content is of great use in the reverse field.

3. The usage of this method and all Java programs may be biased towards Android mobile terminals. However, if Java Web encounters such a problem, it can also be solved in this way, it is not limited to Android.






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.