Need wire to emit interfaces for client and server in same gradle project

198 views Asked by At

I want to use interfaces for both client and server in the same android app. Usecase is to run a okhttpmockwebserver serving gRPC requests within the same app the client is running in. For this i created two library projects with their own wire configuration for client and server similar to those

    wire {
        kotlin {
            includes = ['com..caompany.android.proto.*']
            out "${buildDir}/protos"
            rpcCallStyle = 'suspending'
            rpcRole = 'client'
        }
    }
    wire {
        kotlin {
            includes = ['com..company.android.proto.*']
            out "${buildDir}/protos"
            rpcCallStyle = 'suspending'
            rpcRole = 'server'
        }
    }

Executing the wire-gradle-plugin fails with this exception: com.company.android.proto.HelloReply$Companion$ADAPTER$1 is defined multiple times. Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete, origin: .../com/company/android/proto/HelloReply$Companion$ADAPTER$1.dex

It would help me if wire could either

  • Generate all classes and interfaces at once including server and client role or
  • Exclude the generation of class files, only generating service interfaces for client or server

Is there a workaround i can achieve a similar result without gradle plugin support?

1

There are 1 answers

0
oldergod On

You can have multiple kotlin blocks at the same time. Wire will throw if you generate the same class twice so you need to define the rule as unique between both. You need one block which will generate client role interfaces. You need one block to generate server roles interfaces. Lastly, you need to generate regular types in yet another block, or in one of them (but not both).

Something like this

wire {
    kotlin {
        includes = ['all.services.or.package']
        rpcCallStyle = 'suspending'
        rpcRole = 'client'
    }
    kotlin {
        includes = ['all.services.or.package']
        rpcCallStyle = 'suspending'
        rpcRole = 'server'
    }
    kotlin {
        excludes = ['all.services.or.package']
        rpcRole = 'none'
    }
}