apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.74k stars 653 forks source link

Getting an Error 400 Bad Request after signing APK. Debug version runs perfectly #546

Closed aehaake closed 7 years ago

aehaake commented 7 years ago

Hello after I signed my apk the apolloCall.callback triggers onFailure with an error 400 as message. When testing the app from android studio on my device everything runs perfectly.

Project build.gradle:

  android {
     signingConfigs {
        config {
        }
    }

dexOptions {
    preDexLibraries = false
    javaMaxHeapSize "2g"
}

configurations.all {
    resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
    applicationId "com.app.anthony.kitav1"
    minSdkVersion 15
    targetSdkVersion 25
    versionCode 10
    versionName "1.1"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    signingConfig signingConfigs.config
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
    ebug {
        debuggable true
    }
}
productFlavors {
}
}
repositories {
mavenCentral() // jcenter() works as well because it pulls from Maven Central
}

dependencies {
compile "com.apollographql.apollo:apollo-runtime:$apolloReleaseVersion"
compile "com.apollographql.apollo:apollo-android-support:$apolloReleaseVersion"
compile "com.apollographql.apollo:apollo-rx2-support:$apolloReleaseVersion"
//compile 'com.apollographql.android:api:0.1.0'
//compile 'com.apollographql.android:converter:0.2.1'

compile 'com.android.support:cardview-v7:25.3.+'
compile 'com.github.GoodieBag:Pinview:v1.3'
compile group: 'com.squareup.picasso', name: 'picasso', version: '2.5.0'
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:25.1.0'
compile 'com.google.android.gms:play-services-auth:9.8.0'
compile 'com.google.android.gms:play-services-gcm:9.8.0'
compile 'com.google.android.gms:play-services-vision:9.8.0'
compile 'com.android.support:cardview-v7:25.1.0'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.google.firebase:firebase-auth:9.8.0'
compile 'com.google.firebase:firebase-core:9.8.0'
compile 'me.grantland:autofittextview:0.2.+'
compile 'com.squareup.okhttp3:okhttp:3.8.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'

compile 'com.github.bumptech.glide:glide:4.0.0-RC1'
compile 'com.android.support:support-v4:25.3.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0-RC1'
testCompile 'junit:junit:4.12'

}

apply plugin: 'com.google.gms.google-services'

apply plugin: 'com.apollographql.android'

` module build.gradle

buildscript {
ext.apolloReleaseVersion = '0.3.2'
repositories {
    jcenter()
    maven { url "https://jitpack.io" }
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }

}
 dependencies {

    classpath 'com.android.tools.build:gradle:2.3.3'
    classpath 'com.google.gms:google-services:3.0.0'
    classpath 'com.apollographql.apollo:gradle-plugin:0.3.2'
    //classpath 'com.apollographql.apollo:gradle-plugin:0.3.3-SNAPSHOT'
    //classpath 'com.apollographql.android:gradle-plugin:0.1.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
    maven { url 'https://jitpack.io' }
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

MainActivity on create:

        ApolloSqlHelper apolloSqlHelper = new ApolloSqlHelper(this, SQL_CACHE_NAME);
        NormalizedCacheFactory normalizedCacheFactory = new 
LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION,
                new SqlNormalizedCacheFactory(apolloSqlHelper));

        CacheKeyResolver cacheKeyResolver = new CacheKeyResolver() {
            @Nonnull
            @Override
            public CacheKey fromFieldRecordSet(@Nonnull Field field, @Nonnull Map<String, Object> map) {
                String typeName = (String) map.get("__typename");
                if ("User".equals(typeName)) {
                    String userKey = typeName + "." + map.get("login");
                    return CacheKey.from(userKey);
                }
                if (map.containsKey("id")) {
                    String typeNameAndIDKey = map.get("__typename") + "." + map.get("id");
                    return CacheKey.from(typeNameAndIDKey);
                }
                return CacheKey.NO_KEY;
            }

            @Nonnull
            @Override
            public CacheKey fromFieldArguments(@Nonnull Field field, @Nonnull Operation.Variables 
variables) {
                return CacheKey.NO_KEY;
            }
        };

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new LoggingInterceptor(token, token1))
                .build();
        apolloClient = ApolloClient.builder()
                .serverUrl(BASE_URL)
                .okHttpClient(okHttpClient)

                //.normalizedCache(normalizedCacheFactory, cacheKeyResolver)
                .build();

fetchFeed();
polling();

Mainactivity methods:

protected void polling() {
   final Handler tmHandler = new Handler() {

    };
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10000);
                    tmHandler.post(new Runnable() {

                        @Override
                        public void run() {
                             fetchFeed();

                        }
                    });
                } catch (Exception e) {
                }
            }
        }
    }).start();
}

public static void fetchFeed() {
    final LoadKids query = LoadKids.builder()
            .id(MainActivity.userEinr)
            .build();
    Toast.makeText(mContext, query.toString(), Toast.LENGTH_SHORT).show();

    initGql= MainActivity.apolloClient()
            .query(query)
            .cacheControl(CacheControl.NETWORK_FIRST);
    initGql.enqueue(dataCallback);
    Toast.makeText(mContext, dataCallback.toString(), Toast.LENGTH_SHORT).show();

}
private static ApolloCall.Callback<LoadKids.Data> dataCallback
        = new ApolloCallback<>(new ApolloCall.Callback<LoadKids.Data>() {

    @Override public void onResponse(@Nonnull Response<LoadKids.Data> response) {
        Toast.makeText(mContext, response.toString(), Toast.LENGTH_SHORT).show();

        //feedAdapter.setFeed(response.data().feedEntries());
        System.out.println("APOLLO"+ response.data().einrichtungSolo());
        List<LoadKids.EinrichtungSolo> d =response.data().einrichtungSolo();
        Toast.makeText(mContext, d.toString(), Toast.LENGTH_SHORT).show();

       initApp.setUser1(d);
    }

    @Override public void onFailure(@Nonnull ApolloException e) {
        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();

        Log.e("TAG", e.getMessage(), e);
    }
}, uiHandler);
sav007 commented 7 years ago

I can only suggest to update your proguard rules, seems that it obfuscates too much try to exclude any generated files by Apollo from proguard.

-keep class YOUR_ROOT_PACKAGE_NAME_FOR_GENERATED_APOLLO_MODELS.** {*;}

aehaake commented 7 years ago

You Are a Genius! Thank you!