Git Product home page Git Product logo

Comments (4)

zhangkun83 avatar zhangkun83 commented on July 28, 2024 1

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?

from protobuf-gradle-plugin.

zhangkun83 avatar zhangkun83 commented on July 28, 2024

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.

from protobuf-gradle-plugin.

AseevEIDev avatar AseevEIDev commented on July 28, 2024

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

from protobuf-gradle-plugin.

ejona86 avatar ejona86 commented on July 28, 2024

Seems this was probably fixed by #440.

from protobuf-gradle-plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.