无法在 Firebase 的 Android Codelab 中解析符号 default_web_client_id

Cannot resolve symbol default_web_client_id in Firebase's Android Codelab

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

Cannot resolve symbol 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。

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

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

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

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

它将允许通过 GoogleSignInAccount post 请求 "IdToken" 成功 google 登录并使用 firebase 授权您的凭据。

编辑

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

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

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

说明

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

备注

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

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

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

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

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

oauth_client

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

在使用此 link 将我的后端 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。

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

显然 R.string.default_web_client_id 是从 IDE 构建

生成的

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

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

The google-services plugin has two main functions: 1) Process the google-services.json file and produce Android resources that can be used in your application's code.

~~~~

The main result of the JSON processing is to produce two XML files which you can reference as Android resources in your Java code.

等等 - 成功构建后,如果您在 IDE 中搜索字符串 default_web_client_id ,您将看到一个结果是 values.xml在 /generated 文件夹下,其中包含您的 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>

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

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

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

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

有问题。而是使用:

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

经过一段时间搜索 "smart" 修复而不直接插入 client_id,按照 FirebaseUI project 的回答,我只需要在 app/build.gradle 中添加下一行:

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

我遇到了相同或类似的问题,

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

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

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

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

google-services-plugin documentation

下载最新的 google-services.json。在您的 Google Cloud Credentials.

中存在 OAuth 2.0 客户端 IDclient_id 列表

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

  1. 在 API 控制台中打开 Credentials 页面。
  2. 单击创建凭据 -> OAuth 客户端 ID。然后选择类型 Web application.
  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
            }
        ]
]     

使用以下代码更新您的项目级别 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

我知道现在回答已经晚了,但希望这对以后的人有所帮助。

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

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

转到 Firebase 控制台 > 打开项目 > Select 应用程序 > 添加指纹。

此副本生成后 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"
      }
    },.....

这将解决您的问题。

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

一般解决方案是在 build.gradle 末尾应用 google 播放服务插件,像这样

  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'

就我而言,我忘了添加

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

到build.gradle(:app)

的插件

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

重建项目

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

  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 文件。

我找了一整天,我已经把那个json文件粘贴到ide应用程序并重建了很多次,但android工作室仍然将其标记为错误ide.

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

implementation platform('com.google.firebase:firebase-bom:29.0.0')

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

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

对于我的情况: 该库很旧,所以我去获取最后一个库: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'

并且已经修复

使用 BOM 以使用兼容版本的 firebase。

在你的模块中(app-level)Gradle 文件:

dependencies {
  // ...

  // Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:29.3.0')

  // When using the BoM, you don't specify versions in Firebase library dependencies

  // Declare the dependency for the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics'

  // Declare the dependencies for any other desired Firebase products
  // For example, declare the dependencies for Firebase Authentication and Cloud Firestore
  implementation 'com.google.firebase:firebase-auth'
  implementation 'com.google.firebase:firebase-firestore'
}

来源:https://firebase.google.com/docs/android/setup?authuser=0&hl=en#add-sdks