Gradle compile dependency outside of project

751 views Asked by At

I have a frontend and a backend repository for my app. The backend is written in Go and serves the API over gRPC. The generated gRPC Java files end up in backend-repo/proto-gen/java/ (so backend-repo/proto-gen/java/com/myApp/Users.java).

In my frontend android repo I have submoduled the backend repo to a folder called server. I want to modify my build.gradle to compile the .java files from the backend.

android-repo/
    app/
        build.gradle
    server/proto-gen/java/com/myApp/
        Users.java
        AnotherService.java

I'm very new to Android development and am struggling to figure out the right approach.

This is a snippet from my app/build.gradle but it fails because it can't find the package com.google.protobuf.

task compileGrpc (type: JavaCompile) {
    source = fileTree(dir: '../server/proto-gen/java/', include: '**/*.java')
    destinationDir = file('build/classes')
    classpath = files('../server/proto-gen/java/')
    options.compilerArgs = ["-sourcepath", "$projectDir/../server/proto-gen/java/"]
}
dependencies {
    compile 'com.google.protobuf:protobuf-java:3.0.0-alpha-2'
    compileGrpc.execute()
}
2

There are 2 answers

0
ahasbini On

Simply including the path to the src sourceset for the project would include them within the compilation:

android {
    ...
    sourceSets {
        main {
            java.srcDirs = [java.srcDirs, 'server/proto-gen/java']
        }
    }
}

You'll be able to see the src files/directories included within the Android View/Project Explorer on the right side and edit the files directly.

0
Chetan Laddha On

You can add your dependencies like

dependencies {
        classpath files('server/proto-gen/java/com/myApp/')
    }

This will accept relative path.