2017-02-22 82 views
1

我想在我的應用中使用Firebase Remote Config。 我在Android Studio中創建新項目,並按文檔建議運行Tools - Firebase助手。 在上console.firebase.google.com創建應用程序和旁邊設置添加到我的根build.graldeclasspath 'com.google.gms:google-services:3.0.0'和到App build.gralde使用助理添加助手後,Firebase顯示NoSuchMethodError

apply plugin: 'com.google.gms.google-services' 
… 
compile 'com.google.firebase:firebase-config:10.2.0' 

比我添加到我的MainActivity:

mFirebaseRemoteConfig.activateFetched(); 
mFirebaseRemoteConfig.fetch(); 

當我運行程序的時候,用日誌崩潰在fetch()命令:

E/AndroidRuntime: FATAL EXCEPTION: GoogleApiHandler 
java.lang.NoSuchMethodError: No static method zzz(Ljava/lang/Object;)Ljava/lang/Object; 
    in class Lcom/google/android/gms/common/internal/zzaa; 
    or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' 
    appears in /data/app/my.packagename-1/base.apk) 
at com.google.android.gms.measurement.internal.zzx.zzbd(Unknown Source) 

我試過了ti禁用即時運行和分級清除,但仍然有這個問題。如何解決它?

UPD:我弄清楚,如果我搬到線apply plugin: 'com.google.gms.google-services'從第二到最後一行中的應用build.gralde這個解決的問題,但爲什麼呢?

+1

是的,將「應用」行移動到最後一行解決了我的問題。 :-) – Marlon

回答

1

我真的不知道。因此你沒有包含完整的代碼。我這樣看: 首先,你創建一個這樣

mFirebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); 

一個實例然後,設置CONFIGS發展

FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() 
      .setDeveloperModeEnabled(BuildConfig.DEBUG) 
      .build(); 
    mFirebaseRemoteConfig.setConfigSettings(configSettings); 

然後你設置默認值mFirebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults) ;

的XML文件必須位於RES/XML

你做的取得和在監聽器內部你activateFetched()

mFirebaseRemoteConfig.fetch(cacheExpiration) 
      .addOnCompleteListener(this, new OnCompleteListener<Void>() { 
       @Override 
       public void onComplete(@NonNull Task<Void> task) { 
        if (task.isSuccessful()) { 
         Toast.makeText(MainActivity.this, "Fetch Succeeded", 
           Toast.LENGTH_SHORT).show(); 

         // After config data is successfully fetched, it must be activated before newly fetched 
         // values are returned. 
         mFirebaseRemoteConfig.activateFetched(); 
        } else { 
         Toast.makeText(MainActivity.this, "Fetch Failed", 
           Toast.LENGTH_SHORT).show(); 
        } 
        displayWelcomeMessage(); 
       } 
      }); 

你進去這必須是displayWelcomeMessage()的值在監聽器內部,它會得到的值,然後完成抓取,否則(你會通過異步被搞砸了)

我希望我回答正確回答

順便說一句,所有的示例代碼在這裏https://github.com/firebase/quickstart-android/blob/master/config/app/src/main/java/com/google/samples/quickstart/config/MainActivity.java

+0

感謝您的回覆,但我已經解決了一個問題,請參閱我的問題中的最後一句。 – anber

+0

哦。好吧,哈哈。沒有看到 –