5

I have the source code of an apk, containing a few hundred classes. I need to get the list of all calls to Android SDK methods in my source code. I thought about developing a python script to parse all sources, but there seem to be too many rules to define. I fell it would be too complicated.

Does anyone has an idea ? Or are there existing tools that can do it ?

For exemple, if the code looks like this :

class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState.containsKey("toto")) {
            setContentView(R.layout.mylayout);
            this.uselessMethod();
        }
    }
    public int uselessMethod() {
        new Thread();
    }
}

I want to get something like this :

  • java.lang.Thread#<'init'>()
  • android.app.Activity#onCreate(Bundle) void
  • android.app.Activity#setContentView(int) void
  • android.os.Bundle#containsKey(String) boolean

Thanks.

vdechef
  • 257
  • 3
  • 10
  • 1
    you can use javap : http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javap.html – The Dark Knight Apr 29 '13 at 14:32
  • 1
    javap is a disassembler. I don't need to disassemble the application, as I got the source code. I need to list all calls to Android SDK inside my code. – vdechef Apr 29 '13 at 15:14

3 Answers3

2

Look at Doxygen for extract out all of the methods. You'll probably want to use EXTRACT_ALL http://www.doxygen.nl/manual/starting.html#extract_all which will assume everything in your sources should be documented (like third-party methods).

I was working from memory and just tried it over here. I thought Doxygen did it with additional options (see: how to get doxygen to produce call & caller graphs for c functions). While with Graphviz/dot, Doxygen does generate a collaboration diagram, it doesn't look like it will extract out what you are looking for, which I think is to list out all of the method calls, including those that aren't in your source tree.

albert
  • 8,285
  • 3
  • 19
  • 32
Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
  • Doxygen can generate documentation, and list classes and methods defined in my code. But can it extract third-party methods used by my code (as defined in my example above) ? – vdechef Apr 29 '13 at 14:34
  • I just tested Doxygen with EXTRACT_ALL : it does not extract SDK methods that are called from my code. – vdechef Apr 29 '13 at 15:05
1

This one worked for me:

static void printMethods(Class cls)
{
    if(cls!=null){
        System.out.println();
        System.out.println("Printing Methods of "+cls.getName()+":");
        System.out.println();
        for(Method m:cls.getDeclaredMethods()){
            System.out.println(m.toString());
        }
    }
}

How to call it:

    printMethods(Class_Goes_Here);

Example:

    printMethods(java.lang.Boolean.class);

Result:

Printing Methods of java.lang.Boolean:

public static int java.lang.Boolean.compare(boolean,boolean)
public static boolean java.lang.Boolean.getBoolean(java.lang.String)
public static int java.lang.Boolean.hashCode(boolean)
public static boolean java.lang.Boolean.logicalAnd(boolean,boolean)
public static boolean java.lang.Boolean.logicalOr(boolean,boolean)
public static boolean java.lang.Boolean.logicalXor(boolean,boolean)
public static boolean java.lang.Boolean.parseBoolean(java.lang.String)
public static java.lang.String java.lang.Boolean.toString(boolean)
public static java.lang.Boolean java.lang.Boolean.valueOf(java.lang.String)
public static java.lang.Boolean java.lang.Boolean.valueOf(boolean)
public boolean java.lang.Boolean.booleanValue()
public int java.lang.Boolean.compareTo(java.lang.Boolean)
public int java.lang.Boolean.compareTo(java.lang.Object)
public boolean java.lang.Boolean.equals(java.lang.Object)
public int java.lang.Boolean.hashCode()
public java.lang.String java.lang.Boolean.toString()

Lemme know if you have any further Questions or Doubts

— DiLDoST

DiLDoST
  • 335
  • 3
  • 12
0

After 7months of my last answer on this question:

When i Carefully read your Question again, i have found that what you need is the Stacktrace. I created a way for you to do this so easily, a simple class.

The StacktraceUtils Class:

public static class StackTraceUtils
{
    public static ArrayList<StackTraceElement> getStackTrace(Thread thread)
    {
        ArrayList<StackTraceElement> l=new ArrayList<>();

        Thread k=thread;
        {
            StackTraceElement[] v=k.getStackTrace();

            for (int i=(v.length - 1);i >= 0;i--)
            {
                StackTraceElement ste=v[i];
                if (!(StackTraceUtils.class.getName().equals(ste.getClassName())))
                {
                    l.add(0, ste);
                }
                else
                {
                    break;
                }
            }
        }
        return l;
    }

    public static String getStackTraceString(Thread thread)
    {
        String p="";
        Thread k=thread;
        p += ((k.getName().equals("main") ?"[MAIN]": k.getName()) + " [" + k.getId() + "]" + (Thread.currentThread().getId() == k.getId() ?" [CURRENT]": "") + " : ");
        for (StackTraceElement ste:StackTraceUtils.getStackTrace(k))
        {
            p += "\n\t->\t" + ste;
        }
        return p.toString();
    }
}

You can print the Stacktrace ljke this:

System.out.println(StackTraceUtils.getStackTraceString(Thread.currentThread()));

Result:

Thread-21 [10892] [CURRENT] : 
    ->  Main.main(Main.java:59)
    ->  Main.main(Main.java:17)
    ->  java.lang.reflect.Method.invoke(Native Method)
    ->  java.lang.Thread.run(Thread.java:764)
    ...

Don't forget to upvote me Please, i barely need votes to help others more. And also don't forget to make as answered if it is... Thanks

— DiLDoST

DiLDoST
  • 335
  • 3
  • 12