【问题标题】:Execution failed for task ':app:transformClassesWithDexForDebug' Return code 1 for dex process任务 ':app:transformClassesWithDexForDebug' 执行失败 dex 进程返回码 1
【发布时间】:2017-04-10 13:42:27
【问题描述】:

使用Asm注入一个类的所有方法

这是我的代码:

transform api 获取所有类

def cacheFile = new File(file.parent, file.name + ".cache");
fis = new FileInputStream(file)
fos = new FileOutputStream(cacheFile)
println "injectFile: ${file.path}"
byte[] bytes = hackClass(file, null, false, fis);
fos.write(bytes)

if (file.exists()) {
   file.delete()
 }
cacheFile.renameTo(file)

Asm 方法访问者

  @Override
  public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    MethodVisitor mv = null;

    if(name.equals("onCreate") || name.equals("onPause")){
        System.out.println(file.getName() + "Method name : " + name);
        mv = cv.visitMethod(access, name, desc, signature, exceptions);
        return new TraceMethodVisitor(name, mv);
    }

    if (cv != null){
        mv = cv.visitMethod(access, name, desc, signature, exceptions);
    }

    return mv;
}

方法访问者代码:

@Override
public void visitCode() {
    //add start
    mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    mv.visitLdcInsn("========start=========");
    mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

    mv.visitLdcInsn("Hi");
    mv.visitLdcInsn("hello world");
    mv.visitMethodInsn(INVOKEVIRTUAL, "android/util/Log", "v", "(Ljava/lang/String;Ljava/lang/String;)I", false);

    super.visitCode();
}

如果 MethodVisitor 只修改一个方法,成功

修改两个或多个方法,失败,以上代码修改两个方法

以下是控制台的输出:

  :app:transformClassesWithDexForDebug
  Uncaught translation error: com.android.dx.cf.code.SimException:stack: overflow
  Uncaught translation error: com.android.dx.cf.code.SimException:stack: overflow

  2 errors; aborting
  :app:transformClassesWithDexForDebug FAILED

  FAILURE: Build failed with an exception.

  * What went wrong:
  Execution failed for task ':app:transformClassesWithDexForDebug'.

com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: 返回码 1对于 dex 进程

我该如何解决这个问题? :(

【问题讨论】:

  • 在 build.gradle 的默认配置文件中添加 multiDexEnabled true 像这样defaultConfig { multiDexEnabled true } 你可以添加编译文件:compile 'com.android.support:multidex:1.0.1'
  • 无用,可能转换无法使用 multidex,新错误:任务 ':app:transformClassesWithMultidexlistForDebug' 执行失败。 > proguard.KeepClassSpecification.(ZZZZZZLproguard/ClassSpecification;)V

标签: java android assembly transform


【解决方案1】:

创建类 com/bigocto/hacksourcecode/JavaInjectTest

public class JavaInjectTest {

  public static void test1(){
    //Do something what you want
  }
  public void test2(){
    System.out.println("I am test 2");
  }
}

修改 MethodVisitor 代码

 @Override
public void visitCode() {
    //add start

    this.visitMethodInsn(INVOKESTATIC, "com/bigocto/hacksourcecode/JavaInjectTest", "test1", "()V", false);

    super.visitCode();
}

问题解决!!

【讨论】:

    【解决方案2】:

    我遇到了类似的问题并出现类似的错误:

    Execution failed for task ':app:transformClassesWithDexForDebug'.
    > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Return code 1 for dex process
    

    在我的例子中,错误是包含对 Java 1.8 字节码的依赖导致构建失败,因为我的应用程序使用的是 Java 1.7。

    我向库维护者提出了同样的问题,并通过在库的模块级 build.gradle 中明确添加以下行来解决问题:

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-24
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多