Java开发Android应用读写Google Sheets报ClassNotFoundException咨询
问题背景
开发支持Google Sheets读写能力的Android应用时,参照标准Java环境的Google Sheets API快速入门指南完成配置后,运行时抛出类找不到的崩溃,核心错误堆栈如下:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sun.net.httpserver.HttpServer" on path: DexPathList[[dex file "/data/data/cn.leo.zxingview/code_cache/.overlay/base.apk/classes7.dex", zip file "/data/app/~~3LMO7u4xh20M6CUbBAGxyw==/cn.leo.zxingview-TgsY52whz0D3_gD6p0ZalA==/base.apk"],nativeLibraryDirectories=[/data/app/~~3LMO7u4xh20M6CUbBAGxyw==/cn.leo.zxingview-TgsY52whz0D3_gD6p0ZalA==/lib/arm64, /system/lib64, /system/system_ext/lib64, /product/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:127)
当前模块的build.gradle依赖配置如下:
dependencies { implementation 'com.google.oauth-client:google-oauth-client-jetty:1.32.1' implementation 'com.google.auth:google-auth-library-oauth2-http:0.26.0' implementation 'com.google.apis:google-api-services-sheets:v4-rev20210629-1.32.1' implementation 'com.google.apis:google-api-services-drive:v3-rev20211107-1.32.1' implementation 'com.google.android.gms:play-services-auth:15.0.1' implementation 'pub.devrel:easypermissions:0.3.0' implementation('com.google.api-client:google-api-client-android:1.33.0') { exclude group: 'org.apache.httpcomponents' } }
问题根因
崩溃的核心原因是依赖混用了桌面端Java组件:
- 引入的
com.google.oauth-client:google-oauth-client-jetty是为标准JVM桌面/服务端环境设计的授权组件,它内部通过LocalServerReceiver启动本地Jetty HTTP服务监听OAuth授权回调,这个逻辑依赖标准JDK自带的com.sun.net.httpserver.HttpServer类。 - Android运行时(ART/Dalvik)没有实现
com.sun.net包下的服务端类,运行时加载到该类时直接抛出ClassNotFoundException。
解决步骤
- 移除桌面端专属依赖:直接删除dependencies中的
implementation 'com.google.oauth-client:google-oauth-client-jetty:1.32.1'配置,该组件在Android环境无适配可能,完全无法使用。 - 替换授权逻辑:放弃快速入门中
LocalServerReceiver监听本地端口的桌面端授权方案,使用已经引入的Google Play Services授权组件完成OAuth流程:- 用
GoogleSignInClient申请Sheets、Drive对应的访问权限scope - 用户授权成功后,通过返回的账号对象构建
GoogleAccountCredential凭证 - 用Android兼容的HTTP传输类初始化Sheets客户端,不要用桌面端的
NetHttpTransport
核心实现参考:
// 配置登录选项,声明需要的API权限 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(SheetsScopes.SPREADSHEETS), new Scope(DriveScopes.DRIVE_FILE)) .requestEmail() .build(); GoogleSignInClient signInClient = GoogleSignIn.getClient(activity, gso); // 登录授权成功后,构建API访问凭证 GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( context, Arrays.asList(SheetsScopes.SPREADSHEETS, DriveScopes.DRIVE_FILE) ).setSelectedAccount(authorizedAccount); // 初始化Sheets服务客户端 Sheets sheetsService = new Sheets.Builder( AndroidHttp.newCompatibleTransport(), GsonFactory.getDefaultInstance(), credential ).setApplicationName("你的应用名称").build(); - 用
- 对齐依赖版本:当前配置中
google-api-client-android版本为1.33.0,其余Sheets、Drive API依赖版本为1.32.1,需要将所有Google API相关依赖的版本号对齐,避免运行时出现类方法不匹配的异常。 - 不要直接照搬标准Java快速入门中的
AuthorizationCodeInstalledApp相关授权代码,该类仅适用于桌面端环境,Android环境下没有可用的本地浏览器回调到本地端口的能力,必须走Play Services的系统级授权流程。
内容的提问来源于stack exchange,提问作者Flochocinco
相关产品推荐
相关产品推荐

