Google 广告 API Android 上的客户端抛出 ProviderNotFoundException

Google Ads API Client on Android throws ProviderNotFoundException

如果我将 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.access00(InstantiatingGrpcChannelProvider.java:81)
    at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.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 APIs 其中一些使用 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);
}

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

通过将以下内容添加到您的 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(并将名称替换为错误消息中的任何文件)。