Skip to content

Commit

Permalink
Dagger android (#5)
Browse files Browse the repository at this point in the history
* Removed networking application
* Refactor RetrofitServices to extract dependencies to dagger modules
* Add GsonTypeAdapter to configure Gson and missing License
* Move LoggingInterceptors to util class
* Update build.gradle config
* Remove joda-time proguard options
* Update proguard rules and remove unused properties
* Remove LoggingUtils and dependencies
* Update dagger version
* Add testing libraries and jacoco plugin
* Fix bugs in modules and make methods static
* Fix some issues and documentation in serializers and interceptors
* Add unit tests and javadocs for missing classes
* Add coverage badge
* Add new tests to default modules
  • Loading branch information
Fabian Alvarez authored Dec 22, 2017
1 parent 1923610 commit a73ca53
Show file tree
Hide file tree
Showing 46 changed files with 2,247 additions and 330 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![Build Status](https://www.bitrise.io/app/98b385dd0144faa0/status.svg?token=9gD8GNFS5DF6iIQ7eecIXA&branch=master)](https://www.bitrise.io/app/98b385dd0144faa0)
[![Release](https://jitpack.io/v/Wolox/wolmo-networking-android.svg)](https://jitpack.io/#Wolox/wolmo-networking-android)
[![Coverage Status](https://coveralls.io/repos/github/Wolox/wolmo-networking-android/badge.svg?branch=master)](https://coveralls.io/github/Wolox/wolmo-networking-android?branch=master)

<p align="center">
<img height="140px" width="400px" src="https://cloud.githubusercontent.com/assets/4109119/25454461/de81fad0-2aa2-11e7-831d-b1f3ea7f313a.png"/>
Expand Down
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:3.0.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.2'
}
}

Expand Down
44 changes: 31 additions & 13 deletions networking/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
apply plugin: 'jacoco-android'
apply plugin: 'com.github.kt3k.coveralls'

group = 'com.github.Wolox'

Expand All @@ -10,38 +12,54 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
consumerProguardFiles 'proguard-joda-time.pro', 'proguard-retrofit.pro',
'proguard-okhttp.pro'
consumerProguardFiles 'proguard-retrofit.pro', 'proguard-okhttp.pro', 'proguard-gson.pro',
'proguard-joda-time.pro'
}

lintOptions {
abortOnError false
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

jacocoAndroidUnitTestReport {
excludes = defaultExcludes + ['**/*_Factory.class']
}

coveralls {
jacocoReportPath = "${buildDir}/reports/jacoco/jacocoTestReleaseUnitTestReport/jacocoTestReleaseUnitTestReport.xml"
}

buildscript {
ext.wolmo_version = 'v1.3.1'
ext.wolmo_version = 'v2.0.0-rc1'
ext.retrofit_version = '2.3.0'
ext.okhttp3_version = '3.9.0'
ext.dagger_version = '2.13'
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
implementation fileTree(dir: 'libs', include: ['*.jar'])

// Wolmo
compile "com.github.Wolox:wolmo-core-android:$wolmo_version"
implementation "com.github.Wolox:wolmo-core-android:$wolmo_version"

// Third party
compile "com.squareup.retrofit2:retrofit:$retrofit_version"
compile "com.squareup.retrofit2:converter-gson:$retrofit_version"
// Dagger
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"

compile "com.squareup.okhttp3:okhttp-urlconnection:$okhttp3_version"
compile "com.squareup.okhttp3:okhttp:$okhttp3_version"
compile "com.squareup.okhttp3:logging-interceptor:$okhttp3_version"
// Third party
api "com.squareup.retrofit2:retrofit:$retrofit_version"
api "com.squareup.retrofit2:converter-gson:$retrofit_version"
api "com.squareup.okhttp3:okhttp-urlconnection:$okhttp3_version"
api "com.squareup.okhttp3:okhttp:$okhttp3_version"
api 'joda-time:joda-time:2.9.9'

compile 'joda-time:joda-time:2.9.9'
}
// Test
testImplementation "junit:junit:4.12"
testImplementation "org.mockito:mockito-inline:2.11.0" // Mockito inline adds support for mocking final classes and methods
testImplementation "org.assertj:assertj-core:3.8.0"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp3_version"
}
17 changes: 17 additions & 0 deletions networking/proguard-gson.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
4 changes: 2 additions & 2 deletions networking/proguard-joda-time.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Joda Time 2.3
## Joda Time 2.9

-dontwarn org.joda.convert.**
-dontwarn org.joda.time.**
-keep class org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
-keep interface org.joda.time.** { *; }
1 change: 0 additions & 1 deletion networking/proguard-okhttp.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okhttp3.**

-dontwarn okio.**
8 changes: 4 additions & 4 deletions networking/proguard-retrofit.pro
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Retrofit 2.X
## https://square.github.io/retrofit/ ##

-dontwarn retrofit.**
-keep class retrofit.** { *; }
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions

-keepclasseswithmembers class * {
@retrofit.http.* <methods>;
}
@retrofit2.http.* <methods>;
}
17 changes: 0 additions & 17 deletions networking/proguard-rules.pro

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* MIT License
* <p>
* Copyright (c) 2017 Wolox S.A
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package ar.com.wolox.wolmo.networking.di;

import android.support.annotation.Nullable;

import com.google.gson.FieldNamingPolicy;

import ar.com.wolox.wolmo.networking.di.modules.GsonModule;
import ar.com.wolox.wolmo.networking.di.modules.NetworkingModule;
import ar.com.wolox.wolmo.networking.di.modules.OkHttpClientModule;
import ar.com.wolox.wolmo.networking.di.scopes.NetworkingScope;
import ar.com.wolox.wolmo.networking.retrofit.RetrofitServices;
import ar.com.wolox.wolmo.networking.utils.GsonTypeAdapter;

import dagger.BindsInstance;
import dagger.Component;
import okhttp3.Interceptor;

@NetworkingScope
@Component(modules = { GsonModule.class, OkHttpClientModule.class, NetworkingModule.class })
public interface NetworkingComponent {

RetrofitServices retrofitServices();

@Component.Builder
interface Builder {

@BindsInstance
Builder baseUrl(String baseUrl);

@BindsInstance
Builder okHttpInterceptors(@Nullable Interceptor... interceptors);

@BindsInstance
Builder gsonNamingPolicy(FieldNamingPolicy namingPolicy);

@BindsInstance
Builder gsonTypeAdapters(@Nullable GsonTypeAdapter... typeAdapters);

NetworkingComponent build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ar.com.wolox.wolmo.networking.di.modules;

import ar.com.wolox.wolmo.networking.di.scopes.NetworkingScope;
import ar.com.wolox.wolmo.networking.optimizations.BaseCallCollapser;
import ar.com.wolox.wolmo.networking.optimizations.ICallCollapser;

import dagger.Module;
import dagger.Provides;

/**
* Default module with caching dependencies
*/
@Module
public class CachingModule {

@Provides
@NetworkingScope
static ICallCollapser provideBaseCallCollapser() {
return new BaseCallCollapser();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* MIT License
* <p>
* Copyright (c) 2017 Wolox S.A
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package ar.com.wolox.wolmo.networking.di.modules;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import ar.com.wolox.wolmo.networking.retrofit.serializer.LocalDateSerializer;
import ar.com.wolox.wolmo.networking.utils.GsonTypeAdapter;

import org.joda.time.LocalDate;

import javax.inject.Named;

import dagger.Module;
import dagger.Provides;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class GsonModule {

@Provides
static GsonConverterFactory provideGsonConverterFactory(Gson gson) {
return GsonConverterFactory.create(gson);
}

@Provides
static Gson provideGson(GsonBuilder gsonBuilder) {
return gsonBuilder.create();
}

@Provides
@Named("newInstance")
static GsonBuilder provideNewGsonBuilder() {
return new GsonBuilder();
}

@Provides
static GsonBuilder provideGsonBuilder(@Named("newInstance") GsonBuilder gsonBuilder,
@NonNull FieldNamingPolicy namingPolicy,
@Nullable GsonTypeAdapter... typeAdapters) {

gsonBuilder.setFieldNamingPolicy(namingPolicy);

if (typeAdapters != null && typeAdapters.length > 0) {
for (GsonTypeAdapter typeAdapter : typeAdapters) {
gsonBuilder
.registerTypeAdapter(typeAdapter.getType(), typeAdapter.getTypeAdapter());
}
} else {
gsonBuilder.registerTypeAdapter(LocalDate.class, new LocalDateSerializer());
}

return gsonBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* MIT License
* <p>
* Copyright (c) 2017 Wolox S.A
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package ar.com.wolox.wolmo.networking.di.modules;

import ar.com.wolox.wolmo.networking.di.scopes.NetworkingScope;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.Retrofit.Builder;
import retrofit2.converter.gson.GsonConverterFactory;

@Module
public class NetworkingModule {

@Provides
@NetworkingScope
static Retrofit.Builder provideRetrofitBuilder() {
return new Builder();
}

@Provides
@NetworkingScope
static Retrofit provideRetrofit(Retrofit.Builder builder, String baseUrl,
GsonConverterFactory gsonConverterFactory,
OkHttpClient client) {

return builder.baseUrl(baseUrl).addConverterFactory(gsonConverterFactory).client(client)
.build();
}
}
Loading

0 comments on commit a73ca53

Please sign in to comment.