Langsung ke konten utama

Postingan

Menampilkan postingan dari Januari, 2016

Android Tutorial: Use LeakCanary to detect memory leaks

Overview The memory leak can be a headache to detect and to resolve, small memory leaks can be hidden and may be seen after a long usage of the application and hunting memory leaks is not a simple task. In this tutorial we will create a leaked application and we will use the LeakCanary library to detect the memory leak. Step 1: add the LeakCanary dependency to the application Modify the app/build.gradle to add the LeakCanary dependency as follows: Step 2: Extend and configure the Application class We need to call LeakCanary.install in onCreate method: Step 3: Create a leaked activity For this we will create a singleton class that saves the context: Then, the main activity (leaked one), will use the singleton and then we'll go to a new activity: Then, in the new activity we'll call System.gc to force the garbage collector in order to accelerate the analysis. Step 4: Retrieve the analysis result A nice notification can be shown: The result can be retrieved from logcat: Source c

Android UI/Application Monkey test

Overview The Monkey is a command-line tool that can be ran on any emulator instance or on a device. It sends a pseudo-random stream of user events into the system, which acts as a stress test on the application software. The Monkey includes a number of options, but they break down into four primary categories: Basic configuration options, such as setting the number of events to attempt. Operational constraints, such as restricting the test to a single package. Event types and frequencies. Debugging options. When the Monkey runs, it generates events and sends them to the system. It also watches the system under test and looks for three conditions, which it treats specially: If the Monkey was configures to run in one or more specific packages, it watches for attempts to navigate to any other packages, and blocks them. If the application crashes or receives any sort of unhandled exception, the Monkey will stop and report the error. If the application generates an application not

Dump the Android application heap if an out of memory crash occured

Solution overview The idea is to store the application heap if an out of memory error happens. The HPROF file can be then analyzed using Eclipse Memory Analyzer Tool that I will present later. Step 1: Create a class that implements Thread.UncaughtExceptionHandler The implemented method uncaughtException will check if the exception is out of memory and if it's the case, the HPROF file will be dumped in the device storage. Step 2: Set the uncaught exception handler the thread we want to create the heap dump It's very sample, we can either set the uncaught exception handler to the main thread: Or, to any running thread: Step 3: Create a memory leaked code to invoke an out of memory exception For example, by doing: Step 4: Analyze the HPROF file to fix the memory leak Eclipse Memory Analyzer tool can be used to analyze the dumped heap, I will present the method in a separate article. Source code The Activity code is the following and the sample source code can be found on Github .

FlatBuffers Android Tutorial

FlatBuffers is an efficient cross platform serialization library for C++, Java, C#, Go, Python and JavaScript. It was originally created at Google for game development and other performance-critical applications. FlatBuffers is Open Source (Apache license V2) and available on GitHub . It's currently used by:   Cocos2d-x , the open source mobile game engine and used to serialize the game data. Facebook uses it for client-server communication in the Android app (see the article) . Fun Propulsion Labs at Google in most of libraries and games. Solution overview  The schema will be defind in JSON format, then it will be converted to FlatBuffer format outside the application The Java classes of the Data model will be generated manually using flatc (FlatBuffer compiler) Step 1: Build FlatBuffers Download the source code in Google’s flatbuffers repository .  The build process is described on Google's documentation FlatBuffers Building .  On MacOS for example: Open the xcode proje

How to Perform Rest API using Retrofit in Android (Part-1)

In this post, I will show you How to use Retrofit in Android. Retrofit is a new born baby of web services such as AsyncTask, JSONParsing and Volley. This post is Split into Two Parts. First Part Contains Architecture of Retrofit and How to create MySQL DB and PHP Scripts for Basic Operations. Second Part Contains how to perform Retrofit Operations in Android. Architecture of Retrofit Web Service We need 3 Things for Complete Retrofit Architecture. RestAdapter An Interface with all networking methods and parameters. Getter Setter Class to save data coming from server. Project Structure: Create MySQL DataBase and PHP Scripts. Following image shows my database structure. PHP Scripts: I created db_config.php which contains the script to connect DB. <?php /** * Database config variables */ define("DB_HOST", "localhost"); define("DB_USER", "root"); define("DB_PASSWORD", ""); define("DB_DATABASE", "retrofit_exampl