You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android调用Google Cloud Endpoint库遇编译错误及依赖优化咨询

Fixing Google Cloud Endpoints Compilation Errors & Minimizing Dependencies

Hey there, let's break down exactly what's going wrong here and walk through the fixes step by step.

Why You're Seeing These Errors

1. Java 8 Feature Compatibility Issue

The core problem is your use of guava-22.0.jar. This version of Guava uses Java 8 default interface methods (like BiMap.values() and Predicate.test()), but Android doesn't natively support these features until API 24 (Android 7.0/Nougat). Since your project's minSdkVersion is likely set lower than 24, the D8 compiler throws these errors when trying to process the incompatible code.

2. Unnecessary Dependencies

You mentioned your API doesn't require authentication, which means you're probably carrying extra dependencies related to OAuth, Google account auth, and full Guava functionality that you don't need—these are just adding bloat and causing conflicts.

Step-by-Step Solutions

1. Fix the Java 8 Compilation Error

You have two options here, depending on whether you need to support devices below API 24:

Option A: Enable Java 8 Compilation Support (Recommended)

Even if your minSdkVersion is below 24, you can configure Android Studio to compile Java 8 code and automatically handle compatibility for older devices. Add this to your app module's build.gradle file:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

This tells the D8 compiler to desugar Java 8 features so they work on older Android versions.

Option B: Downgrade to an Android-Friendly Guava Version

If you don't want to enable Java 8 support, replace your current Guava dependency with an Android-optimized build that avoids incompatible Java 8 features. Use this in your build.gradle:

// Replace the standard guava dependency with this
implementation 'com.google.guava:guava-android:28.2-android'

2. Minimize Dependencies (No Authentication Needed)

Since your API doesn't require authentication, strip out all unnecessary libraries. Here's a minimal dependency set for Google Cloud Endpoints:

// Only the core Endpoints client library
implementation 'com.google.endpoints:endpoints-android-client:2.0.1'
// Android-compatible Guava (from Option B above, or use Java 8 support with standard Guava)
implementation 'com.google.guava:guava-android:28.2-android'

Remove any dependencies related to:

  • OAuth (e.g., com.google.oauth-client:google-oauth-client-android)
  • Google Play Services Auth (e.g., com.google.android.gms:play-services-auth)
  • Any other auth-related or heavyweight libraries you copied over but don't need

3. Final Cleanup

  • Clear your Gradle cache to avoid leftover conflicting files: Run File > Invalidate Caches... in Android Studio, then restart. Alternatively, run ./gradlew clean build in your project's root directory via terminal.
  • Double-check your minSdkVersion in build.gradle is set to your desired minimum API level.

Once you've made these changes, your empty MainActivity should compile and run without errors, and you can start adding your Endpoint API calls.

内容的提问来源于stack exchange,提问作者Azeem Haider

火山引擎 最新活动