androidgoogle-ads-apigoogle-client

Google Ads API Client on Android throws ProviderNotFoundException


If I use Google Ads API with Java client library it throws the following exception:

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)

I'm not sure if it has something to do with how I use the client API. I use other Google APIs where some of them use GoogleSignInAccount, so I extract the access token from the signed in account:

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);
}

The last line is where the exception is raised.


Solution

  • The problem is "solvable" by adding the following to your gradle.build file:

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

    To prevent version clashes with the other gprc libraries included by the Google Ads API library, you should check the versions first by executing gradlew app:dependencies (in the Terminal view of Android Studio) and search for other libraries (e.g. io.grpc:grpc-core). Use the same version instead of mine (so replace 1.37.0 with the appropriate version).

    I already reported a bug.

    If you experience a build error like that

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

    just add

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

    to the packagingOptions of your gradle.build file (and replace the name with whatever file is in your error message).