Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot import Google client after updating Android Studio

After updating to Android Studio 2.3 it appears I can not use the code for the Cloud Endpoints

class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void, String> {
    private static MyApi myApiService = null;
    private Context context;

    @Override
    protected String doInBackground(Pair<Context, String>... params) {
        if(myApiService == null) {  // Only do this once
            MyApi.Builder builder = new MyApi.Builder(AndroidHttp.newCompatibleTransport(),
                    new AndroidJsonFactory(), null)
                // options for running against local devappserver
                // - 10.0.2.2 is localhost's IP address in Android emulator
                // - turn off compression when running against local devappserver
                .setRootUrl("http://10.0.2.2:8080/_ah/api/")
                .setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
                    @Override
                    public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
                        abstractGoogleClientRequest.setDisableGZipContent(true);
                    }
                });
                // end options for devappserver

            myApiService = builder.build();
        }

        context = params[0].first;
        String name = params[0].second;

        try {
            return myApiService.sayHi(name).execute().getData();
        } catch (IOException e) {
            return e.getMessage();
        }
    }

    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
}

The problem arises with importing AndroidHttp and AndroidJsonFactory. It used to work with the click-alt-enter import, not now. I can copy the import manually

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;

but it highlights 'client', cannot resolve symbol client. The gradle file has the dependency compile project(path: ':backend', configuration: 'android-endpoints') and I have synched.

Rather than trying to rollback versions, is there a different import we need to use now or configure differently? Is the code on the Google Cloud page now outdated?

like image 979
tagliatelli Avatar asked Mar 28 '17 15:03

tagliatelli


1 Answers

I added these dependencies in the endpoints build.gradle file:

compile group: 'com.google.api-client', name: 'google-api-client', version: '1.22.0'
compile group: 'com.google.api-client', name: 'google-api-client-android', version: '1.22.0'

I don't know why they suddenly are needed, but I solved it by adding these dependencies to the missing packages.

like image 123
Johan Naslund Avatar answered Nov 20 '22 17:11

Johan Naslund