Android 上的 Google Ads API 客户端引发 ProviderNotFoundException

Posted

技术标签:

【中文标题】Android 上的 Google Ads API 客户端引发 ProviderNotFoundException【英文标题】:Google Ads API Client on Android throws ProviderNotFoundException 【发布时间】:2021-07-04 20:12:27 【问题描述】:

如果我将 Google Ads API 与 Java 客户端库一起使用,则会引发以下异常:

io.grpc.ManagedChannelProvider$ProviderNotFoundException: No functional channel service provider found. Try adding a dependency on the grpc-okhttp, grpc-netty, or grpc-netty-shaded artifact
    at io.grpc.ManagedChannelProvider.provider(ManagedChannelProvider.java:43)
    at io.grpc.ManagedChannelBuilder.forAddress(ManagedChannelBuilder.java:39)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:325)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.access$1800(InstantiatingGrpcChannelProvider.java:81)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider$1.createSingleChannel(InstantiatingGrpcChannelProvider.java:231)
    at com.google.api.gax.grpc.ChannelPool.create(ChannelPool.java:72)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:241)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:219)
    at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:199)
    at com.google.ads.googleads.v7.services.stub.GrpcGoogleAdsServiceStub.create(GrpcGoogleAdsServiceStub.java:97)
    at com.google.ads.googleads.v7.services.stub.GoogleAdsServiceStubSettings.createStub(GoogleAdsServiceStubSettings.java:185)
    at com.google.ads.googleads.v7.services.GoogleAdsServiceClient.<init>(GoogleAdsServiceClient.java:138)
    at com.google.ads.googleads.v7.services.GoogleAdsServiceClient.create(GoogleAdsServiceClient.java:119)
    at com.google.ads.googleads.lib.catalog.GeneratedCatalog$V7Client.createGoogleAdsServiceClient(GeneratedCatalog.java:4948)

我不确定这是否与我使用客户端 API 的方式有关。我使用其他 Google API,其中一些使用 GoogleSignInAccount,因此我从登录帐户中提取访问令牌:

Credentials credentials = OAuth2Credentials.newBuilder()
        .setAccessToken(new AccessToken(
                GoogleAuthUtil.getToken(mContext,
                        GoogleSignIn.getLastSignedInAccount(mContext).getAccount(),
                        "oauth2:" + scope)),
                // Set expiration time in one hour, as credentials are created every time
                // this method is called.
                new Date(System.currentTimeMillis() + 60 * 60 * 1000))
        .build();
GoogleAdsClient client = GoogleAdsClient.newBuilder()
        .setCredentials(mAuthorizationService.getCredentials(SCOPE_ADWORDS))
        .setDeveloperToken(mDeveloperToken)
        .build();

String query = "SELECT metrics.cost_micros"
        + "FROM campaign "
        + "WHERE segments.date BETWEEN '" + getDate(start) + "' AND '" + getDate(end) + "' ";
SearchGoogleAdsRequest request = createRequest(client, query);
try (GoogleAdsServiceClient googleAdsService = createServiceClient(client))

    GoogleAdsServiceClient.SearchPagedResponse response = googleAdsService.search(request);

最后一行是引发异常的地方。

【问题讨论】:

【参考方案1】:

通过将以下内容添加到您的 gradle.build 文件中,可以“解决”该问题:

implementation 'io.grpc:grpc-okhttp:1.37.0'

为防止与 Google Ads API 库中包含的其他 gprc 库发生版本冲突,您应首先通过执行 gradlew app:dependencies(在 android Studio 的终端视图中)检查版本并搜索其他库(例如 io.grpc:grpc-core )。使用相同的版本而不是我的(所以将 1.37.0 替换为适当的版本)。

我已经举报了bug。

如果您遇到类似的构建错误

2 files found with path 'META-INF/io.netty.versions.properties'

添加

pickFirst 'META-INF/io.netty.versions.properties' 

到您的 gradle.build 文件的 packagingOptions (并将名称替换为错误消息中的任何文件)。

【讨论】:

以上是关于Android 上的 Google Ads API 客户端引发 ProviderNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章

Google Ads API PHP 示例的区别

Google Ads API - 是不是存在 REST API?

com.google.android.gms.ads.AdView 无法实例化

错误:找不到符号导入 com.google.android.gms.ads.InterstitialAd

如何通过 Google Ads API 获取已注销的帐户?

找不到类“com.google.android.gms.ads.identifier.AdvertisingIdClient”