google/protobuf-gradle-plugin

Issue compiling protos...

cyboy22 opened this issue · 4 comments

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)

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'

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.

  • What went wrong:
    Execution failed for task ':app:generateDebugProto'.

Could not resolve all files for configuration ':app:protobufToolsLocator_javalite'.
Could not resolve com.google.protobuf:protoc-gen-javalite:3.25.3.
Required by:
project :app
> Could not resolve com.google.protobuf:protoc-gen-javalite:3.25.3.
> Could not get resource 'https://mvnrepository.com/artifact/com.google.protobuf/protobuf-javalite/com/google/protobuf/protoc-gen-javalite/3.25.3/protoc-gen-javalite-3.25.3.pom'.
> Could not GET 'https://mvnrepository.com/artifact/com.google.protobuf/protobuf-javalite/com/google/protobuf/protoc-gen-javalite/3.25.3/protoc-gen-javalite-3.25.3.pom'. Received status code 403 from server: Forbidden

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.

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.

Revised gradle build output...

Task :app:generateDebugProto FAILED

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:generateDebugProto'.

Could not resolve all files for configuration ':app:protobufToolsLocator_javalite'.
Could not find com.google.protobuf:protoc-gen-javalite:3.25.3.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/com/google/protobuf/protoc-gen-javalite/3.25.3/protoc-gen-javalite-3.25.3.pom
- https://repo.maven.apache.org/maven2/com/google/protobuf/protoc-gen-javalite/3.25.3/protoc-gen-javalite-3.25.3.pom
Required by:
project :app

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.

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

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