gpt4 book ai didi

android - com.squareup.okhttp3 库的 Gradle 依赖问题

转载 作者:搜寻专家 更新时间:2023-11-01 08:20:15 24 4
gpt4 key购买 nike

我正在将 Back4App 集成到我的应用程序中,并逐步按照 android 教程进行操作,现在我在 live-query 中本教程的一部分,遇到了重复 okhttp3 库的问题。我能够成功构建项目,但是在尝试运行时,我收到编译错误,指出

Program type already present: okhttp3.internal.ws.WebSocketReader$FrameCallback

我的App级build.gradle是

apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId 'my.app.id'

//multiDexEnabled true

minSdkVersion 19
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"


javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}

configurations.all {
// OkHttp 3.5.0+ includes the websockets API, so we need this to prevent a conflict
//exclude module: 'okhttp-ws'
}

packagingOptions {
exclude 'META-INF/rxjava.properties'
}
}

dependencies {
// Google GCM Firebase stuff
implementation 'com.parse.bolts:bolts-android:1.4.0'
implementation 'com.google.firebase:firebase-core:16.0.3'

implementation 'com.google.firebase:firebase-messaging:17.3.0'

// Back4App library
implementation 'com.parse:parse-android:1.16.3'


// Back4App Live Query Client
// *************************************************** //
implementation 'com.github.tgio:parse-livequery:1.0.3'
// *************************************************** //

// in-app billing
implementation 'com.android.billingclient:billing:1.1'
// Arch COmponents ViewModelProviders Library
implementation "android.arch.lifecycle:extensions:$rootProject.lifecycle_version"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.room_version"
// For Room Dependencies
implementation "android.arch.persistence.room:runtime:$rootProject.room_version"
//implementation "android.arch.persistence.room:compiler:$room_version"
// Room -- Optionales
implementation "android.arch.persistence.room:rxjava2:$rootProject.room_version"
// Room Test helpers
testImplementation "android.arch.persistence.room:testing:$rootProject.room_version"
implementation 'de.hdodenhof:circleimageview:2.2.0'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:support-v13:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'org.jetbrains:annotations-java5:15.0'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-vector-drawable:27.1.1'
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
// AndroidJUnitRunner and JUnit Rules
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-accessibility:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-web:3.0.2'
androidTestImplementation 'com.android.support.test.espresso.idling:idling-concurrent:3.0.2'
// Espresso dependency either "compile" or "androidTestImplementation", depends on app's implementation
androidTestImplementation 'com.android.support.test.espresso:espresso-idling-resource:3.0.2'
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:2.15.0'
androidTestImplementation 'com.android.support:support-annotations:27.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

repositories {
jcenter()
google()
maven {
url "https://maven.google.com"
}
}
apply plugin: 'com.google.gms.google-services'

这是我正在测试的 Activity

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.SaveCallback;

import org.json.JSONObject;

import tgio.parselivequery.BaseQuery;
import tgio.parselivequery.LiveQueryClient;
import tgio.parselivequery.LiveQueryEvent;
import tgio.parselivequery.interfaces.OnListener;

public class QueryExample extends AppCompatActivity {

// Subscription
final tgio.parselivequery.Subscription sub = new BaseQuery.Builder("message")
.where("destination", "pokelist")
.addField("content")
.build()
.subscribe();



// Back4App's Parse setup
private void initializeParse() {

Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId(getResources().getString(R.string.back4app_server_url))
.clientKey(getResources().getString(R.string.back4app_client_key))
.server("https://parseapi.back4app.com/")
.build());

initLiveQueryClient();
}

// Init Live Query Client
private void initLiveQueryClient() {

// Example: 'wss://livequerytutorial.back4app.io'
LiveQueryClient.init("wss://"+"myprototype.back4app.io",
getResources().getString(R.string.back4app_app_id),
true);

LiveQueryClient.connect();
}

int numPokes = 0;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_live_query_example);
EditText pokeText = findViewById(R.id.pokeText);

initializeParse();

sub.on(LiveQueryEvent.CREATE, new OnListener() {
@Override
public void on(JSONObject object) {
runOnUiThread(new Runnable() {
@Override
public void run() {
EditText pokeText = findViewById(R.id.pokeText);
numPokes++;
if(numPokes == 1) {
pokeText.setText("Poked " + numPokes + " time.");
}
else {
pokeText.setText("Poked " + numPokes + " times.");
}
}
});
}
});

FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
ParseObject poke = new ParseObject("Message");
poke.put("content", "poke");
poke.put("destination", "pokelist");
poke.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
Snackbar.make(view, "Poke has been sent!", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
});
}
}

注释掉的时候注意

实现'com.github.tgio:parse-livequery:1.0.3'

从我的应用程序级别 build.gradle,我能够编译和运行该应用程序,但是上面的 QueryExample Activity 由于缺少 okhttp3-ws 导入而崩溃

我研究了很多关于这个问题的 Stack Overflow 问题并实现了这个解决方案 Here .原因在此 Github 论坛(弃用)中进行了解释,但与 2017 年 3 月 21 日的 cinder92 评论类似,问题仍然存在。有没有人经历过这个问题,如果找到了解决方案?我的 Activity 短暂运行,直到崩溃并显示错误日志

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kungfu.tuga, PID: 25976
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/ws/WebSocketListener;
at tgio.parselivequery.LiveQueryClient.getNewRequestId(LiveQueryClient.java:128)
at tgio.parselivequery.BaseQuery$Builder.<init>(BaseQuery.java:100)
at com.kungfu.tuga.utilities.the_wire.QueryExample.<init>(QueryExample.java:27)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.ws.WebSocketListener" on path: DexPathList[[zip file "/data/app/com.kungfu.tuga-9jJWU4yht8e1tuYOuZmIQQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.kungfu.tuga-9jJWU4yht8e1tuYOuZmIQQ==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at tgio.parselivequery.LiveQueryClient.getNewRequestId(LiveQueryClient.java:128)
at tgio.parselivequery.BaseQuery$Builder.<init>(BaseQuery.java:100)
at com.kungfu.tuga.utilities.the_wire.QueryExample.<init>(QueryExample.java:27)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1174)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2669)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

最佳答案

是的,我也遇到过这个问题,但是,如果你这样设置你的 gradle:

implementation ('com.parse:parse-android:1.16.3'){
exclude group: "com.squareup.okhttp3"
}

它将毫无问题地工作。 (:

关于android - com.squareup.okhttp3 库的 Gradle 依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52178396/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com