google / protobuf-gradle-plugin

Protobuf Plugin for Gradle
Other
1.76k stars 274 forks source link

Issue compiling protos... #758

Open cyboy22 opened 2 months ago

cyboy22 commented 2 months ago

Hi, I am trying to build a simple android app that uses protocol buffers (and will try to move the protocol buffer code into an android library in the same project thereafter if successful). Here is my build.gradle...

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    id "com.google.protobuf" version "0.9.4"
}

android {
    namespace 'choice.dimension.new_protobuf_app'
    compileSdk 34

    defaultConfig {
        applicationId "choice.dimension.new_protobuf_app"
        minSdk 24
        targetSdk 34
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary true
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    buildFeatures {
        compose true
    }
    composeOptions {
        kotlinCompilerExtensionVersion '1.5.1'
    }
    packaging {
        resources {
            excludes += '/META-INF/{AL2.0,LGPL2.1}'
        }
    }
}

dependencies {

    implementation libs.androidx.core.ktx
    implementation libs.androidx.lifecycle.runtime.ktx
    implementation libs.androidx.activity.compose
    implementation platform(libs.androidx.compose.bom)
    implementation libs.androidx.ui
    implementation libs.androidx.ui.graphics
    implementation libs.androidx.ui.tooling.preview
    implementation libs.androidx.material3
    testImplementation libs.junit
    androidTestImplementation libs.androidx.junit
    androidTestImplementation libs.androidx.espresso.core
    androidTestImplementation platform(libs.androidx.compose.bom)
    androidTestImplementation libs.androidx.ui.test.junit4
    debugImplementation libs.androidx.ui.tooling
    debugImplementation libs.androidx.ui.test.manifest
    api libs.protobuf.javalite
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        javalite {
            artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                javalite { }
            }
        }
    }
}

and here is the output...

> Task :app:compileDebugJavaWithJavac FAILED
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:181: error: memoizedSerializedSize has private access in GeneratedMessageLite
      int size = memoizedSerializedSize;
                 ^
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:197: error: memoizedSerializedSize has private access in GeneratedMessageLite
      memoizedSerializedSize = size;
      ^
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:395: error: an enum switch case label must be the unqualified name of an enumeration constant
        case IS_INITIALIZED: {
             ^
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:398: error: an enum switch case label must be the unqualified name of an enumeration constant
        case MAKE_IMMUTABLE: {
             ^
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:404: error: an enum switch case label must be the unqualified name of an enumeration constant
        case VISIT: {
             ^
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:405: error: cannot find symbol
          Visitor visitor = (Visitor) arg0;
          ^
  symbol:   class Visitor
  location: class Person
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:405: error: cannot find symbol
          Visitor visitor = (Visitor) arg0;
                             ^
  symbol:   class Visitor
  location: class Person
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:413: error: cannot find symbol
          if (visitor == com.google.protobuf.GeneratedMessageLite.MergeFromVisitor
                                                                 ^
  symbol:   variable MergeFromVisitor
  location: class GeneratedMessageLite
/home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java:418: error: an enum switch case label must be the unqualified name of an enumeration constant
        case MERGE_FROM_STREAM: {
             ^
Note: /home/cyril/AndroidStudioProjects/new_protobuf_app/app/build/generated/source/proto/debug/javalite/tutorial/PersonOuterClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
9 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

* Try:
> Run with --info option to get more log output.
> Run with --scan to get full insights.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 1s
20 actionable tasks: 1 executed, 19 up-to-date

I would be grateful for any suggestions as to where I might be going wrong (if I have tried to do a similar thing with the kotlin DSL but had no luck) or let me know if you need any more information (gradle version is 8.7)

ejona86 commented 2 months ago

That version of protobuf is from 2016. Use a newer version (e.g., 3.25.3)

   artifact = 'com.google.protobuf:protoc:3.0.0'
       artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
cyboy22 commented 2 months ago

Hi - I modified my build.gradle as below...

plugins { alias(libs.plugins.android.application) alias(libs.plugins.jetbrains.kotlin.android) id "com.google.protobuf" version "0.9.4" }

android { namespace 'choice.dimension.new_protobuf_app' compileSdk 34

defaultConfig {
    applicationId "choice.dimension.new_protobuf_app"
    minSdk 24
    targetSdk 34
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    vectorDrawables {
        useSupportLibrary true
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion '1.5.1'
}
packaging {
    resources {
        excludes += '/META-INF/{AL2.0,LGPL2.1}'
    }
}

}

dependencies {

implementation libs.androidx.core.ktx
implementation libs.androidx.lifecycle.runtime.ktx
implementation libs.androidx.activity.compose
implementation platform(libs.androidx.compose.bom)
implementation libs.androidx.ui
implementation libs.androidx.ui.graphics
implementation libs.androidx.ui.tooling.preview
implementation libs.androidx.material3
testImplementation libs.junit
androidTestImplementation libs.androidx.junit
androidTestImplementation libs.androidx.espresso.core
androidTestImplementation platform(libs.androidx.compose.bom)
androidTestImplementation libs.androidx.ui.test.junit4
debugImplementation libs.androidx.ui.tooling
debugImplementation libs.androidx.ui.test.manifest
//api libs.protobuf.javalite
implementation 'com.google.protobuf:protoc:3.25.3'
implementation 'com.google.protobuf:protobuf-javalite:3.25.3'

}

protobuf { protoc { artifact = 'com.google.protobuf:protoc:3.25.3' } plugins { javalite { artifact = 'com.google.protobuf:protoc-gen-javalite:3.25.3' } } generateProtoTasks { all().each { task -> task.builtins { remove java } task.plugins { javalite { } } } } }

but am still receiving the following when running ./gradlew build ...

Task :app:generateDebugProto FAILED

FAILURE: Build failed with an exception.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 966ms 4 actionable tasks: 1 executed, 3 up-to-date

I fear I am misunderstanding something as I am fairly new the plugin.

cyboy22 commented 2 months ago

Revised gradle build output...

Task :app:generateDebugProto FAILED

FAILURE: Build failed with an exception.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/8.7/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 862ms 4 actionable tasks: 1 executed, 3 up-to-date

ejona86 commented 2 months ago

You don't need protoc-gen-javalite, it is a builtin in later versions of protobuf.