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

Does not work with android plugin #11

Closed
ghost opened this issue May 13, 2015 · 12 comments
Closed

Does not work with android plugin #11

ghost opened this issue May 13, 2015 · 12 comments
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented May 13, 2015

This plugin does not work with the android plugin for gradle, since it applies the java plugin.

@ejona86
Copy link
Collaborator

ejona86 commented May 13, 2015

Try adding remove java within the builtins section.

@AstralStorm
Copy link

Same issue, that remove java is not the problem nor fixes it.

The problem is here: https://github.com/google/protobuf-gradle-plugin/blob/master/src/main/groovy/com/google/protobuf/gradle/ProtobufPlugin.groovy#L70

This protobuf plugin just does not support android plugin.

@zhangkun83 zhangkun83 self-assigned this May 18, 2015
@zhangkun83 zhangkun83 reopened this May 20, 2015
@zhangkun83
Copy link
Collaborator

The problem with the android plugin is more than just that it cannot be applied when the java plugin has already been applied. The android project is structured differently from a java project.

Here I managed to get the nano protos compiled in an android project. It doesn't look very nice. The Android plugin uses a sourceSet inside the android block, not on the top-level. Ideally I should insert the proto convention to the android sourceSet, but I haven't figured out how.

@zhangkun83 zhangkun83 added this to the 0.5.0 milestone May 20, 2015
@zhangkun83
Copy link
Collaborator

One more android plugin quirk gets in my way.

I tried to have the protobuf plugin to call android.sourceSets.<sourceSet>.java.srcDir to add generated sources, just like what we have been doing with java plugin. This can only be done in the project.afterEvaluate hook because only at that point have the proto source sets been evaluated. However, it just doesn't work. It seems the android plugin only reads android.sourceSets BEFORE this point. After that, any changes to android.sourceSets will have no effect.

An experiment to illustrate the problem. In ProtobufPlugin.apply(), which is run when the user does apply plugin: 'com.google.protobuf', if I add the line below, files under build/generated-sources/main will be compiled.

project.android.sourceSets.main.java.srcDir("build/generated-sources/main")

If I do this instead, they won't:

project.afterEvaluate {
  project.android.sourceSets.main.java.srcDir("build/generated-sources/main")
}

This makes it impossible to continue specifying proto files and options inside sourceSets and make it work with android plugin. I am thinking of using our own conventions/functions to do this, which is more flexible, e.g., we can then change android.sourceSets at evaluation time, then the changes can be picked up by android plugin when it compiles the sources.

@zhangkun83
Copy link
Collaborator

Anyway, although putting everything in sourceSets along with the java sources looks very nice from user's aspect, I have been uncomfortable with the fact that the implementation uses Gradle internal API and undocumented internal features. It is very fragile.

@AstralStorm
Copy link

What about com.android.library?

@zhangkun83
Copy link
Collaborator

So, this was unintentionally closed. I have checked in a comprehensive solution for Android, which should work for both library and application. It will be released as 0.5.0. Soon I will create a release branch for it, and update README in there.

@zhangkun83 zhangkun83 reopened this Jun 26, 2015
@zhangkun83
Copy link
Collaborator

I have created the v0.5.x branch. Please follow its README and try it out. You may want to refer to testProjectAndroid as a working example for Android.

@madongfly

@mnvl
Copy link

mnvl commented Jul 5, 2015

I had manually add dependency to protobuf-nano:

dependencies {
    compile 'com.google.protobuf.nano:protobuf-javanano:3.0.0-alpha-3.1'
}

@mnvl
Copy link

mnvl commented Jul 5, 2015

It would be nice to be able to use javanano generated code with java projects and java generated code with android projects though. If I add

        all()*.builtins {
            java {
            }
        }

then it ends up generating both java and javanano classes and compilation fails because it finds duplicate classes.

@zhangkun83
Copy link
Collaborator

@manvelavetisian The java builtin is added automatically in Java projects, and so is javanano in Android projects. If you want java builtin instead in an Android project, you need to remove javanano explicitly:

all()*.builtins {
  remove javanano
  java {
  }
}

@zhangkun83
Copy link
Collaborator

0.5.0 has been released. Please refer to the instructions from the master README.

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

4 participants