Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proto2 and javalite #92

Closed
mdusina opened this issue Aug 13, 2016 · 5 comments
Closed

proto2 and javalite #92

mdusina opened this issue Aug 13, 2016 · 5 comments

Comments

@mdusina
Copy link

mdusina commented Aug 13, 2016

Similar to #89 , I'm having issues upgrading from protobuf 3.0.0-beta-3 to 3.0.0 because of the javalite split. I'm wondering if proto2 lite API is even supported anymore? The protobuf release notes for 3.0.0 specifically calls out the Lite runtime, but only mentions proto3 syntax.

General project structure:
Protos are in java projects
An android application project depends on them and uses the generated proto classes
Protos have proto2 syntax and use "option optimize_for = LITE_RUNTIME"
com.google.protobuf:protobuf-gradle-plugin:0.7.7
com.android.tools.build:gradle:2.1.2
gradle-2.14.1

Before the upgrade, this was all that was needed to get the lite api:

apply plugin: 'java'
apply plugin: 'com.google.protobuf'
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0-beta-3'
    }
}
dependencies {
    compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'
}

After the upgrade, the android project always fails because it can't find the symbols for the generated proto classes. I've tried several combinations of options, none of which seem to produce the symbols.

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 { }
            }
        }
    }
}

If I omit the "remove java" portion, it complains about duplicate classes (which is expected). But when "remove java" is included, there's 0 classes...

I also tried compiling com.google.protobuf:protobuf-lite:3.0.1 instead of com.google.protobuf:protobuf-java:3.0.0 just in case that was the root cause (it wasn't).

If I upgrade to 3.0.0, and remove the LITE_RUNTIME option from the protos, everything works just fine (and my apk grows in size).

@zhangkun83
Copy link
Collaborator

@mdusina I have just added testProjectLite that verifies javalite does generate files.

@xfxyjwf it seems the javalite plugin is not generating files for him. Is it because his use of proto2 syntax?

@xfxyjwf
Copy link

xfxyjwf commented Aug 15, 2016

I don't think proto2 syntax is a problem. The lite plugin should work with both proto2 and proto3 syntax.

@zhangkun83
Copy link
Collaborator

If I omit the "remove java" portion, it complains about duplicate classes (which is expected). But when "remove java" is included, there's 0 classes...

This is interesting. When it complains about duplicate classes, can you check if the lite classes are generated?

@mdusina
Copy link
Author

mdusina commented Aug 21, 2016

(Apologies for the delay, this is a personal project where I'm trying to teach myself Android Studio, Android development, and gradle all at once).

I think I know what happened. The builder API for maps seems to have changed in 3.0.0. Instead of proto.getMutableFoo().put(1, 2), it's now proto.putFoo(1, 2) (which, admittedly, is a much nicer way of working with map builders). For some reason, Android Studio's incremental compiler marks every single proto reference as an error if there's any other error after a clean build (which is what I did after upgrading to 3.0.0). This was further obscured because the canary version of Android Studio seems to have a legitimate problem reading the generated javalite classes while doing incremental compiles for syntax error highlighting (the AS 2.1.2 has no problem with it).

When I switch back to Android Studio 2.1.2 and fix all map builder calls, both the real compiler and Android Studio's incremental compiler are happy.


Answers to your questions (just FYI):

When omitting remove java, I see both:
build\generated\source\proto\main\java\com{package}\ProtoPb.java
build\generated\source\proto\main\javalite\com{package}\ProtoPb.java
:generateProto completes and :compileJava fails with the duplicate class error (as it should). There aren't any generated class files.

When I include remove java, I only see:
build\generated\source\proto\main\javalite\com{package}\ProtoPb.java
build\classes\main\com{package}\ProtoPb$1.class

@mdusina mdusina closed this as completed Aug 21, 2016
@mdusina
Copy link
Author

mdusina commented Sep 22, 2016

FYI

the canary version of Android Studio seems to have a legitimate problem reading the generated javalite classes while doing incremental compiles for syntax error highlighting (AS 2.1.2 has no problem with it).

This appears to be true for the new release to the stable channel -- Android Studio 2.2. I upgraded yesterday and can't get Android Studio to recognize any of the proto imports (in Java classes). I can still run tests and deploy apk's just fine. The IDE insists the symbols for the generated proto classes don't exist though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants