3

So I went trough my code and I noticed that the retrofit dependency is nowhere to be found in any gradle file. My first thought was that it's bundled inside another library, so I started excluding them one by one and still I couldn't figure out where it is.

Problem 1: I can't find out the version of retrofit I'm using for troubleshooting purposes.
Problem 2: If I uncomment the HttpLoggingInterceptor it crashes with java.lang.NoSuchMethodError

Error log:

E/UncaughtException: java.lang.NoSuchMethodError: No virtual method startsWith(Lokio/ByteString;)Z in class Lokio/ByteString; or its super classes (declaration of 'okio.ByteString' appears in /data/data/com.company.app/files/instant-run/dex/slice-okio-1.6.0_7e5c9782b87cba0343c811c73cffd2135b1ad139-classes.dex)
        at okhttp3.internal.http2.Hpack$Writer.writeHeaders(Hpack.java:511)
        at okhttp3.internal.http2.Http2Writer.headers(Http2Writer.java:299)
        at okhttp3.internal.http2.Http2Writer.synStream(Http2Writer.java:127)
        at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:230)
        at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:205)
        at okhttp3.internal.http2.Http2Codec.writeRequestHeaders(Http2Codec.java:105)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:43)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
        java.lang.NoSuchMethodError: No virtual method startsWith(Lokio/ByteString;)Z in class Lokio/ByteString; or its super classes (declaration of 'okio.ByteString' appears in /data/data/com.company.app/files/instant-run/dex/slice-okio-1.6.0_7e5c9782b87cba0343c811c73cffd2135b1ad139-classes.dex)
        at okhttp3.internal.http2.Hpack$Writer.writeHeaders(Hpack.java:511)
        at okhttp3.internal.http2.Http2Writer.headers(Http2Writer.java:299)
        at okhttp3.internal.http2.Http2Writer.synStream(Http2Writer.java:127)
        at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:230)
        at okhttp3.internal.http2.Http2Connection.newStream(Http2Connection.java:205)
        at okhttp3.internal.http2.Http2Codec.writeRequestHeaders(Http2Codec.java:105)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:43)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:212)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        at java.lang.Thread.run(Thread.java:818)

Here is my code:

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

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class RestApi {
    private static String BASE_URL = "https://someurl.com";
    private static RestInterface apiService;

    public static RestInterface getApi() {

        if(apiService == null){
            Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
                    .registerTypeAdapterFactory(new ItemTypeAdapterFactory())
                    .create();

            // This crashes with NoSuchMethodError

//            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
//            logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
//
//            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
//            httpClient.addInterceptor(logging);

            Retrofit adapter = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    //.client(httpClient.build())
                    .build();

            apiService = adapter.create(RestInterface.class);
        }
        return apiService;
    }
}

Top level gradle :

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'io.fabric.tools:gradle:1.21.5'
        classpath "io.realm:realm-gradle-plugin:2.3.0"
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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

App gradle :

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.someid.app"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    testOptions {
        unitTests.returnDefaultValues = true
    }
}
repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

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


    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
    compile 'com.android.support:support-v4:25.1.0'
    compile 'com.android.support:recyclerview-v7:25.1.0'

    compile 'com.google.firebase:firebase-core:10.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'
    compile 'com.google.firebase:firebase-auth:10.0.1'
    compile 'com.google.firebase:firebase-crash:10.0.1'
    compile 'com.google.firebase:firebase-config:10.0.1'
    compile 'com.google.firebase:firebase-storage:10.0.1'
    compile 'com.google.firebase:firebase-ads:10.0.1'
    compile 'com.google.firebase:firebase-database:10.0.1'

    compile 'com.firebaseui:firebase-ui-database:1.1.1'
    compile 'com.google.android.gms:play-services-auth:10.0.1'
    compile 'com.facebook.android:facebook-android-sdk:[4,5)'

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

    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.robolectric:robolectric:3.2.1'
}

apply plugin: 'realm-android'
apply plugin: 'com.google.gms.google-services'
Hristo Stoyanov
  • 1,960
  • 1
  • 12
  • 24
  • It's not clear what exactly your problem is, does this solve your problem? http://stackoverflow.com/questions/32514410/logging-with-retrofit-2 – Egor Jan 31 '17 at 09:49
  • Please post the stack trace of `NoSuchMethodError`. To figure out where retrofit2 comes from, use `./gradlew dependencies` – Egor Jan 31 '17 at 10:08
  • what was the problem ? I'm facing the same issue – Ran Sep 26 '17 at 14:41

2 Answers2

3

There's easy way to list all project dependencies to trace where they are coming from by using dependencies task:

gradlew app:dependencies

which would produce report you can further investigate (docs: 4.7.4. Listing project dependencies).

Also, there seem to be an IDEA plugin called "Gradle View" if prefer it that way: https://plugins.jetbrains.com/idea/plugin/7150-gradle-view

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Thanks , this was helpful to find all versions and possible updates. – Hristo Stoyanov Jan 31 '17 at 12:12
  • For dependency update checks -> you may want to check [this answer for Android Studio built-in feature](http://stackoverflow.com/a/28683488/1235698) or [this one, for pure gradle based one](http://stackoverflow.com/a/41958982/1235698). – Marcin Orlowski Jan 31 '17 at 13:44
2

I noticed that your build.gradle has the following dependencies:

dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        classpath 'com.google.gms:google-services:3.0.0'
        classpath 'io.fabric.tools:gradle:1.21.5'
        classpath "io.realm:realm-gradle-plugin:2.3.0"
    }

I guess it's the fabric SDK that uses retrofit according the build.gradle on their github repo: ِ classpath 'io.fabric.tools:gradle:1.21.5' https://github.com/twitter/twitter-kit-android/blob/3530b71de233606248b2c80a4d67b78a69642523/dependencies.gradle

dependencies {
    .
    .
    retrofit:'com.squareup.retrofit2:retrofit:2.0.2',
    .
    .
}
Mina Wissa
  • 10,923
  • 13
  • 90
  • 158