I decompiled .apk using Apktool , classes.dex using dex2jar and opened the jar to view the java code in jd-gui . I need to insert my code at the beginning of a single class method. I use javassist . Created a program in a .java file, trying to compile it

 javac -cp ./javassist.jar Hook.java 

I get the file Hook.class , as a result, in the same folder are:

  • javassist.jar
  • android.jar (from Android SDK)
  • SecretCode.class (from decompiled dex2jar.jar)
  • Received Hook.class

In SecretCode.class there is a getPassword () method to which password data is transmitted, which is needed to activate USB Autoloader. I need to insert a code that will display passwords in the Toast message. It should be located at the very beginning of the getPassword () method. So, here is my Hook.java with code ...

 import javassist.*; import java.util.logging.*; import java.io.IOException; public class Hook { public void addHook(String targetClass, String targetMethod) throws NotFoundException, CannotCompileException, IOException { Logger logger = Logger.getLogger("Javassist"); final String targetFolder = "./modified/"; try { final ClassPool pool = ClassPool.getDefault(); pool.appendClassPath(new LoaderClassPath(getClass().getClassLoader())); pool.appendClassPath("./android.jar"); final CtClass compiledClass = pool.get(targetClass); final CtMethod method = compiledClass.getDeclaredMethod(targetMethod); method.insertBefore("Toast.makeText(getApplicationContext(), $1 + $2, Toast.LENGTH_SHORT).show();"); compiledClass.writeFile(targetFolder); logger.info(targetClass + "." + targetMethod + " has been modified and saved under " + targetFolder); } catch (NotFoundException e) { logger.warning("Failed to find the target class to modify " + targetClass); } } public static void main(String[] args) throws Exception { final String targetClass = "com.android.customsettings.SecredCode"; final String targetMethod = "getPassword"; new Hook().addHook(targetClass, targetMethod); } } 

Now I have to write a command that essentially inserts the code for the Toast popup message at the beginning of the getPassword() method of the getPassword() class:

 java -cp .;javassist.jar;android.jar Hook 

The command does not work, the case is in the insertBefore () method that inserts my code, not such class Toast writes me a command line, I don’t understand what the problem is? Did I really write the wrong code for Toast?

Tell me what's wrong with Hook.java , or maybe I'm doing something wrong? And also - how will this team work? PS I did not SecretCode.class class from the jar archive, I just created it, and copied the code from jd-gui

  • In my opinion, the question to ask is "How to collect dex from java-class" - nick_n_a
  • The jar builds the jar.exe utility. The description on it is quite good. - nick_n_a
  • @nick_n_a, not at all: D I do not need .jar, I stopped at the stage of introducing code from Hook.java into SecretCode.class. The most interesting thing is that the test java (which is in the javassist itself) runs perfectly. what's the matter with my Hook.java not understand. Something is wrong with the addHook () method ... - Flippy
  • Exception what? The string you quoted is output by your own code. This is not enough for diagnosis. - Pavel Mayorov
  • @Pavel Mayorov, and there’s nothing else) Catch is thrown because of an error in try ... - Flippy

0