【问题标题】:Cannot resolve symbol default_web_client_id in Firebase's Android Codelab无法在 Firebase 的 Android Codelab 中解析符号 default_web_client_id
【发布时间】:2016-10-15 02:23:43
【问题描述】:

我正在尝试学习 Firebase,所以我通过了 Android Codelab。然而,他们给我的项目有一个错误:

无法解析符号 default_web_client_id

而且我不知道如何解决它,因为我不知道default_web_client_id 的值或它是什么。它在onCreate() 方法中: SigninActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_in);
    mFirebaseAuth = FirebaseAuth.getInstance();

    // Assign fields
    mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);

    // Set click listeners
    mSignInButton.setOnClickListener(this);

    // Configure Google Sign In
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.default_web_client_id))
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();
}

我不知道它是什么,它的价值是什么,以及为什么它会给我这个错误。到目前为止,除了添加google-services.json 之外,我没有更改任何内容。我已经添加了我的SHA-1 并在控制台中启用了 Google。

【问题讨论】:

标签: android firebase cannot-find-symbol


【解决方案1】:

解析google-services.json 时有时会出现问题。我已将此问题报告给相关团队。

同时按照以下步骤解决此问题以更进一步 -

1) 打开google-services.json文件 -> 客户端 -> oauth_client -> client_id

2) 复制此客户端 ID 并硬编码此 .requestIdToken("your ID")

这将允许通过 GoogleSignInAccount 请求“IdToken”后成功的 google 登录,并使用 firebase 授权您的凭据。

编辑

尝试删除并重新创建项目并在您的 Android 项目中重新导入新的google-service.json

【讨论】:

  • 这是我在 oauth_client 中看到的:"oauth_client": [] 没有 ID,我也无法在文件中的任何位置找到 client_id
  • @AbAppletic 尝试添加 SHA1 密钥并从 firebase 控制台重新下载配置文件 - 项目设置
  • 还是什么都没有。我已经添加了我的 SHA1
  • @AbAppletic 如果您刚刚开始该项目。尝试删除并重新创建一个新项目-> 添加 android 应用程序。这应该有效。即使不起作用,请尝试联系 firebase 团队。
  • 如果您有超过 1 个客户端 ID 怎么办?我刚才看了一下我的项目。客户端类型 1 和客户端类型 3 有一个。这些是什么意思?
【解决方案2】:

更通用的解决方案是将google-services.json 添加到应用的根目录中。
并添加

apply plugin: 'com.google.gms.google-servicesbuild.gradle 文件的末尾。

说明

当应用程序从 google-services.json 配置文件构建键值对字符串时,然后将其放入 values.xml 文件中,以使它们在您的代码中的任何位置都可以全局使用。这使我们免于在您的代码中对 client_id 进行硬编码。

注意

不要在strings.xml 中添加default_web_client_idclient_id 作为其值,以避免重复错误,Error: Duplicate resources稍后在运行代码时。

【讨论】:

  • 在进行此更新后构建工作正常,然后运行无效缓存并重新启动。比编辑 .json 文件要好得多 - 谢谢
  • 在我的情况下不起作用。我正在使用 Android Studio 4.1
【解决方案3】:

在没有直接插入client_id 的情况下搜索“智能”修复程序一段时间后,按照FirebaseUI project 的答案,我只需要在app/build.gradle 中添加下一行:

implementation 'com.firebaseui:firebase-ui-auth:4.3.2'

【讨论】:

  • 这有助于解决构建过程中的错误。但是,它不会自动将 json 文件中的 ID 放入 values.xml ......所以它可以工作但不能登录:(
  • 是的,它对我有用,非常感谢,这比直接插入 client_id 更好
【解决方案4】:

显然R.string.default_web_client_id 是从 IDE 构建生成的

我以为我们应该手动添加它 - 耗时的错误

https://developers.google.com/android/guides/google-services-plugin

google-services 插件有两个主要功能: 1) 处理 google-services.json 文件并生成 Android 资源 在您的应用程序代码中使用。

~~~~

JSON 处理的主要结果是生成两个 XML 文件 您可以在 Java 代码中将其作为 Android 资源引用。

所以 - 成功构建后,如果您在 IDE 中搜索字符串 default_web_client_id ,您将看到 /generated 文件夹下的一个结果是 values.xml ,其中包含以下值您的 firebase 配置,如下例所示。

