如果在gradle中没有库依赖项,改造如何工作?

时间:2017-01-31 09:45:34

标签: android gradle retrofit2

所以我通过我的代码,我注意到在任何gradle文件中都找不到改进依赖项。我的第一个想法是它被捆绑在另一个图书馆里面,所以我开始逐个排除它们,但我仍然无法弄清楚它在哪里。

问题1: 我无法找到我用于故障排除目的的改装版本 问题2: 如果我取消注释HttpLoggingInterceptor它会崩溃java.lang.NoSuchMethodError

错误日志:

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)

这是我的代码:

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;
    }
}

顶级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'

2 个答案:

答案 0 :(得分:3)

通过使用dependencies任务,可以轻松列出所有项目依赖项以跟踪它们的来源:

gradlew app:dependencies

会产生报告,您可以进一步调查(docs: 4.7.4. Listing project dependencies)。

此外,似乎有一个名为" Gradle View"的IDEA插件。如果喜欢那样:https://plugins.jetbrains.com/idea/plugin/7150-gradle-view

答案 1 :(得分:2)

我注意到你的build.gradle有以下依赖项:

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"
    }

我想这是根据他们的github repo上的build.gradle使用改进的结构SDK: 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',
    .
    .
}