Is there a way to generate C# code from .proto files with servive(grpc) using Gradle? - grpc-c#

I'm trying to generate C# code with gradle. I was able to generate message blocks but service blocks don't seem to be generated.
Here is my gradle file:
import com.google.protobuf.gradle.id
import com.google.protobuf.gradle.proto
plugins {
id("java")
id("idea")
id("com.google.protobuf")
}
repositories {
google()
mavenCentral()
}
dependencies {
implementation("com.google.protobuf:protobuf-kotlin:${project.property("protobuf.version")}")
implementation("io.grpc:grpc-kotlin-stub:${project.property("grpc.kotlin.version")}")
implementation("io.grpc:grpc-protobuf:${project.property("grpc.version")}")
}
sourceSets {
main {
proto {
srcDir("src")
}
}
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:${project.property("protobuf.version")}"
}
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${project.property("grpc.version")}"
}
id("grpckt") {
artifact = "io.grpc:protoc-gen-grpc-kotlin:${project.property("grpc.kotlin.version")}:jdk8#jar"
}
}
generateProtoTasks {
all().forEach {
ofSourceSet("main")
it.plugins {
id("grpc") {}
id("grpckt") {}
}
it.builtins {
id("kotlin") {}
id("csharp") {}
}
}
}
}
I think I can't use Grpc.Tools nuget package because imports inside .proto files are not found (I think it require to have .proto files inside the C# project, but I still need gradle to generate Kotlin source code).
So, if it's not possible, my only option is to use protoc executable right?
Edit: I added attribute ProtoRoot and now, I can use Grpc.Tools:
<ItemGroup>
<Protobuf Include="..\..\..\..\FanControl\proto\src\proto\settings.proto" GrpcServices="None" ProtoRoot="..\..\..\..\FanControl\proto\src\" />
</ItemGroup>

Related

KMM Project: Expected class has no actual declaration in module for JVM

