Skip to content

1 Getting started

Nobbi edited this page Jul 12, 2017 · 9 revisions

Getting started

Follow these steps to get DebugGhost up and running in your project:

Include the lib in your project

Add lib with jitpack.io

Add jitpack.io as maven repository to your projects build.gradle

...
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
        ...
    }
}
...

Add the following line to your apps build.gradle (v1.1.1 is the tag, you can also use the commit hash)

dependencies {
  ...
  // only compile for debug build-type
  debugCompile 'com.github.sanidgmbh:debugghost:v1.1.1'
  
  // alternatively only for product flavours
  // <productFlavourName>Compile 'com.github.sanidgmbh:debugghost:v1.1.1'
}

Add lib as module in your project

  • Checkout/download DebugGhost.
  • in AndroidStudio: File > New > Import Module...
  • select the location of your DebugGhost download
  • name the module :debugghostlib (or whatever you like, just make sure the name matches in the dependencies section later)
  • add the following line to your apps build.gradle
dependencies {
  ...
  // only compile for debug build-type
  debugCompile project(path: ':debugghostlib')
  
  // alternatively only for product flavours
  // <productFlavourName>Compile project(path: ':debugghostlib')
}

Setup App Environment

Using abstract application class

In order to only compile DebugGhostLib in certain build-types or product-flavours we can create an abstract Application class which will be derived in the special flavours. Put the following class in your main folder (under java > your.app.package):

public class AbstractDebugGhostExampleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        // Do your general application stuff
    }
}

Now, for your release build type (or product flavour), you add the following Application class to your release (or product-flavour) folder (also under java > your.app.package):

public class MyApp extends AbstractDebugGhostExampleApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}

This is the application class that will not reference DebugGhostLib.

Also tell your AndroidManifest.xmlthat you're using your own application class; this will be done in your main folder:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="demo.app.android.sanid.com.debugghostexample" xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- permissions go here -->
    <application android:name=".MyApp"> <!-- register your own application class -->
        <!-- your activities go here -->
    </application>
</manifest>

Now, for your debug build type (or product flavour), you add the following Application class to your debug (or product-flavour) folder (also under java > your.app.package):

public class MyApp extends AbstractDebugGhostExampleApplication {
    private DebugGhostBridge mDebugGhostBridge;

    @Override
    public void onCreate() {
        super.onCreate();

        mDebugGhostBridge = new DebugGhostBridge(this, MyDatabaseHelper.DATABASE_NAME, MyDatabaseHelper.DATABASE_VERSION);
        mDebugGhostBridge.startDebugGhost();
    }
}

In this Application class we reference the DebugGhostBridge and also start it.

using reflection (shorter way)

The shorter way is just using reflection on your application class. You still need your own application class, but you can integrate everything for DebugGhost here without the need of an abstract class and application classes for each flavour.

public class MyApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //... whatever needs to be done in your application class
        loadDebugGhost();
    }

    private void loadDebugGhost() {
        try {
            Class aClass = Class.forName("<full.qualified.package.name>.DebugGhostBridge");
            Constructor constructor = aClass.getConstructor(new Class[]{Context.class, String.class, Integer.TYPE});
            Method method = aClass.getMethod("startDebugGhost");

            Object dgb = constructor.newInstance(context,
                    MyDatabaseHelper.DATABASE_NAME, MyDatabaseHelper.DATABASE_VERSION);
            method.invoke(dgb, new Object[]{});

        } catch (Exception e) {
            Log.i("DebugGhost", "DebugGhost not loaded: " + e.getMessage());
        }
    }
}

The full.qualified.package.name-part is the package where you put your DebugGhostBridge in one (or more) of your flavours. Have a look at the screenshot in the section include DebugGhostBridge

No SQLite Database?

Include DebugGhostBridge

In the debug package (or whatever product-flavour you chose) I usually create a debugghost-package to seperate this part (your.app.package.debugghost) and add the DebugGhostBridge (referenced in our Application class) to it. It looks like this:

public class DebugGhostBridge extends AbstractDebugGhostBridge {
    public DebugGhostBridge(Context context, String databaseName, int databaseVersion) {
        super(context, databaseName, databaseVersion);
        initCommands();
    }

    private void initCommands() {
        // add your custom commands here which are derived from AbstractGhostCommand
        // e.g. 
        // addGhostCommand(new ShowToastCommand(mContext, "Show toast in app", "show_toast", " "));
    }
}

Using GhostCommands

Your project tree now looks something like this:

project tree

Browsing SQLite database and getting device info

Without doing anything more you can start your app and the DebugGhostService (depending on your build-type or product-flavour configuration) will start. In the notification bar the service tells you on which IP and port (default is 8080) it is running. Connect to the device in the same local network via your browser on the development machine.

http://<my-ip>:8080

Next: Configuration >