Clone wiki

android-aspectj-demo / Home

Example tracing

Tracing the all methods invocations is easy! Lets see the sample Activity code with 3 methods.

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        // several method calls for test

    private void foo() {

    private void bar(int x) {

Tracing with disabled METHOD_RETURNS monitoring.

And here METHOD_RETURNS monitoring is enabled.

How to add AspectJ into android application build process

This is small guide how to integrate the AspectJ into your Android application and use it to trace all methods invocations in log.

  1. Download AspectJ and install

  2. Modify build.xml - add aspectj processor task definition and "-post-compile" target like as follows:

<taskdef resource="org/aspectj/tools/ant/taskdefs/">
        <pathelement location="${aspectj.home}/lib/aspectjtools.jar"/>

<target name="-post-compile">
    <property name="aop.output" value="bin/aop"/>
    <echo message="Weaving aspects to .class files before dex converts .class files to .dex file"/>
    <iajc destDir="${aop.output}" Xlintwarnings="true" showWeaveInfo="true" target="1.5"
            <pathelement location="trace.lst"/>
            <pathelement location="${out.classes.absolute.dir}"/>
            <pathelement location="${aspectj.home}/lib/aspectjrt.jar"/>
            <fileset dir="libs" includes="*.jar"/>
            <fileset dir="${sdk.dir}\platforms\${target}\" includes="*.jar"/>
            <fileset dir="${out.classes.absolute.dir}"/>
    <move file="${out.classes.absolute.dir}" todir="bin/classes.old"/>

    <move todir="bin/classes">
        <fileset dir="${aop.output}"/>


Don't forget about aspectj.home property:

  1. Add aspect files to project and trace.lst file to project root.

  2. Now you are able to use ant to build the whole android application with enabled AspectJ post compilation:

ant debug

IntelliJ IDEA settings

If you use IntelliJ IDEA you can also deal with build-in AspectJ support in this IDE. And you will be able to build the project directly from IDE with AspectJ postprocessing. To achieve this you can do next steps:

  1. Enable AspectJ Support plugin in IDEA Settings

  2. Open Settings -> Compiler -> Java Compiler, select Use compiler -> Ajc, select the checkbox "Use Javac to compile modules without AspectJ facet"

  3. Also in this tab you should specify the actual path to aspectjtools.jar in "Path to Ajc compiler" textbox.

  4. Open project settings (F4 on project title). Go to Facets tab and add AspectJ facet to your application main module. No additional settings are necessary there.

  5. Select Build -> Rebuild project. It is necessary to apply changed aspects configuration. You should do this everytime when you have changed something in aspects configuration or/and their code. IDEA has a possible bug and doesn't recognize aspects changes automatically correctly.

  6. Select Run -> Debug.