2016-05-17 117 views
0

我正在使用android studio中的Parse SDK。每件事情都工作得很好,直到我在我的项目中启用multidex,因为在添加地图库后,我的项目超出了64K方法的限制。我得到以下错误,当我运行我的应用程序:解析sdk“java.lang.NoSuchMethodError”。 Okhttp库之间的冲突

java.lang.NoSuchMethodError: com.squareup.okhttp.OkHttpClient.setFollowRedirects 
at com.parse.ParseOkHttpClient.<init>(ParseOkHttpClient.java:58) 
at com.parse.ParseHttpClient.createClient(ParseHttpClient.java:45) 
at com.parse.ParsePlugins$Android.newHttpClient(ParsePlugins.java:175) 
at com.parse.ParsePlugins.restClient(ParsePlugins.java:91) 
at com.parse.Parse.getEventuallyQueue(Parse.java:615) 
at com.parse.Parse.access$800(Parse.java:42) 
at com.parse.Parse$1.call(Parse.java:410) 
at com.parse.Parse$1.call(Parse.java:407) 
at bolts.Task$4.run(Task.java:357) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
at java.lang.Thread.run(Thread.java:856) 

这是我gradle这个文件看起来像:

apply plugin: 'com.android.application' 

android { 
compileSdkVersion 23 
buildToolsVersion "23.0.2" 

dexOptions { 
    javaMaxHeapSize "4g" 
} 

defaultConfig { 
    minSdkVersion 14 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
    multiDexEnabled true 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(dir: 'libs', include: ['*.jar']) 
testCompile 'junit:junit:4.12' 
compile 'com.github.clans:fab:1.6.2' 
compile 'com.google.android.gms:play-services:8.4.0' 
compile 'com.google.maps:google-maps-services:0.1.9' 
compile 'com.google.maps.android:android-maps-utils:0.4' 
compile 'com.balysv:material-ripple:1.0.2' 
compile 'com.android.support:cardview-v7:23.3.0' 
compile 'com.android.support:recyclerview-v7:23.3.0' 
compile 'com.android.support:design:23.3.0' 
compile 'com.android.support:appcompat-v7:23.3.0' 
compile 'com.android.support:multidex:1.0.1' 
compile('com.github.afollestad.material-dialogs:core:[email protected]') { 
    transitive = true 
} 
compile 'com.parse.bolts:bolts-android:1.4.0' 
compile 'com.parse:parse-android:1.13.0' 
} 

,这是我的清单文件:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.rowburst.traverous.traverous"> 

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="23" /> 

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

<application 
    android:name="com.example.MyApplication" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/MAPS_API_KEY" /> 

    <activity 
     android:name="com.example.LaunchingActivity" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.NoDisplay"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name="com.example.LoginActivity" 
     android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen" /> 

    <activity android:name="com.example.MainActivity"></activity> 
</application> 

这是我的应用程序类:

public class MyApplication extends Application { 

@Override 
public void onCreate() { 
    super.onCreate(); 
    Parse.initialize(new Parse.Configuration.Builder(this) 
      .applicationId(Const.APP_ID) 
      .server(Const.SERVER_URL) 
      .build()); 
} 

@Override 
protected void attachBaseContext(Context base) { 
    super.attachBaseContext(base); 
    MultiDex.install(this); 
} 
} 

我觉得问题在于MyApplication类中的Parse.initialize语句。因为当我调试我的应用程序,并在该语句上设置一个断点(每一件事情在该语句之前看起来都很好),当执行initialize语句时,我的应用程序崩溃。我搜索了很多,我没有找到任何解决这个问题。请帮助...

编辑: 确定后进一步研究,并与gradle这个依赖玩弄我发现这个错误不是因为multiDexEnabled的,而这是因为通过分析和使用okhttp库之间的冲突google-地图服务。 Google-maps-services使用OkHttp:2.0.0,我认为Parse是使用OkHttp:2.4.0。我认为OkHttp:2.0.0没有名称为setFollowRedirects的这种方法,它覆盖了OkHttp:2.4.0,这就是为什么我的应用程序崩溃。如果我删除谷歌地图服务编译声明我的应用程序工作正常。

注意:如果我做了compile com.squareup.okhttp:okhttp:2.4.0错误不再出现,但我的解析函数(即登录和注册回调)给出这个错误com.parse.ParseException:java.lang.IllegalArgumentException: value == null,他们不再工作。

+0

您是否曾尝试在'super.onCreate();'之前添加'MultiDex.install(this);''? – diogojme

+0

确实很重要吗?因为我已经将它添加到了'attachBaseContext'方法中,并且我已经使用log语句进行了检查,它在'onCreate()'之前被调用。 –

+0

我试过了,还是一样的错误。 –

回答

1

当您初始化分析时,将空字符串传递给客户端密钥。

Parse.initialize(new Parse.Configuration.Builder(this) 
     .applicationId(Const.APP_ID) 
     .server(Const.SERVER_URL) 
     .clientKey("") 
     .build()); 
+0

:它工作,非常感谢!我甚至不认为这可能是正确的答案 - 我的不好。但我不知道为什么使用空字符串初始化clientkey可解决问题? –

0

尝试增加

compile 'com.squareup.okhttp:okhttp:2.4.0' 

您的build.gradle文件。

+0

这解决了'NoSuchMethodError',但不幸的是,解析sdk不再工作,如果我这样做。它在每个我从解析sdk调用的方法上给出了'com.parse.ParseException:java.lang.IllegalArgumentException:value == null'。例如'ParseUser.logInInBackground'或'ParseUser.signUpInBackground' –

+0

请阅读编辑,看看你能帮忙吗? –