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

proto import statement from Android project to Android project broken? #56

Closed
mdusina opened this issue Feb 7, 2016 · 4 comments
Closed

Comments

@mdusina
Copy link

mdusina commented Feb 7, 2016

I accidentally opened this in the old repository.

I have two Android libraries, both of which have protos. ProjectB depends on ProjectA -- compile project(':projectA'). The only non-standard thing I've done is to disable nano protos and use the normal style protos (reverting that doesn't solve my problem).

Both protos can compile just fine when they're independent. However, if I add an import statement within protoB.proto to protoA.proto, I get an error:
protoA.proto: File not found.
protoB.proto: Import "protoA.proto" was not found or had errors.

I think this issue is similar to #22 but in my case, both projects are Android instead of plain Java. Should this work?

https://github.com/google/protobuf-gradle-plugin/blob/master/testProjectAndroid/build.gradle shows an Android app depending on protos defined in another Java project. If that works, shouldn't it still work when the other project is an Android project?

Should I move all protos into their own Java projects and have the Android projects depend on that instead?

Details:
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.1'
proto_library_version = '3.0.0-beta-1'
google_proto_library = "com.google.protobuf:protobuf-java:${proto_library_version}"
google_protoc_artifact = "com.google.protobuf:protoc:${proto_library_version}"


I just confirmed my theory by converting ProjectA to a Java project (ProjectB is still an Android library). All of the sudden, protoB.proto is allowed to import a proto from protoA.proto.

In my case, I think I can just convert both of them to Java projects and be done with it. I made them Android library projects even though they only contain regular Java (there was some limitation with Android and eclipse long ago). But it still seems like this shouldn't be a restriction.

@zhangkun83
Copy link
Collaborator

Sorry for taking so long. This is a known missing feature, which is marked as a TODO in the source code. I didn't do it partially because the doubt of whether this feature would be needed, and that I was not familiar with Android development thus I was not sure I could do it right.

@zhangkun83
Copy link
Collaborator

I am trying to figure out how to include protos in the artifact of a Android library project. In Java project I can just include the files in the processResources task, but it doesn't work for Android project. The process*Resources tasks in Android is different from the ones in Java.
@mdusina any ideas?

@AseevEIDev
Copy link

AseevEIDev commented Jan 23, 2018

My way of solving problem with import proto files from android library (project).

If android library is local and connected this way:

compile project(':androidlibrary')

I didn't find the way to put proto files in .aar (android library package). But I created external only java library with only proto files and connect it to our android library:

compile project(':androidlibraryproto')

This way all protobuf files remain in jar file of androidLibraryProto library within .aar and you can successfully import proto files from you App.

androidlibraryproto.gradle
apply plugin: 'java'
apply plugin: 'com.google.protobuf'

repositories {
    maven { url "https://plugins.gradle.org/m2/" }
}

configurations {
    grpcCompile
}

sourceSets {
    grpc {
        compileClasspath += configurations.grpcCompile
    }
    test {
        compileClasspath += grpc.output
        runtimeClasspath += grpc.output
    }
    main {
        java {
            srcDirs 'build/generated/source/proto/main/grpc'
            srcDirs 'build/generated/source/proto/main/java'
        }
    }
}

dependencies {
    compile 'com.google.protobuf:protobuf-java:3.2.0'
    grpcCompile 'io.grpc:grpc-stub:1.0.0-pre2'
    grpcCompile 'io.grpc:grpc-protobuf:1.0.0-pre2'
    compile 'io.grpc:grpc-okhttp:1.0.3'
    compile 'io.grpc:grpc-protobuf:1.0.3'
    compile 'io.grpc:grpc-stub:1.0.3'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
        }
    }
    generateProtoTasks {
        ofSourceSet('grpc').each { task ->
            task.plugins {
                grpc {
                    outputSubDir = 'grpc_output'
                }
            }
            task.generateDescriptorSet = true
        }
    }
}

project.afterEvaluate { compileJava.dependsOn generateSources }

jar {
    sourceSets.all { sourceSet ->
        from sourceSet.output
        dependsOn sourceSet.getCompileTaskName('java')
    }
}
androidlibrary.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.google.protobuf'

android {...}

dependencies {
    compile project(':androidlibraryproto')
    ...
}
app.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'

android {...}

dependencies {
    compile project(':androidlibrary')
    ...
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.0.0'
    }
    plugins {
        grpc {
            artifact = 'io.grpc:protoc-gen-grpc-java:1.0.3'
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                java {}
            }
            task.plugins {
                grpc {}
            }
            generateSources.dependsOn task
        }
    }
}

When we decided to upload android library to JCenter and use this way:

compile "com.github.username:androidlibrary:0.0.1"

This way we can't leave compile project(':androidlibraryproto') in dependencies, because when we'll compile libarary from JCenter then an error will occur - can't get androidlibraryproto library.

We have to ways:

  • get Jar package from androidlibraryproto, copy it to androidlibrary/libs, compile this way: compile files("libs/andoridlibraryproto") and upload it to JCenter.
    This way unreal now, because proto files not extracted from .aar package. I think for using this way you should add .aar to this line
    } else if (file.path.endsWith('.jar') || file.path.endsWith('.zip')) {
  • upload androidlibraryproto to JCenter and connect to androidlibrary and App this way compile "com.github.username:androidlibraryproto:0.0.1"
    Then we can compile our android library in app.gradle this way:
protobuf ("com.github.username:androidlibraryproto:0.0.1") {
    transitive = false // To avoid duplicate zip entry error when minifyEnabled
}
compile ("com.github.username:androidlibrary:0.0.1") {
    transitive = false // To avoid duplicate zip entry error when minifyEnabled
}

Now I'm using last way, I uploaded proto files to JCenter separately and compile it in app.grade and androidlibrary.gradle

@ejona86
Copy link
Collaborator

ejona86 commented Nov 5, 2022

Seems this was probably fixed by #440.

@ejona86 ejona86 closed this as completed Nov 5, 2022
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