Skip to content

Instantly share code, notes, and snippets.

View andretietz's full-sized avatar

André Tietz andretietz

View GitHub Profile
@andretietz
andretietz / android-dependencies.gradle
Last active May 9, 2017 20:48
android-dependencies
ext {
sourceCompatibilityVersion = JavaVersion.VERSION_1_8
targetCompatibilityVersion = JavaVersion.VERSION_1_8
versions = [
annotations: [
jsr305: '3.0.1'
],
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClearableEditText">
<attr name="clearIcon" format="reference"/>
</declare-styleable>
</resources>
public final class YourLibraryInitProvider extends ContentProvider {
public YourLibraryInitProvider() {
}
@Override
public boolean onCreate() {
// get the context (Application context)
Context context = getContext();
// initialize whatever you need
<manifest package="some.library.packagename"
xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<provider
android:authorities="${applicationId}.yourlibraryinitprovider"
android:exported="false"
android:enabled="true"
android:name=".YourLibraryInitProvider" />
</application>
public final class YourLibraryInitProvider extends ContentProvider {
...
@Override
public void attachInfo(Context context, ProviderInfo providerInfo) {
if (providerInfo == null) {
throw new NullPointerException("YourLibraryInitProvider ProviderInfo cannot be null.");
}
// So if the authorities equal the library internal ones, the developer forgot to set his applicationId
public class YourApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
SomeLibrary.init(this);
SomeOtherLibrary.init(this);
}
}
// use SparseArray on android! I just did this to make clear what happens here
Map<Integer, String> annotationRegistration = new HashMap<>();
List<CallAdapter.Factory> factories = new ArrayList<>();
// add all your CallAdapter.Factories here
factories.add(...)
// add default CallAdapter as the last item. This is mandatory!
factories.add(Retrofit2Platform.defaultCallAdapterFactory(null));
CustomAnnotationCallAdapterFactory callAdapterFactory =
new CustomAnnotationCallAdapterFactory(factories, annotationRegistration);
package retrofit2;
import java.util.concurrent.Executor;
import retrofit2.CallAdapter.Factory;
public final class Retrofit2Platform {
private Retrofit2Platform() {
}
class CustomAnnotationInterceptor implements Interceptor {
private final Map<Integer, String> registration;
public CustomAnnotationInterceptor(Map<Integer, String> reg) {
this.registration = reg;
}
private Integer identify(Request request) {
// make sure this is the same method you use in the CallAdapter
return (request.url() + request.method()).hashCode();