I have a KMM project that is working perfectly except Android Studio gives an error on every expect function/value in my project complaining the actual version of it doesn't exist for JVM.
The A in a yellow diamond to the side of it shows both the iOS and Android actual versions and the project builds/runs just fine.
I've double checked package names and it happens with every expect regardless of the package.
I've looked over my gradle build files and can't find anything weird when comparing them to a new KMM example project.
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("com.rickclephas.kmp.nativecoroutines") version "0.12.2-new-mm"
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
//iosSimulatorArm64() sure all ios dependencies support this target
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "15.0"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
}
}
sourceSets {
val ktorVersion = "2.0.2"
val commonMain by getting {
dependencies {
implementation("com.litclimbing:firebase-auth:+")
implementation("com.litclimbing:firebase-firestore:+")
implementation("com.litclimbing:buffer:+")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.2")
implementation("io.ktor:ktor-client-core:$ktorVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
}
}
val androidMain by getting {
dependencies {
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
}
}
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
//val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
//iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
}
val iosX64Test by getting
val iosArm64Test by getting
//val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
//iosSimulatorArm64Test.dependsOn(this)
}
}
sourceSets.all {
languageSettings.optIn("kotlin.RequiresOptIn")
languageSettings.optIn("kotlin.ExperimentalUnsignedTypes")
}
}
android {
compileSdk = 32
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 29
targetSdk = 32
}
}
buildscript {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21")
classpath("com.android.tools.build:gradle:7.2.1")
classpath("com.google.gms:google-services:4.3.10")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.9.0")
}
}
allprojects {
repositories {
google()
mavenLocal()
mavenCentral()
maven {
setUrl("https://jitpack.io")
}
maven {
setUrl("https://maven.pkg.jetbrains.space/public/p/kotlinx-coroutines/maven")
}
}
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
I've even tried copying the gradle files to a new KMM project to see if it would break it and it didn't so I'm at a loss of where to even look.
Not really the solution I was looking for but it seems to be a bug in Android Studio/IntelliJ and not the gradle setup.
To fix it do the following.
Close Android Studio
Open your project folder in the file browser
Rename the .idea folder to .ideaBackup
Reopen the project
Assuming everything is now working, delete .ideaBackup
Enjoy your lack of random errors

Gradle build not including source/src groovy

I am trying to create a jar from a basic program.
I have a basic groovy project i.e. src/org...../*.groovy In the root
I have the following build.gradle
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
}
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.xxx.clarity.ClarityServices'
}
}
Problem is when I run and/or inspec the jar file my sclasses from src/** are not included! (all the dependencies are perfect)
What is the problem here?
UPDATE
When I add:
from files(fileTree(dir: 'src'))
to the task it includes the .groovy files :(
When I add
from sourceSets.main.output.classesDir
to the task and:
sourceSets {
main {
groovy {
srcDir 'src'
}
}
}
They do not get included :( Can't find any other ways....
By default, Gradle looks for source in src/main/groovy when the 'groovy' plugin in applied. You'll need to either restructure your project or configure your source sets to appropriately reflect your project structure.
Final working build.gradle. (thanks all).
apply plugin: 'application'
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
compile 'org.codehaus.groovy:groovy-all:2.3.6' //Was missing
}
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output) //Was missing/wrong
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jar
sourceSets.main.groovy {
srcDirs = [ 'src' ] //Was missing/wrong
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.petermac.clarity.ClarityServices'
}
}
referencing sourceSets.main.output.classesDir in your jar task means that it will just copy everything from that directory in your jar. The problem is that when you run gradle buildLabServicesJar nothing tells gradle that the classes should be compiled first. That's why the directory keeps to be empty and your jar doesn't contain the compiled classes. If you modify your task declaration from
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output.classesDir)
...
}
to
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output)
...
}
task autowiring kicks in. task autowiring means that if you declare an output of one task as input to another task (your buildLabServicesJar) gradle knows that it must generate the output first (run the compile task for example).
hope that helps!
You must excuse me but I have recently crossed over from a long life of Microsoft and am still learning. I am surprised by the lack of blogs and example code of basic stuff, what I am doing is so standard....(I will be posting one once/if I figure this out)
Note: Intellij -> Build -> Build Artifacts works perfectly but I would like to move this to Bamboo.
anyway taking into account everyone's ideas, here is my file (and error)
apply plugin: 'groovy'
version = '1.0'
repositories {
mavenCentral();
}
dependencies
{
compile files (fileTree(dir: 'lib', include: ['*.jar']),
fileTree(dir: 'lib/DocxDep', include: ['*.jar']))
}
//println "Classes dir: " + sourceSets.main.groovy
task buildLabServicesJar(type: Jar) {
from files(sourceSets.main.output)
//from sourceSets.main.groovy.output
//from files(fileTree(dir: 'src'))
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Implementation-Title': 'Lab Services',
'Implementation-Version': version,
'Main-Class': 'org.petermac.clarity.ClarityServices'
}
}
sourceSets {
main {
groovy.srcDirs = [ 'src' ]
}
}
ERROR:Cannot infer Groovy class path because no Groovy Jar was found on class path: configuration ':compile'
And if I change src line to:
srcDirs = [ 'src/**' ]
It builds but leaves out all my source again.

How to build Groovy JAR w/ Gradle and publish it to in-house repo

I have a Groovy project and am trying to build it with Gradle. First I want a package task that creates a JAR by compiling it against its dependencies. Then I need to generate a Maven POM for that JAR and publish the JAR/POM to an in-house Artifactory repo. The build.gradle:
apply plugin: "groovy"
apply plugin: "maven-publish"
repositories {
maven {
name "artifactory01"
url "http://myartifactory/artifactory/libs-release"
}
}
dependencies {
compile "long list starts here"
}
// Should compile up myapp-<version>.jar
jar {
}
// Should publish myapp-<version>.jar and its (generated) POM to our in-house Maven/Artifactory repo.
publishing {
publications {
myPublication(MavenPublication) {
from components.java
artifact sourceJar {
classifier "source"
}
pom.withXml {
// ???
}
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
However I do not believe I have set up versioning correctly with my jar task (for instance, how could I get it creating myapp-1.2.1 vs. myapp-1.2.2? I also don't think I have my publications configuration set up correctly: what should go in pom.withXml?
You're more than welcome to use artifactory plugin for that.
The documentation can be found in our user guide and below you can find a full working example of gradle build.
Run gradle build artifactoryPublish to build and publish the project.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '3.0.1')
}
}
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.jfrog.artifactory'
group = 'com.jfrog.example'
version = '1.2-SNAPSHOT'
status = 'SNAPSHOT'
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile 'junit:junit:4.11'
}
task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}
publishing {
publications {
main(MavenPublication) {
from components.java
artifact sourcesJar
}
}
artifactory {
contextUrl = 'http://myartifactory/artifactory'
resolve {
repository {
repoKey = 'libs-release'
}
}
publish {
repository {
repoKey = 'libs-snapshot-local'
username = 'whatever'
password = 'whatever123'
}
defaults {
publications 'main'
}
}
}
package is a keyword in Java/Groovy, and you'd have to use a different syntax to declare a task with that name.
Anyway, the task declaration for package should be removed, as the jar task already serves that purpose. The jar task configuration (jar { from ... }) should be at the outermost level (not nested inside another task), but from configurations.compile is unlikely what you want, as that will include Jars of compile dependencies into the Jar (which regular Java class loaders can't deal with), rather than merging them into the Jar. (Are you even sure you need a fat Jar?)
Likewise, the publish task declaration should be removed, and replaced with publishing { publications { ... } }.
Also, the buildscript block should probably be removed, and repositories { ... } and dependencies { ... } moved to the outermost level. ( buildscript { dependencies { ... } } declares dependencies of the build script itself (e.g. Gradle plugins), not the dependencies of the code to be compiled/run.)
I suggest to check out the many self-contained example builds in the samples directory of the full Gradle distribution (gradle-all).

gradle merge configurations.testRuntime with configurations.testCompile in copy

I'd like to copy some of my dependencies (retrieved from maven) to specific location in my build.gradle file.
If I iterate only testRuntime it works OK. My code is:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testRuntime 'com.h2database:h2:1+'
}
task foo {
copy {
from configurations.testRuntime.findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}
however I'd like to go for testCompile rather than testRuntime in case of h2 dependency. So I tried:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testCompile 'com.h2database:h2:1+'
}
task foo {
copy {
from [ configurations.testRuntime, configurations.testCompile ].flatten().findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}
However here I get error:
No such property: from for class: org.gradle.api.internal.file.copy.CopySpecWrapper_Decorated
I guess trouble is in my merging of the 2 lists here. Still can't figure out the right way.
Your solution will copy the files on every build invocation, even if the foo task isn't invoked. Here is a correct solution:
task foo(type: Copy) {
from configurations.testRuntime // includes configurations.testCompile
into "outputdir"
include "**/p6spy/**"
include "**/h2/**"
}
well, found solution myself, documenting it:
dependencies {
testRuntime 'p6spy:p6spy:2.+'
testCompile 'com.h2database:h2:1+'
}
task foo {
copy {
from configurations.testRuntime.plus(configurations.testCompile).findAll { it.getAbsolutePath().contains("/p6spy/") || it.getAbsolutePath().contains("/h2/") }
into "outputdir"
}
}

NDK Debugging with gradle-experimental plugin

I'm trying to add native debugging to a project that is an Android Studio NDK project. In the past I just used gradle to kick off a shell script, which built the NDK lib. Now I'm trying to move to use the gradle-experimental plugin.
I've scoured the net for what little info there is, (mostly here, Android Tools Site - Gradle Experimental), about using gradle-experimental with the NDK and I've put together this build.gradle file which is using the preview NDK support for doing the NDK build inline with the Java build.
After finally getting this together from bits-and-pieces of info, I managed to get the NDK portion building, but now it fails to include the httpmime-4.4-beta1.jar file that is clearly included in the dependencies, and I've tried many different permutations of it such as in:
compile files("libs/httpmime-4.4.jar")
But regardless, the errors for the missing symbols from the Jar file still appear.
build.gradle source
apply plugin: 'com.android.model.application'
String APP_PACKAGE_NAME = 'com.obfuscated.app',
VERSION_NAME = '3.0',
TOOLS_VERSION = '23.0.2'
int VERSION_CODE = 15,
MIN_SDK_VERSION = 13,
TARGET_SDK_VERSION = 19,
COMPILE_SDK_VERSION = 23
model {
repositories {
libs(PrebuiltLibraries) {
// prebuilt binaries mirroring Android.mk
libstuff {
headers.srcDirs.add(file("jni/stuff/include/stuff"))
binaries.withType(SharedLibraryBinary) {
sharedLibraryFile = file("jni/stuff/lib/libstuff.so")
}
}
// ...several more of these actually exist in build.gradle and are working
cares {
headers.srcDirs.add(file("jni/c-ares/include"))
binaries.withType(SharedLibraryBinary) {
// StaticLibraryBinary and staticLibraryFile doesnt work despite sample code, at least not for com.android.tools.build:gradle-experimental:0.6.0-alpha5, this builds even though its a static-lib
sharedLibraryFile = file("jni/c-ares/lib/libcaresARM.a")
}
}
}
}
android {
compileSdkVersion = COMPILE_SDK_VERSION
buildToolsVersion = TOOLS_VERSION
defaultConfig.with {
applicationId = APP_PACKAGE_NAME
minSdkVersion.apiLevel = MIN_SDK_VERSION
targetSdkVersion.apiLevel = TARGET_SDK_VERSION
versionCode = VERSION_CODE
versionName = VERSION_NAME
buildConfigFields {
create() {
type "int"
name "VALUE"
value "1"
}
}
compileOptions.with {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}
}
signingConfigs {
create("appRelease") {
storeFile file('sign.jks')
storePassword '...'
keyAlias '...'
keyPassword '...'
storeType "jks"
}
}
} // end android
android.lintOptions {
abortOnError = false
}
android.packagingOptions {
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/services/javax.annotation.processing.Processor'
}
android.ndk {
moduleName = "native"
toolchain "clang"
toolchainVersion "3.5"
platformVersion = MIN_SDK_VERSION
ldLibs.addAll('atomic', 'android', 'log', 'OpenSLES')
abiFilters.addAll(["armeabi", "armeabi-v7a"])
CFlags.addAll(["-mfloat-abi=softfp", "-mfpu=neon", "-O3", "-DCARES_STATICLIB", "-Wno-c++11-long-long"])
cppFlags.addAll(["-I${file("jni")}".toString(),
"-I${file("jni/c-ares/include")}".toString(),
"-I${file("jni/coffeecatch")}".toString()])
stl = "stlport_shared"
}
android.sources {
main {
jniLibs {
dependencies {
}
}
jni {
dependencies {
library "libstuff"
library "cares"
// ...
}
source {
srcDir "jni"
}
}
// java {
// dependencies {
// compile files("libs/httpmime-4.4-beta1.jar")
// compile files("libs/FlurryAnalytics-5.1.0.jar")
// }
// }
}
}
android.buildTypes {
debug {
ndk.with {
debuggable = true
}
}
release {
minifyEnabled = false
ndk.with {
debuggable = true
}
}
}
android.productFlavors {
create("arm") {
ndk.with {
abiFilters.add("armeabi-v7a")
ldLibs.addAll([file("jni/stuff/lib/libstuff.so").toString(),
file("jni/c-ares/lib/libcaresARM.a").toString()])
}
}
create("fat") {
// compile and package all supported ABI
}
}
} // end model
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.+'
compile 'com.android.support:appcompat-v7:23.+'
compile 'com.android.support:support-v13:23.+'
compile 'com.android.support:support-annotations:23.+'
compile 'com.squareup:otto:1.3.8'
compile 'com.github.machinarius:preferencefragment:0.1.1'
compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'com.fasterxml.jackson.core:jackson-core:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.5.+'
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.+'
compile 'com.jakewharton:butterknife:7.0.1'
compile 'com.google.guava:guava:19.0'
}
allprojects {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:deprecation" << "-Xlint:unchecked"
}
}
Out of frustration, I switched back to the non-experimental branch, and even with the old build.gradle file it is now failing to find that same jar file. So is it a problem with Android Studio 2.0 Preview 6?
Has anyone else experienced this, or have a solution for it? It would be so convenient to finally have NDK debugging work right in Android Studio, and if it weren't for this last hurdle, I think I would be there.
Short of re-writing the code that depends on that jar file, I am at a loss for what else to try. I am also open to suggestions for the format of my build.gradle file above, since the documentation for these new features is very sparse still, and some of the samples seem to be already out-of-date with regard to the proper syntax.
WHAT AM I MISSING?
You can see that the C and Cpp (mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp), steps are happening just fine, but then the Javac fails. This jar file approach has worked fine for the past 2 years or so for the http-mime lib from apache, so I don't understand why suddenly this is a problem.
:mobile:mergeArmDebugAndroidTestAssets
:mobile:generateArmDebugAndroidTestResValues UP-TO-DATE
:mobile:generateArmDebugAndroidTestResources
:mobile:mergeArmDebugAndroidTestResources
:mobile:processArmDebugAndroidTestResources
:mobile:generateArmDebugAndroidTestSources
:mobile:copyArmeabi-v7aDebugArmSharedLibraryStlSo
:mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainC
:mobile:compileNativeArmeabi-v7aDebugArmSharedLibraryNativeMainCpp
:mobile:linkNativeArmeabi-v7aDebugArmSharedLibrary
:mobile:nativeArmeabi-v7aDebugArmSharedLibrary
:mobile:stripSymbolsArmeabi-v7aDebugArmSharedLibrary
:mobile:ndkBuildArmeabi-v7aDebugArmSharedLibrary
:mobile:ndkBuildArmeabi-v7aDebugArmStaticLibrary UP-TO-DATE
:mobile:copyArmeabiDebugArmSharedLibraryStlSo
:mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainC
:mobile:compileNativeArmeabiDebugArmSharedLibraryNativeMainCpp
:mobile:linkNativeArmeabiDebugArmSharedLibrary
:mobile:nativeArmeabiDebugArmSharedLibrary
:mobile:stripSymbolsArmeabiDebugArmSharedLibrary
:mobile:ndkBuildArmeabiDebugArmSharedLibrary
:mobile:ndkBuildArmeabiDebugArmStaticLibrary UP-TO-DATE
:mobile:processAndroidArmDebugMainJniLibs UP-TO-DATE
:mobile:androidArmDebug
:mobile:compileArmDebugJavaWithJavac
:mobile:compileArmDebugJavaWithJavac - is not incremental (e.g. outputs have changed, no previous execution, etc.).
Yes, I know the apache libs are deprecated, but this is legacy code that should work despite that fact, and will be updated in the future.
A general way to do the include you're looking for is this in the dependencies.
compile fileTree(dir: 'libs', include: ['*.jar'])
However, I'm not certain that will solve this particular problem. I've always had success with putting the jar in the libs directory at the top of the directory structure.
If you need to have the jar in a different location, then this works for me:
repositories {
flatDir {
dirs '<relativePathToJar>'
}
}
model { ... }
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}

Resources