Mar 2, 2015

Android Libraries & Tools you should give a try


When we started working on our new internal project, QAwear, we had to choose some libraries to work with. QAwear is an Android app that lets you build your projects via Jenkins and Sonar and review the results - all within the app. Moreover, you can connect your smartwatch with the app. You will get notified about results and you can easily build a project: simply choose one and shake the watch ("shake to build").
We compared different libraries and these are the ones we liked most:

Retrofit
Retrofit is a type-safe REST client library for Android and Java applications, which “turns your REST Api into a Java interface”.  The request method, relative URL, parameters, headers, form encodes and multipart can be added with annotations.

For deserialization, Retrofit uses GSON by default for mapping the JSON responses to POJOs, but XML converters, Protocol Buffers or custom parsers can be configured, as well custom error handlers.

/** 
  * Common interface defintion to query Sonar and obtain data via REST. 
  */ 
public interface SonarApiServiceI { 

    /** 
      * Get a list of all projects asynchron. 

      * @param cb retrofit callback function 
      */ 
    @ GET ( "/api/resources" ) 
    void getProjectsCb ( retrofit . Callback < List < SonarProject >> cb ) ; 

    /** 
      * Get a selected sonar project with passed metrics and violations. 

      * @param project the sonar project's id 
      * @param metrics the mectrics as comma separated String 
      * @param alerts boolean, if true, violations will be returned too 
      * @return a list with one SonarProject object 
      */ 
    @GET("/api/resources") 
    List<SonarProject> getProjectMetrics (@Query("resource") int project, 
    @Query("metrics") String metrics, @Query("includealerts") Boolean alerts); 

    //... 

//generates an implementation of the SonarApiService interface. 
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(host)
.setRequestInterceptor(new BasicAuthInterceptor(user, token)) //adding basic authentication header 
.setConverter(new GsonConverter(gson)) //setting custom JSON converter 
.setErrorHandler(new NetworkingErrorHandler(this.context)) //using custom Error handler 
.build(); 

service = adapter.create(SonarApiServiceI.class);
Other popular networking libraries are VolleyAndroid Async HTTP and Ion.


Dagger

Dagger is a dependency injection framework, which checks dependencies already at compile time and generates code to avoid reflection. This approach increases performance significantly for Android applications, because reflection is very slow in the Dalvik VM. View Demo.


Butterknife is a small library, which aims to help you write clearer, easier to understand, and less redundant code. The provided annotations turn time consuming and redundant bindings of views, onClick-Listener and other run-of-the-mill task into a pleasant one-liner. During the compilation of the application, the code for view look-ups gets generated instead of using reflection, so it doesn’t influence your app’s performance.

public class ExampleActivity extends Activity { 

    @InjectView(R.id.text_message) 
    TextView textMessage; //"injecting" TextView

    @InjectViews({R.id.user_name, R.id.password}) 
    List<EditText> credentialFields; //grouping views

    @Override public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.example_activity); 

        ButterKnife.inject(this); 

        //act on all views in the list at once
        ButterKnife.apply(credentialFields, ACTION);
    } 


    //OnClickListener 
    @OnClick(R.id.submit) void submit() { 
        //action... 


}
EventBus is a tiny and very fast Android optimized EventBus, which simplifies the communication between different components of the app.

//Defining an event
public class MyEvent () {…}

//Register subscriber
EventBus.getDefault().register(this);

//Post an event
EventBus.getDefault().post(new MyEvent());

//Receive an event – based on conventions
public void onEvent (MyEvent event) {…};

//Unregistering
EventBus.getDefault().unregister(this);
The SDK Manager Plugin is a Gradle plugin that downloads and updates missing or out-of-date SDKs and support libraries automatically before building your app. It ensures, that all developer and even your CI system have all necessary SDKs and support libraries to be able to build your application.
You only have to add the dependency for the SDK Manager and apply the plugin before the regular 'android' plugin to your build.gradle file:
buildscript { 

    repositories { 
        mavenCentral() 
    } 

    dependencies 
        classpath 'com.android.tools.build:gradle:0.12.+' 
        classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+' 
   } 



apply plugin: 'android-sdk-manager' 
apply plugin: 'com.android.application'

Robolectric is a powerful unit testing framework, with the significant benefit of reducing the overhead of deploying apps on a real device or an emulator before every test depending on Android Api components. By replacing all Android classes with so-called shadow objects, behaving similar to the objects of the Android SDK, you can even run your tests inside the regular JVM. You don’t depend on a Dalvik VM and its long test run startup time.
Furthermore, it is possible to provide custom implementations for specific SDK methods for simulating error conditions or hardware features like real-world sensors and behavior, for example. View Demo.


Fabric.io

Fabric was announced 2014 at twitter’s first developer conference. By the time of this post it’s still in a closed beta phase, but already one of the best tools for crash reporting and beta distribution (advertising platform not tried).
Fabric gets shipped out of the box, you only have to install the Plugin for your IDE and select the components you want to use in your app. The Plugin will show you, which dependencies have to be added and the positions in your code where changes have to be made and will perform them for you.
The crash reports are very detailed. You can see all information of the affected device, that could help you to retrace the bug’s cause as well as the whole stack trace of the uncaught exceptions over all threads.
Furthermore it provides extensive user statistics and beta distribution, similar to Google Analytics and Google play beta tests.




No comments:

Post a Comment