Sources

Android Shortcuts: Starting a new project

noras noras |

Tags: #Android #Android Studio #gradle #shortcuts

So you are staring your android project and need to prepare your gradle file. Here is a shortcut offered to you on a gold plate.

Adding the essential repositories

Android studio with gradle is an awesome framework that offers you a painless way to add libraries to your projects without any hassle. In order to import the libraries, they need to be imported from repositories. The most used libraries in my experience are the following.

repositories {
    jcenter()
    maven { url 'https://maven.fabric.io/public' }
    maven { url 'https://jitpack.io' }
    maven { url 'https://maven.google.com' }
}

Some of the most used libraries

Here is a list of the most used libraries by me. I will try to extend this list and add more explanations about how and when to use each library. Hopefully with some lovely shortcut code.

HTTP requests

 compile 'com.android.volley:volley:1.1.0'

Useful template for volley

 public class APIManager {

        private Cache cache;
        private Network network;
        private RequestQueue queue;
        private Context context;


        public APIManager(Context context) {
            this.context = context;
            cache = new DiskBasedCache(context.getCacheDir(), 1024 * 1024);
            network = new BasicNetwork(new HurlStack());
            queue = new RequestQueue(cache, network);
            queue.start();
        }


        public void doSomePostRequest(String payload) {

            final JSONObject params = new JSONObject();
            try {


                JSONObject payloadJson = new JSONObject();
                payloadJson.put("payload", payload);


            } catch (JSONException e) {
                e.printStackTrace();
            }


            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, API_ENDPOINT, params,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {

                            Log.d("RESPONSE ", "RESPONSE " + response.toString());

                        }
                    }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("RESPONSE_ERROR", "RESPONSE_ERROR " + error.getMessage());
                        NetworkResponse networkResponse = error.networkResponse;
                        if (networkResponse != null && networkResponse.data != null) {
                            String jsonError = new String(networkResponse.data);
                            // Print Error!
                            Log.d("RESPONSE_ERROR", "RESPONSE_ERROR " + jsonError);
                        }

                    }

                }
            );
            queue.add(jsonObjectRequest);
        }

    }

Loading spinners

source

 compile 'com.github.ybq:Android-SpinKit:1.2.0'

Chart library

source

 compile 'com.github.PhilJay:MPAndroidChart:v3.0.3'

Customisable button navigation bar

compile 'com.aurelhubert:ahbottomnavigation:2.1.0'

Firebase

compile 'com.google.firebase:firebase-messaging:11.8.0'

View Pager Indicator

compile 'fr.avianey.com.viewpagerindicator:library:2.4.1.1@aar'

Barcode and Qr-code scanning

 compile 'me.dm7.barcodescanner:zbar:1.9'

Loading and caching images

 compile 'com.squareup.picasso:picasso:2.5.2'

Tracking crashes

compile('com.crashlytics.sdk.android:crashlytics:2.6.5@aar') {
        transitive = true;
    }

Binding JSON to Objects

 compile 'com.fasterxml.jackson.core:jackson-core:2.6.7'
 compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.7'
 compile 'com.fasterxml.jackson.core:jackson-databind:2.6.7'

Local database solution

 compile 'com.j256.ormlite:ormlite-android:4.48'

Internal data exchange

 compile 'de.greenrobot:eventbus:2.2.1'

Scanning credit cards

compile 'io.card:android-sdk:5.5.0'

About the author

noras

"Senior Software Engineer. MSc in Computer systems and Networks with big interest in security. Loves to play with Android code and does security research for fun and profit. Speaks 4 languages and codes in much more."

Related articles

Tags: #Android #Android Studio #gradle #shortcuts




Copyright © 2019 - nindoda.com