在Android中使用Google客户端库

时间:2018-07-01 11:22:57

标签: android google-api-client android-googleapiclient

我添加了一些代码,以在People Api登录后使用GoogleSignInOptions从People Api获取用户个人资料信息。这是代码:

private void getProfileData(final Context context, GoogleSignInAccount account, final ProfileDataListener listener) {
    ProfileDataTask profileDataTask = new ProfileDataTask(context, account, listener);
    profileDataTask.execute();
}

private static class ProfileDataTask extends AsyncTask<GoogleSignInAccount, Void, Person> {

    private GoogleAccountCredential credential;
    private String appName;
    private ProfileDataListener listener;

    ProfileDataTask(Context context, GoogleSignInAccount account, ProfileDataListener listener) {
        this.credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(Scopes.PROFILE));
        this.credential.setSelectedAccount(new Account(account.getEmail(), "com.google"));

        this.appName = context.getString(R.string.app_name);
        this.listener = listener;
    }

    @Override
    protected Person doInBackground(GoogleSignInAccount... accounts) {
        PeopleService service = new PeopleService.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
                .setApplicationName(appName)
                .build();
        try {
            return service.people()
                    .get("people/me")
                    .setPersonFields("genders,birthdays")
                    .execute();
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    protected void onPostExecute(Person person) {
        super.onPostExecute(person);

        if (listener != null) {
            listener.onCompleted(person);
        }
    }
}

这非常简单(尽管缺少这些api的文档),并且一切正常。

Gradle依赖项是:

implementation 'com.google.api-client:google-api-client-android:1.23.0'
implementation 'com.google.apis:google-api-services-people:v1-rev299-1.23.0'

当我在使用proguard的'release'构建类型中进行im构建时,发生了问题。我的构建因以下原因而失败:

  

警告:org.apache.commons.logging.impl.ServletContextCleaner:找不到超类或接口javax.servlet.ServletContextListener   警告:org.apache.commons.logging.impl.AvalonLogger:找不到引用的类org.apache.avalon.framework.logger.Logger   警告:org.apache.commons.logging.impl.AvalonLogger:找不到引用的类org.apache.avalon.framework.logger.Logger   ...警告:org.apache.commons.logging.impl.AvalonLogger:找不到引用的类org.apache.avalon.framework.logger.Logger   警告:org.apache.commons.logging.impl.Log4JLogger:找不到引用的类org.apache.log4j.Category   警告:org.apache.commons.logging.impl.Log4JLogger:找不到引用的类org.apache.log4j.Category   警告:org.apache.commons.logging.impl.Log4JLogger:找不到引用的类org.apache.log4j.Priority   警告:org.apache.commons.logging.impl.Log4JLogger:找不到引用的类org.apache.log4j.Priority   警告:org.apache.commons.logging.impl.Log4JLogger:找不到引用的类org.apache.log4j.Priority

当我运行./gradlew app:dependencies时,我看到以下内容:

enter image description here

似乎google-api-client-android库取决于不适合android使用的更通用的google-api-client库。并随后包含非android组件。它是否正确?我该如何解决?谢谢。

1 个答案:

答案 0 :(得分:1)

好的,终于找到了解决方案。 我将Google Client API库gradle依赖项更改为此:

implementation('com.google.api-client:google-api-client-android:1.23.0') {
    // Exclude artifacts that the Android SDK/Runtime provides.
    exclude(group: 'xpp3', module: 'xpp3')
    exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
    exclude(group: 'junit', module: 'junit')
    exclude(group: 'com.google.android', module: 'android')
}
implementation ('com.google.apis:google-api-services-people:v1-rev299-1.23.0') {
    // Exclude artifacts that the Android SDK/Runtime provides.
    exclude(group: 'xpp3', module: 'xpp3')
    exclude(group: 'org.apache.httpcomponents', module: 'httpclient')
    exclude(group: 'junit', module: 'junit')
    exclude(group: 'com.google.android', module: 'android')
}

关于从android使用该库的Google文档太糟糕了...