实际上看到那个文件,有助于澄清这里的事情

<resources>
    <string name="default_web_client_id" translatable="false">123.apps.googleusercontent.com</string>
    <string name="firebase_database_url" translatable="false">https://123.firebaseio.com</string>
    <string name="gcm_defaultSenderId" translatable="false">123</string>
    <string name="google_api_key" translatable="false">123</string>
    <string name="google_app_id" translatable="false">123</string>
</resources>

【讨论】:

  • 这是我的问题的解决方案,我刚刚重建了项目并且它工作了。
【解决方案5】:
  1. google-services.json 在 ./app/ 文件夹中
  2. 在项目级build.gradle 中添加以下内容:
    buildscript {
    ...
    dependencies {
        ...
        classpath 'com.google.gms:google-services:4.3.5'
    }
  1. 在应用级build.gradle,应用插件:
    apply plugin: 'com.google.gms.google-services'

这是我发现的烦人的事情。将其从 4.3.5 升级到高于此值的任何内容都会导致 Android Studio 无法检测到生成的 values.xml 文件。

【讨论】:

  • 相同的 com.google.gms:google-services:4.3.10 不工作,所以我实现了 4.3.5
【解决方案6】:

**对我来说,目前的主要问题是确保从同一位置下载 json 文件。如果第一个来自 firebase 控制台,请不要使用 api 控制台获取文件,反之亦然。文件不一样**

【讨论】:

  • 感谢您的评论。我只是尝试从 API 谷歌下载 json 文件,但现在所有链接都参考 Firebase 的下载页面:(
  • 很抱歉听到这个消息,但这有点取决于原始文件的来源。如果该项目最初是一个 Firebase 项目,则使用该文件。如果项目来自 api 控制台下载并使用该文件。不要混合它们。在这篇文章的时候,这是真的。不幸的是,它是一个移动的目标。
  • 好吧,现在我硬编码了网络 ID,至少它可以工作,即使是硬编码
  • 也向 firebase 发送了一个问题,他们根本没有回应 - 所以将来我会将所有后端从 firebase 转移到更可靠的地方
【解决方案7】:

我已经下载并解析了google-services.json,但它仍然找不到字符串。

我注意到我的 oauth_client 有一个 client_type 为 1 的密钥,仅此而已。在 Google API 控制台中,我只有一个 Android 密钥。

因此,您需要转到 API 控制台并生成一个 Web Server 密钥。然后,再次下载您的 google-services.json,您将拥有一个类型为 3 的oauth_client

现在,插件将生成一个名为 default_web_client_id 的字符串。

【讨论】:

    【解决方案8】:

    我有同样的问题或类似的问题,

    确保在您的 google-services.json 中有:

    ...    
    "client": [
            ...
              "oauth_client": [
                ...
                {
                  "client_id": "YOUR WEB CLIENT ID",
                  "client_type": 3
                }     
    ...
    

    由于某种原因,从 firebase 控制台下载的文件不包含它。

    在 google-services.json 文件中添加条目后,一切都开始按预期工作。

    google-services-plugin documentation

    【讨论】:

      【解决方案9】:

      除了 Dexto 的回答,我还想提一件事 在 JSON 文件中,您将获得两种客户端 ID

      一个 client_type 值为 1,另一个 client_type 值为 3 确保您指定了 client_typeclient_id,其值为 3

      【讨论】:

        【解决方案10】:
        classpath 'com.google.gms:google-services:4.1.0'
        

        有问题。改为使用:

        classpath 'com.google.gms:google-services:4.2.0'
        

        【讨论】:

          【解决方案11】:

          下载您最新的google-services.jsonclient_id 列表存在于您的Google Cloud Credentials 中的OAuth 2.0 客户端ID

          然后检查它是否包含client_id"client_type" : 3。如果没有,您需要创建一个新的:

          1. 在 API 控制台中打开 Credentials 页面。
          2. 点击创建凭据 -> OAuth 客户ID。然后选择类型 Web 应用程序
          3. 等待 2-3 分钟,刷新 Firebase Console 并再次下载您的 google-services.json。它现在应该包含client_id"client_type" : 3

          清理并重建您的项目以应用新的 API 配置。


          带有"client_type" : 3client_id 通常在oauth_client 标签内,而不是servicesother_platform_oauth_client

          如果您遇到这种情况并且无法构建项目,请尝试将您的 client_id 复制到 oauth_client 标记并重新构建。

          "client": [
                  ...
                  "oauth_client": [
                      ...
                      {
                        "client_id": "YOUR WEB CLIENT ID",
                        "client_type": 3
                      }
                  ]
          ]     
          

          【讨论】:

            【解决方案12】:

            在 Firebase 控制台中更改配置后,再次尝试下载您的 .json 文件。使用这个较新的配置文件,而不是旧的。

            【讨论】:

              【解决方案13】:

              在使用此链接将我的后端 ID 创建到 Google API 后已修复。

              https://developers.google.com/identity/sign-in/android/start-integrating#get_your_backend_servers_oauth_20_client_id

              • 1- 在 API 控制台中打开凭据页面。

              • 2- Web 应用程序类型客户端 ID 是您的后端服务器的 OAuth 2.0 客户端 ID。

              之后,你可以重新下载你的json文件,android studio会自动匹配你的字符串id。

              【讨论】:

                【解决方案14】:

                我知道现在回答很晚,但希望这对将来的人有所帮助。

                无需在应用程序中硬编码 default_web_client_id 即可访问。

                要从 google-services.json 访问 Android App 中的 default_web_client_id,我们必须在 FireBase 项目设置下添加 SHA1 密钥。

                转到 Firebase 控制台 > 打开项目 > 选择应用 > 添加指纹。

                此副本生成 google-services.json 到项目后。

                之后你会看到json文件的区别如下:

                之前:

                "oauth_client": []
                

                之后:

                "oauth_client": [
                    {
                      "client_id": "23........4-asdj...........................asda.googleusercontent.com",
                      "client_type": 1,
                      "android_info": {
                        "package_name": "com.abc.xyz",
                        "certificate_hash": "asjhdashhs"
                      }
                    },.....
                

                这将解决您的问题。

                【讨论】:

                  【解决方案15】:

                  对此的通用解决方案是像这样在 build.gradle 末尾应用 google play services 插件

                    apply plugin: 'com.android.application'
                  
                  android {
                      compileSdkVersion 30
                      buildToolsVersion "30.0.0"
                  
                      buildFeatures {
                          dataBinding true
                      }
                  
                      defaultConfig {
                          applicationId "xxxxxx"
                          minSdkVersion 21
                          targetSdkVersion 30
                          versionCode 1
                          versionName "1.0"
                          multiDexEnabled true
                          testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                      }
                  
                      buildTypes {
                          release {
                              minifyEnabled false
                              proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                          }
                      }
                  }
                  
                  dependencies {
                      implementation fileTree(dir: "libs", include: ["*.jar"])
                      implementation 'androidx.appcompat:appcompat:1.2.0'
                      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
                  
                  
                  
                      testImplementation 'junit:junit:4.13'
                      androidTestImplementation 'androidx.test.ext:junit:1.1.1'
                      androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
                      // For Common Dimension
                      implementation 'com.intuit.sdp:sdp-android:1.0.5'
                      implementation 'com.intuit.ssp:ssp-android:1.0.5'
                      // Retrofit and Gson
                      implementation 'com.google.code.gson:gson:2.8.6'
                      implementation 'com.squareup.retrofit2:retrofit:2.6.1'
                      implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
                      implementation 'com.squareup.retrofit2:converter-scalars:2.6.1'
                      // Rx Java and Dagger
                      implementation 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
                      implementation 'io.reactivex:rxandroid:1.2.1'
                      implementation 'io.reactivex:rxjava:1.1.6'
                  
                      implementation 'com.google.dagger:dagger:2.24'
                      annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
                      compileOnly 'javax.annotation:jsr250-api:1.0'
                      compileOnly 'org.glassfish:javax.annotation:10.0-b28'
                      // Glide Image Loading
                      implementation 'com.github.bumptech.glide:glide:4.9.0'
                      annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
                      implementation 'com.android.support:design:30.0.0'
                      implementation 'com.android.support:recyclerview-v7:30.0.0'
                      implementation 'com.android.support:cardview-v7:30.0.0'
                      implementation 'com.android.support:multidex:1.0.3'
                  
                      /*Jsoup*/
                      implementation 'org.jsoup:jsoup:1.9.1'
                  
                      /*Firebase*/
                      implementation 'com.google.firebase:firebase-core:17.5.0'
                      implementation 'com.google.firebase:firebase-config:19.2.0'
                      implementation 'com.google.firebase:firebase-messaging:20.2.4'
                      implementation 'com.google.firebase:firebase-database:19.3.1'
                      implementation 'com.google.firebase:firebase-auth:19.3.2'
                      implementation 'com.firebaseui:firebase-ui-storage:6.2.0'
                      implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
                      implementation 'com.google.firebase:firebase-analytics:17.5.0'
                  
                      /*location and google map*/
                      implementation 'com.google.android.gms:play-services-maps:17.0.0'
                      implementation 'com.google.android.gms:play-services-location:17.0.0'
                      implementation 'com.google.android.gms:play-services-places:17.0.0'
                      implementation 'com.google.android.gms:play-services-auth:18.1.0'
                  
                      /*Circle Image View*/
                      implementation 'de.hdodenhof:circleimageview:3.0.1'
                      implementation 'com.github.ittianyu:BottomNavigationViewEx:2.0.4'
                      implementation "com.android.support:design:30.0.0"
                      implementation 'com.facebook.android:facebook-android-sdk:5.15.3'
                  
                  }
                  
                  apply plugin: 'com.google.gms.google-services'
                  

                  【讨论】:

                    【解决方案16】:

                    在我的情况下,我忘了添加

                    id 'com.google.gms.google-services'
                    

                    到build.gradle(:app)的插件

                    【讨论】:

                      【解决方案17】:

                      我也遇到了同样的问题,请确保“google-services.json”在您的应用目录中。然后只需从“Build -> Rebuild Project”重建项目

                      由于字符串资源“default_web_client_id”是自动生成的,一旦你重建项目就会解决

                      【讨论】:

                        【解决方案18】:

                        就我而言: 该库是旧的,所以我去获取最后一个库:https://firebase.google.com/docs/auth/android/firebaseui

                        放入依赖: 实现 'com.firebaseui:firebase-ui-auth:7.2.0'

                        以及目前的情况

                        // 为 Firebase 平台导入 BoM 实现平台('com.google.firebase:firebase-bom:26.7.0')

                        // 使用 BoM 时,您无需在 Firebase 库依赖项中指定版本 实施 'com.google.firebase:firebase-auth-ktx'

                        它是固定的

                        【讨论】:

                          【解决方案19】:

                          使用以下代码更新您的项目级 build.gradle 文件:

                          buildscript {
                          repositories {
                              google()
                              jcenter()
                              mavenCentral()
                          }
                          dependencies {
                              classpath 'com.android.tools.build:gradle:3.3.2'
                              classpath 'com.google.gms:google-services:4.2.0'
                          }}
                          allprojects {
                          repositories {
                              google()
                              jcenter()
                              maven { url "https://maven.google.com"}  
                          }}
                          task clean(type: Delete) {
                          delete rootProject.buildDir }
                          

                          更多详情:answerdone.com

                          【讨论】:

                            【解决方案20】:

                            对我来说,问题是因为我使用的是 minSdkVersion 15,更新到 16 解决了我的问题。

                            【讨论】:

                              【解决方案21】:
                              implementation platform('com.google.firebase:firebase-bom:29.0.0')
                              
                              implementation 'com.firebaseui:firebase-ui-auth:4.3.2'
                              

                              将这些行放入build.gradle(projectName)

                              【讨论】:

                                【解决方案22】:

                                从您与您的 android 项目连接的 firebase 项目中再次下载 google-services.json 文件,并将其替换到 app/src 目录中。然后选择构建子菜单中的clean project选项。这对我有用。

                                【讨论】:

                                  【解决方案23】:

                                  我找了一整天,已经把那个json文件粘贴到App里面并rebuild了很多次,但是android studio还是在ide中标记为error。

                                  解决方案:直接运行项目,忽略错误,它会工作,这是一个IDE错误。

                                  【讨论】:

                                    猜你喜欢
                                    • 2019-06-16
                                    • 2019-06-15
                                    • 2016-10-06
                                    • 1970-01-01
                                    • 2017-08-09
                                    • 2016-12-04
                                    • 1970-01-01
                                    • 2021-12-31
                                    • 1970-01-01
                                    相关资源
                                    最近更新 更多