Search code examples
javaandroidsoot

Instrumenting Android apps with Soot using a helper class


I am instrumenting Android applications using a helper class following the example for Java instrumentation in http://www.sable.mcgill.ca/soot/tutorial/profiler2/profiler2.html.

In my BodyTransformer, I have a static block to load MyCounter class

counterClass = Scene.v().loadClassAndSupport("MyCounter");

Since Soot.Main.main(args) that processes my args (in which I provide -android-jars) is not executed while it is loading MyCounter, Soot cannot find my android jar and gives the error:

Caused by: java.lang.RuntimeException: You are analyzing an Android application but did not define android.jar. Options -android-jars or -force-android-jar should be used.
at soot.Scene.defaultClassPath(Scene.java:455)
at soot.Scene.getSootClassPath(Scene.java:224)
at soot.SootResolver.<init>(SootResolver.java:81)
at soot.Singletons.soot_SootResolver(Singletons.java:802)
at soot.SootResolver.v(SootResolver.java:91)
at soot.Scene.loadClass(Scene.java:667)
at soot.Scene.loadClassAndSupport(Scene.java:653)
at MyBodyTransformer.<clinit>(MyBodyTransformer.java:26)
... 1 more

As a solution, I provided my command line arguments (android jars, soot classpath, prepend classpath and process directory) in my main class, before creating my BodyTransformer. Now, it works.

I would like to ask whether there is a more proper way to solve this problem.


Solution

  • loadClassAndSupport is nor sufficicient. Here is what you should do. In your analysis' main method, before you call Soot's main method add the following:

    Scene.v().addBasicClass("MyCounter");

    Then within your analysis simply use Scene.v().getSootClass("MyCounter").