Skip to content

Commit

Permalink
Merge pull request #95 from Meituan-Dianping/optimize
Browse files Browse the repository at this point in the history
修复自动化补丁在方法参数是数组的bug
  • Loading branch information
mivanzhang authored Apr 26, 2017
2 parents 5695f1a + a4f7e88 commit b61fe41
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 56 deletions.
6 changes: 3 additions & 3 deletions README-zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
apply plugin: 'robust'
compile 'com.meituan.robust:robust:0.3.3'
compile 'com.meituan.robust:robust:0.3.4'
```
2. 在整个项目的build.gradle加入classpath
Expand All @@ -37,8 +37,8 @@
jcenter()
}
dependencies {
classpath 'com.meituan.robust:gradle-plugin:0.3.3'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.3'
classpath 'com.meituan.robust:gradle-plugin:0.3.4'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.4'
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Robust is an Android HotFix solution with high compatibility and high stability.
//please uncomment fellow line before you build a patch
//apply plugin: 'auto-patch-plugin'
apply plugin: 'robust'
compile 'com.meituan.robust:robust:0.3.3'
compile 'com.meituan.robust:robust:0.3.4'
```
2. Add below codes in the outest project's build.gradle file.

Expand All @@ -34,8 +34,8 @@ Robust is an Android HotFix solution with high compatibility and high stability.
jcenter()
}
dependencies {
classpath 'com.meituan.robust:gradle-plugin:0.3.3'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.3'
classpath 'com.meituan.robust:gradle-plugin:0.3.4'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.4'
}
}
```
Expand Down
Binary file modified app/robust/app-release.apk
Binary file not shown.
Binary file modified app/robust/patch.jar
Binary file not shown.
41 changes: 22 additions & 19 deletions app/src/main/java/com/meituan/sample/SecondActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

Expand All @@ -23,20 +26,18 @@

import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

protected String flag = "flagstring";
protected static String name = "zhang";
public int times = 0;
public static String staticStringField = "meituan";
public static int staticIntField = 12311111;
public long longField = 123l;
public Hll hll = new Hll(true);
private People people = new People();
public static State state = new State(new Hll(true));

private ListView listView;
private String[] multiArr = {"列表1", "列表2", "列表3", "列表4"};
private String inlineToString(){
return super.toString();
}
Expand All @@ -46,6 +47,8 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println(inlineToString());
setContentView(R.layout.activity_main2);

listView = (ListView) findViewById(R.id.listview);
//test lambda expression
TextView textView = (TextView) findViewById(R.id.secondtext);
textView.setOnClickListener(v -> {
Expand Down Expand Up @@ -80,6 +83,9 @@ protected void onCreate(Bundle savedInstanceState) {
Bundle bundle=new Bundle();
bundle.putInt("asd",1);
bundle.getFloat("asd");
//test array
BaseAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, multiArr);
listView.setAdapter(adapter);
}
/**
* if you change the return value you will change the show text,in the demo we built a patch to change the text
Expand All @@ -91,11 +97,20 @@ public String getTextInfo(String meituan) {
p.setCates(" AutoPatch");
people.setName(" I am Patch");
ConcreateClass concreateClass = new ConcreateClass();

// return "you make it!! name is " + p.getName() + " \npatch success " + people.getName() ;
getArray(meituan);
//打开这部分注释,查看修复效果
// Arrays.fill(multiArr,"修复后的数据");
// return "修复后:you make it!! name is " + p.getName() + " \npatch success " + people.getName() ;
return "error occur " + concreateClass.getA();
}

@Add
public String[] getArray(String meituan) {
People p = new People();
p.setName("mivazhang");
return new String[]{p.getName(),"meituan"};
}

// another usage of Modify anntation
// @Modify(value = "com.meituan.sample.SecondActivity.onCreate(android.os.Bundle)")
private String getInfo(State stae, Super s, long l) {
Expand Down Expand Up @@ -140,18 +155,6 @@ public static String[] methodWithArrayParameters(String[] flag) {
return flag;
}

public Boolean getBoolean(String flag) {
return false;
}

public String getString(Hll hll) {
return "meituan";
}

public Super getStatus() {
return new Super();
}

@Override
public void onClick(View v) {
Toast.makeText(SecondActivity.this, "from implements onclick ", Toast.LENGTH_SHORT).show();
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />

<ListView
android:layout_width="318dp"
android:layout_height="381dp"
android:id="@+id/listview"
android:layout_gravity="bottom|left"/>

</android.support.design.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class ReflectUtils {
}
StringBuilder signureBuilder = new StringBuilder();
String name;
boolean isArray=false;
for (int index = 1; index < signature.indexOf(")"); index++) {
if (Constants.OBJECT_TYPE == signature.charAt(index) && signature.indexOf(Constants.PACKNAME_END) != -1) {
name = signature.substring(index + 1, signature.indexOf(Constants.PACKNAME_END, index)).replaceAll("/", ".")
Expand All @@ -171,6 +172,10 @@ class ReflectUtils {
signureBuilder.append(name);
}
index = signature.indexOf(";", index);
if(isArray){
signureBuilder.append("[]");
isArray=false;
}
signureBuilder.append(".class,");
}
if (Constants.PRIMITIVE_TYPE.contains(String.valueOf(signature.charAt(index)))) {
Expand All @@ -185,8 +190,16 @@ class ReflectUtils {
case 'D': signureBuilder.append("double"); break;
default: break;
}
if(isArray){
signureBuilder.append("[]");
isArray=false;
}
signureBuilder.append(".class,");
}

if (Constants.ARRAY_TYPE.equals(String.valueOf(signature.charAt(index)))) {
isArray=true;
}
}
if (signureBuilder.toString().length() > 0 && String.valueOf(signureBuilder.charAt(signureBuilder.toString().length() - 1)).equals(","))
signureBuilder.deleteCharAt(signureBuilder.toString().length() - 1);
Expand All @@ -200,7 +213,6 @@ class ReflectUtils {
if (e.signature == null) {
return "{\$_=(\$r)\$proceed(\$\$);}";
}

String signatureBuilder = getParameterClassSignure(e.signature, patchClassName);
stringBuilder.append("{");
if (isStatic) {
Expand Down Expand Up @@ -252,25 +264,6 @@ class ReflectUtils {
}


static int calculateParameterCount(String signature) {
int endIndex = signature.indexOf(")") == -1 ? signature.length() : signature.indexOf(")")
signature = signature.substring(0, endIndex)
int parameterCount = 0;
if (null == signature) {
return 0;
}
for (int i = 0; i < signature.length(); i++) {
//object parameter
if (Constants.OBJECT_TYPE == (signature.charAt(i))) {
i = signature.indexOf(";", i);
parameterCount++;
}
if (Constants.PRIMITIVE_TYPE.contains(String.valueOf(signature.charAt(i)))) {
parameterCount++;
}
}
return parameterCount;
}

private static String getParameterClassString(CtClass[] parameters) {
if (parameters == null || parameters.length < 1) {
Expand Down Expand Up @@ -454,7 +447,7 @@ class ReflectUtils {
if (!m.method.returnType.equals(CtClass.voidType)) {
stringBuilder.append("\$_=(\$r)");
}
if (calculateParameterCount(m.signature) > 0) {
if (m.method.parameterTypes.length > 0) {
stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(this," + Constants.ORIGINCLASS + ",\$\$);");
} else {
stringBuilder.append(getStaticSuperMethodName(m.methodName) + "(this," + Constants.ORIGINCLASS + ");");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import java.util.zip.ZipOutputStream
* AutoPatchTransform generate patch dex
*/
class AutoPatchTransform extends Transform implements Plugin<Project> {
private
static String zipCommand = " zip -r " + Constants.ZIP_FILE_NAME + " com -x \"*/\\.*\" -x \"\\.*\"";
private
static String dex2SmaliCommand;
private
Expand Down Expand Up @@ -150,8 +148,7 @@ class AutoPatchTransform extends Transform implements Plugin<Project> {
}
def zipPatchClassesFile(){
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(Config.robustGenerateDirectory+ Constants.ZIP_FILE_NAME));
zipAllPatchClasses(Config.robustGenerateDirectory+"com","",zipOut);

zipAllPatchClasses(Config.robustGenerateDirectory+Config.patchPackageName.substring(0,Config.patchPackageName.indexOf(".")),"",zipOut);
zipOut.close();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,12 @@ private List<String> getPackNameFromSmaliLine(String line) {
return packageNameList;

}
public static void main(String[] args) {
SmaliUitils smaliUitils=new SmaliUitils();
smaliUitils.getObscuredMethodSignure("invokeReflectConstruct(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;","com.meituan.second");
}
private String getObscuredMethodSignure(final String line, String className) {

private String getObscuredMethodSignure(final String line, String className) {
if (className.endsWith(Constants.PATCH_SUFFIX) && Config.modifiedClassNameList.contains(className.substring(0, className.indexOf(Constants.PATCH_SUFFIX)))) {
className = className.substring(0, className.indexOf(Constants.PATCH_SUFFIX));
}
Expand All @@ -232,9 +236,15 @@ private String getObscuredMethodSignure(final String line, String className) {
String parameter = line.substring(line.indexOf("("), line.indexOf(")") + 1);
int endIndex = line.indexOf(")");
String methodSigure = line.substring(0, endIndex + 1);
//invokeReflectConstruct(Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/Class;)Ljava/lang/Object;
boolean isArray=false;
for (int index = line.indexOf("(") + 1; index < endIndex; index++) {
if (Constants.PACKNAME_START.equals(String.valueOf(methodSigure.charAt(index))) && methodSigure.indexOf(Constants.PACKNAME_END) != -1) {
if (Constants.PACKNAME_START.equals(String.valueOf(methodSigure.charAt(index))) && methodSigure.contains(Constants.PACKNAME_END)) {
methodSignureBuilder.append(methodSigure.substring(index + 1, methodSigure.indexOf(Constants.PACKNAME_END, index)).replaceAll("/", "\\."));
if(isArray){
methodSignureBuilder.append("[]");
isArray=false;
}
index = methodSigure.indexOf(";", index);
methodSignureBuilder.append(",");
}
Expand Down Expand Up @@ -271,9 +281,17 @@ private String getObscuredMethodSignure(final String line, String className) {
default:
break;
}
if(isArray){
methodSignureBuilder.append("[]");
isArray=false;
}
methodSignureBuilder.append(",");
}

if (Constants.ARRAY_TYPE.equals(String.valueOf(methodSigure.charAt(index)))) {
isArray=true;
}

}

List<String> returnTypeList = gePackageNameFromSmaliLine(line.substring(endIndex + 1));
Expand All @@ -283,6 +301,7 @@ private String getObscuredMethodSignure(final String line, String className) {
String obscuredMethodSignure = methodSignureBuilder.toString();
String obscuredMethodName = getObscuredMemberName(className, ReadMapping.getInstance().getMethodSignureWithReturnType(returnTypeList.get(0), obscuredMethodSignure));
obscuredMethodSignure = obscuredMethodName + parameter;
// System.out.println("getObscuredMethodSignure is "+obscuredMethodSignure.substring(0, obscuredMethodSignure.indexOf("(")) + parameter);
return obscuredMethodSignure.substring(0, obscuredMethodSignure.indexOf("(")) + parameter;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public class Constants {
public static Class AddAnnotationClass = null;

public final static String[] LIB_NAME_ARRAY = {"baksmali-2.1.2.jar", "smali-2.1.2.jar", "dx.jar"};
public static final String PACKNAME_START = "L";
public static final String PACKNAME_END = ";";
public final static String PRIMITIVE_TYPE = "ZCBSIJFDV";
public final static String ARRAY_TYPE = "[";
public final static char OBJECT_TYPE = 'L';
public static final String PACKNAME_START = String.valueOf(OBJECT_TYPE);
public static final Boolean OBSCURE = true;
// public static final Boolean OBSCURE = false;
// public static final Boolean isLogging = false;
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ buildscript {
classpath 'com.android.tools.build:gradle:2.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.meituan.robust:gradle-plugin:0.3.3'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.3'
classpath 'com.meituan.robust:gradle-plugin:0.3.4'
classpath 'com.meituan.robust:auto-patch-plugin:0.3.4'
classpath 'me.tatarka:gradle-retrolambda:3.2.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.3'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
VERSION_NAME=0.3.3
VERSION_NAME=0.3.4
GROUP=com.meituan.robust

0 comments on commit b61fe41

Please sign in to comment.