Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Objects from the Android Studio debugger

Is there a way to export objects from the Android Studio (IntelliJ IDEA) debugger. For example Visual Studio has Object Exporter

In the end I need to create/generate objects initialized with values that are based on server responses. So any other solutions to achieve this end are also welcome.

like image 295
Leo K Avatar asked Mar 11 '15 13:03

Leo K


People also ask

What is USB debugging in Android Studio?

USB Debugging allows an Android device to communicate with a computer that's running the Android SDK in order to use advanced operations.

How do I use USB debugging on Android?

To enable USB debugging, toggle the USB debugging option in the Developer Options menu. You can find this option in one of the following locations, depending on your Android version: Android 9 (API level 28) and higher: Settings > System > Advanced > Developer Options > USB debugging.

What is Android debugger attach?

Attaching the debugger You usually start a debugging session using the Debug button or menu option. However, if you started an app by running it, you can attach a debugger to the running app without needing to restart it. To do this, click Attach Debugger to Android Process.


2 Answers

Here is how you could export a variable that is called output to your clipboard:

  • Add a breakpoint and execute your code in debug mode
  • When you hit your breakpoint, select your object and click 'Evaluate Expression' in the context menu
  • Write an expression that converts your object to a String in the Expression field, for example if your project has access to Gson:

    new GsonBuilder().create().toJson(output)
    
  • In the context menu of the result, select 'Copy Value'

like image 120
Geekarist Avatar answered Oct 19 '22 09:10

Geekarist


This is probably more cumbersome than what you would like, but the following does work:

Create a custom data type renderer as @Donn_Felker mentioned. Assuming that you have a class on the classpath that can serialize an object to JSON (or some other format in which you are interested), use that object in the data type renderer to produce a String containing the serialized data.

In my case, I have a class in my project name GsonProvider, which is essentially a Factory class for Google's GSON. The expression that I use in my data type renderer is: com.example.GsonProvider.getGson().toJson(this) The class name needs to be fully-qualified.

It would be nice if you could use this just when 'rendering' the inspected node, but unfortunately, while you can see the serialzed value there, there doesn't seem to be any way to copy it to the clipboard or export it in any way.

So, add the expression to the 'when expanding the node' section. I was able to make this work by choosing "use list of expressions", and then adding an expression with the name "json", and a value of com.example.GsonProvider.getGson().toJson(this)

I save my custom renderer with the name "json".

With that in place, you can right-click on a variable in either the 'variables' or 'watches' window, and select "View As" --> json, and you will see it as a json string. Right click on it, and choose "copy data". This will copy the json string to your clipboard.

like image 32
GreyBeardedGeek Avatar answered Oct 19 '22 08:10

GreyBeardedGeek