GRPC从1.25.0更新到1.34.1后找不到符号错误



如果我使用旧版本,Protobuf可以成功生成protos,但是当我将Protobuf依赖更新到最新的1.34.1时,我总是面临这个问题。

如果我使用旧版本,Protobuf可以成功生成protos,但是当我将Protobuf依赖更新到最新的1.34.1时,我总是遇到这个问题。

误差

C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:337: error: an enum switch case label must be the unqualified name of an enumeration constant
        case IS_INITIALIZED: {
             ^
C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:340: error: an enum switch case label must be the unqualified name of an enumeration constant
        case MAKE_IMMUTABLE: {
             ^
C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:346: error: an enum switch case label must be the unqualified name of an enumeration constant
        case VISIT: {
             ^
C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:347: error: cannot find symbol
          Visitor visitor = (Visitor) arg0;
          ^
  symbol:   class Visitor
  location: class LoginRequest
C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:347: error: cannot find symbol
          Visitor visitor = (Visitor) arg0;
                             ^
  symbol:   class Visitor
  location: class LoginRequest
C:Users1AndroidStudioProjectscapella-client-androiddappbuildgeneratedsourceprotodebugjavaliteapiAbonentOuterClass.java:353: error: cannot find symbol
          if (visitor == com.google.protobuf.GeneratedMessageLite.MergeFromVisitor
                                                                 ^
  symbol:   variable MergeFromVisitor
  location: class GeneratedMessageLite

我的gradle模块

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'com.google.protobuf'
    id 'kotlin-android-extensions'
    id 'kotlin-kapt'
}
 
protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.5.1-1' }
    plugins {
        javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.34.1' // CURRENT_GRPC_VERSION
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins{
                remove java
            }
            task.plugins {
                javalite {                }
                grpc { // Options added to --grpc_out
                    option 'lite' }
            }
        }
    }
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
    defaultConfig {
        applicationId "com.glonass.glonassmobile"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
dependencies {
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
    def nav_version = "2.3.2"
    def fragment_version = "1.3.0-rc01"
    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation 'androidx.core:core-ktx:1.3.2'
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    //For using fragment result listener
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    implementation "androidx.fragment:fragment-ktx:$fragment_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation 'io.grpc:grpc-okhttp:1.34.1' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-protobuf-lite:1.34.1' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-stub:1.34.1' // CURRENT_GRPC_VERSION
    implementation 'io.grpc:grpc-testing:1.34.1' // CURRENT_GRPC_VERSION
    implementation 'javax.annotation:javax.annotation-api:1.3.2'
    
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.1'
}

My build gradle project

buildscript {
    ext.kotlin_version = "1.3.72"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.14'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

Java Lite运行时不保证API/ABI稳定。这包括生成的代码和运行时之间的接口。因此,您必须使用与protobuf-javalite jar相同的protoc-gen-javalite版本(在本例中,可能是3.12.0)。

我看到你在3.0.0版本使用protoc-gen-javalite:

    javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }

Java Lite代码生成与protoc合并为内置,因此您不再使用插件语法。

protobuf {
    protoc { artifact = 'com.google.protobuf:protoc:3.12.0' }
    plugins {
        grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.34.1' }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins{
                java { option 'lite' }
            }
            task.plugins {
                grpc { option 'lite' }
            }
        }
    }
}

最新更新