using startMethodTracing and stopMethodTracing with ddms - android

I'd like to measure performance in a specific function (and everything it calls). To do so, it seems I should use Debug.startMethodTracing() and Debug.stopMethodTracing(), then somehow start DDMS tracing.
However, when I open a DDMS view in Eclipse and click the Start Method Profiling button, it starts immediately - not when startMethodTracing() executes. The result is a huge trace file with mostly irrelevant stuff in it. Too hard to narrow in (I tried a few times).
Apparently TraceView is deprecated. Should I hack a way to use that anyway? Is it any better? What is the correct approach to do what I need?
Thanks.

If you use Debug.startMethodTracing() and Debug.stopMethodTracing() a .trace file will be created on the device. The exact location of it is printed out in the logcat.
Look for entries that start with "TRACE STARTED" and "TRACE STOPPED". You then can use adb pull to get the .trace file from your device and open it in the tool of your choice for analysis (e.g. Eclipse or some third party product)
There is no need to manually use DDMS in Eclipse if you're using the above calls. The .trace file itself can simply be opened in Eclipse by using File -> Open

Related

Debug on many phones in Android Studio

Is it possible to debug an app on many phones at the same time in Android Studio? By this I mean launching multiple debug instances, each one on a different phone, like in Eclipse.
At the time of posting this question probably the only solution was the one posted by George V.M. Now, after several updates of Android Studio, this can be easily done by creating N copies of the same Debug configuration and launching each of them on a separate phone.
Update
You might want to take a look at Vlad's answer. This one is pointless for newer versions of Android Studio. In case anyone is still curious, this was my really hacky way of solving the problem
In case anyone out there is still looking for a solution to this, here's what I've found.
I'm working on a project that requires wireless communication between 2 instances of the same app running on 2 phones (actually, 2 or more). There were a lot of times where I wished I could debug 2 devices at a time. It wasn't until recently that I figured out how to do it.
It's actually quite simple: Have 2 instances of Android Studio open and you can debug multiple devices at once, (one device on each Android Studio instance) with breakpoints and everything!
Catch 1: You can't open two instances of the same project
I haven't been able to run two instances of Android Studio where both instances have the same project open. It will just redirect you to the already open project.
Solution:
The solution to this is to just make a copy of your project somewhere on your PC and open that project allowing you to have two copies of the same project open.
Catch 2: Changes have to be made on each copy manually.
Now here's a new problem. What if while debugging, you find a mistake in your code and amend it. Now you have to make sure you make that exact same amendment in your second copy of the project so that the second device doesn't have the same error if it hits those lines. This is annoying, having to remember to make a change twice; once in each copy of the project.
Solution:
My solution to this problem was to just make a 'symbolic link' of the project instead of a physical copy.
(A symbolic link is a 'nickname' or a 'reference' to a file. If you make a symbolic link fileB that points to fileA, although they seem like two distinct files to the OS, they in fact point to the exact same physical file. Any changes made on fileA will be reflected in fileB since they point to the same physical file/data on the disk. Instructions on how to create symbolic links are given below)
Now hold on just one second!!! Making a symbolic link of the entire project might not be a very smart idea since you'll have two Android Studios trying to edit the same files. This could lead to problems, especially in the case of build files and IDE files.
So what I did was make a symbolic link of only the source files, or any file which I'd be editing directly and which AS wouldn't normally touch, and make a physical copy of every other file.
The only thing you need to do is remember to hit Ctrl+S after making changes to your code in one AS instance so that the changes will be reflected on the second one. It might still take a couple of seconds for those changes to be reflected in the second instance but you can just click the "Synchronize" button (top left, next to "Open" and "Save") on your second AS which will cause all externally changed files, i.e., the file you just edited in your first instance, to be reloaded from disk.
You still need to be careful though. Every time you make a change in one AS, make sure you save all those changes and that those changes are reflected in the second AS before you try doing any editing in the second AS. Otherwise conflicts could cause you to lose the changes you made in one copy. One way around this is to force yourself to make changes to a file only on one AS and not the other.
Actual Instructions:
Okay that was a lot of talk. Here are the steps you can follow along with tips:
Close Android Studio and make a copy of your project into another folder on your PC
Go to your second copy and delete all your source code files since we're going to make symbolic links of them.
(these are the files I usually make symbolic links of instead of a copy:
all build.gradle files
the entire app/src folder
if you have any other loose source or resource files or othwerwise non IDE files that you might edit, make symbolic links of them as well
Make a symbolic link of all those non IDE files from the first project folder into your second project folder.
to make a symbolic link of a file in Windows, use
mklink path\to\symbolic\link path\to\original\file
to make symbolic link of a folder in Windows, use
mklink /j "path\to\symbolic\link" "path\to\original\folder"
to make a symbolic link of a file or folder in Linux, use
ln -s "path/to/original/file_or_folder" "path/to/link"
Open up Android Studio again. It will probably open the original copy of your project if that was the last project you opened in AS.
Go to File>Open and open up the second project copy on your PC.
You will now have 2 copies of your project running and you will be able to debug your app on 2 devices at the same time! (Remember that breakpoints won't be shared between the two copies)
If you are talking about attaching the debugger to several phones, I'm going to say no.
The port will be blocked with that traffic from one phone.
Unless someone figure out a hacky way to do this, atm It's not possible as far as my knowledge.
Android studio has its VM devices, but it requires installing Intel Accelerator because it's too slow until it show up. alternatively, I recommended "Genymotion" for running multiple instances and different devices. I'm using it and its working perfectly.
It may takes a while for setup and installing but once its installed it will be light weight and I promise you'll be pleased while working on it =)
Here is the link:
genymotion intallation user guide,
genymotion website

Is there any way to get the memory usage of an Android activity?

OutOfMemory exception occurred frequently in an Android app with more than ten activities. I want to calculate the memory usage for each activity, is there any way?
Use DDMS (DDMS perspective in Eclipse or Device Monitor from Android Studio I think. Select the process on the left and click the "Update Heap" button. Find the memory information on the right. Your process needs to be set to be debuggable for this.
But I don't think this can distinguish between Activities though.
If you need more detailed information you can make DDMS dump an hprof file with your complete heap contents for further analysis. Use these instructions to convert the file format,
hprof-conv <infile> <outfile>
and then use the Eclipse Memory Analyzer to open and inspect it.

Flash Builder embedding files into app like flash IDE?

I'll try to make this simple :
If I create an AIR app from the Flash IDE, I can choose to embed folder in my package. Then I can load the files using 'app:/'+filename. Everything is ok.
I have to move to Flash Builder because I can't test workers in the IDE (thanks Adobe). My issue is that, if I test/debug from Flash Builder, it does a stream error when calling 'app:/'+filename. If I launch the test in the IDE from FB, it works but the Workers don't. I should mention, the reason I'm using this method is that I have so many graphical assets, it's just easier to maintain/update this way instead of using [Embed.. ] for all my items, and it just works in the IDE...
I've added my folder to my sources locations in Flash Builder, still it seems I cannot use the 'app:/' thing.
How can I make this work so I don't change my code and still use 'app:/'? FB is such a confusing program...
edit : I tested again the workers in the IDE build launched by FB (the test in flash IDE icon), I can trace its state with :
worker.start();
worker.addEventListener(Event.WORKER_STATE, this._handleWorkerState);
private function _handleWorkerState(__e:Event):void{
trace(__e.currentTarget.state);
}
traces 'new' then 'running'. But for some reason, it doesn't send or receive any data from any message channel, which, again, works in FB4.7 when i run a debug but doesn't find my files....
Error #2044: Unhandled ioError:. text=Error #2032: Stream Error. URL: app:/foldername..
So basically, I'm looking for a solution to at least one of my problems :)
EDIT :
So ok. Here it is, one issue was due to the wrong debugger version installed (for the workers part). So I can now work and compile in the IDE again. I haven't found an answer to why 'app:/' doesn't work from FB4.7. So that would be the remaining question.
One option since you have Flash IDE is to create a library with all of your images. Drop all your images into the library in Flash and export them for actionscript. Then publish and create a a SWC. Then you can use the swc, which is kind of like a zip file for display objects, in flashbuilder and access them like:
var mc:MovieClip = new imageExportedForAS3_1()
Create a top level folder in your flex project called for example images, copy all of your images into that folder, then every time you need to load an image, just use the source attribute and use the absoulte rute, for example.
<mx:Image source="#Embed(source='../images/pic.png')"
I have never used the app:/ sentence before! Good luck!

Push files from Android to PC over ADB

It's probably not possible due to safety issues and many other reasons, but it's worth a shot, so here goes:
Is it possible to push files from an Android device directly to a computer using ADB?
Why would you want that, you might ask. Good question. I find it useful to view larger Strings on a computer instead of on an Android device, especially since Log.d() won't show Strings of a length more of a couple hundred characters. Things like SOAP requests and responses, other xml files are not easily viewable on my Nexus 7. I've tried some things with the UsbManager class and the UsbDevice class, but I can't seem to find the USB-connection to my computer.
PS. I can think of other methods, like using a logging webservice, for all I care, or writing a script which pulls a certain (log) directory periodically, but I'm just curious whether or not it is possible, it makes my life ever so slightly easier.
As I can read in your question, you are quite aware of the fact that you can pull files from your Android device to your PC, so I won't suggest that.
To answer your question: No, this is not possible. It's not how adb works. Even if you could "push" from Android to PC, you need a piece of software to handle the data. Android does not contain any API which makes that possible, and neither does any part of the Android SDK.
Still you could use any of the methods you already know of (adb pull, Eclipse DDMS View, and yes, even a logging webservice, as you yourself suggested).
Hope this clarifies a bit.
You can push files from ADB to PC (eclipse).
In Eclipse Window-Open Perspective-DDMS
and then in DDMS view select your device from the left side list.
and in the Right side view, you will find a folder called mnt, inside it you will find sd card. There are your files. your devices files. Now to get them out to your pc
There are two buttons on the right side top.
one button says pull a file from device
another button says push a file to device
You need pull the file from device.
Select your respective file and click the pull a file from device button.
To copy a file or directory (and its sub-directories) from the emulator or device, use
adb pull <remote> <local>
For more details on the usage, refer this link
EDIT: What I understand is that you want your app to pull a particular file right? If yes, you need to use
public class YourAppCode {
public static void main(String[] args) throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(new String[] {"/usr/bin/adb", "devices");
}
}
Instead of devices, you need to send pull command along with the source and destination.

Eclipse Debug mode & Exceptions

I'm sure this has been asked before, but I looked and don't see a definitive answer anywhere.
In eclipse, while debugging Android program, if I cause an exception Eclipse always wants to pull up the source file where the actual throw statement is contained. I don't have the source attached (nor do I want to), so it brings up this 'Source not found' window for android.jar every time I hit an exception.
I know I can look at the logcat to see the call stack, but is there any way to make eclipse automatically open the file that I caused the exception from (the deepest file in the call stack for which source exists), and not the android.jar? It seems like a trivial thing to do, but instead I'm always stuck looking at this useless page every time, when I think a smart solution would be to automatically pull up the file from my source code that caused the problem. Instead I have to look at the call stack, return back to 'java' view, and find the offending file myself and open it, which is pretty onerous.
Is there any way to improve my IDE setup so I get more useful information right away than looking at this useless class not found page?
Try using step filters (Editable in properties: Window - Preferences - Java - Debug - Step Filters).
That way you can ignore certain packages where you do not have the sources available.

Categories

Resources