简体   繁体   English

构建取决于Android Project Gradle的Java库

[英]Building Java Library that Depends on Android Project Gradle

I have an Android project that defines various datatypes that are serialized via Protocol Buffers. 我有一个Android项目,它定义了通过Protocol Buffers序列化的各种数据类型。 I would like to have a separate Java project that is able to deserialize these data types on the server. 我想有一个单独的Java项目,它能够在服务器上反序列化这些数据类型。

My project is structured as follows: 我的项目结构如下:

Android Library
  - Data
      - TypeA 
      - TypeB
      - ...

Java Server
  - Parser (depends on .Data)

In this example, all classes in the package Data handle marshaling themselves. 在此示例中,数据包Data中的所有类都处理自己编组。

When trying to build the Java Server, it successfully compiles the Android project, but ultimately fails with Gradle saying it cannot find the referenced packages. 在尝试构建Java Server时,它成功编译了Android项目,但最终因Gradle无法找到引用的包而失败。

Note, that I can successfully build the Java Server with cross project dependencies if I only pull in other Java libraries, leading me to believe this is a Java -> Android dependency problem rather than a Gradle configuration issue. 请注意,如果我只引入其他Java库,我可以成功构建具有跨项目依赖关系的Java Server,这让我相信这是一个Java - > Android依赖问题,而不是Gradle配置问题。

On possible solution I have considered is to pull out all of these data types from the Android project and place them in a separate Java library. 我考虑过的可能解决方案是从Android项目中提取所有这些数据类型并将它们放在一个单独的Java库中。 The trouble with this however is that some of these classes merely provide a means of converting Android specific classes ( SensorEvent, Location, etc ) into a type that can be used with ProtoBuf. 然而,问题在于其中一些类只提供了将Android特定类(SensorEvent,Location等)转换为可与ProtoBuf一起使用的类型的方法。 One solution I've been considering would look like this: 我一直在考虑的一个解决方案是这样的:

Data Types
  - TypeA
  - TypeB
  - ...

Android Library
  - Data
    - AndroidTypeA (Class with Android Specific constructor that can be cast to the parent type)
    - AndroidTypeB
    - ...

Java Server
  - Parser ( Depends on the Data Types module )  

While this would indeed solve my issue, it hardly seems like the best solution. 虽然这确实可以解决我的问题,但它似乎不是最好的解决方案。

Is there any way to accomplish this through Gradle alone? 有没有办法通过Gradle单独完成这项工作?


For reference here are abbreviated versions of my build.gradle files. 这里参考我的build.gradle文件的缩写版本。

Android Library: Android库:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 21
    buildToolsVersion "21.0.2"

    defaultConfig {
        applicationId "com.foo.bar.androidlib"
        ...
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    ...
    compile 'com.google.protobuf:protobuf-java:2.6.1'
}

Java Server: Java服务器:

apply plugin: 'java'


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.protobuf:protobuf-java:2.6.1'
    compile project(':androidlib')
}

Edit: 编辑:

Specifically Gradle gives the following error: 特别是Gradle给出以下错误:

error: package com.foo.bar.androidlib does not exist 错误:包com.foo.bar.androidlib不存在

In Gradle, you can't have a Java library that depends on an Android library. 在Gradle中,您不能拥有依赖于Android库的Java库。 This is because the Android plugin in Gradle isn't based on the Java plugin and isn't compatible with it, so Java doesn't understand its sourcesets and can't otherwise play nice with it. 这是因为Gradle中的Android插件不是基于Java插件而且与它不兼容,因此Java不了解它的源集,否则无法使用它。

I don't think you can solve this problem purely through Gradle configuration, and even if you could, it would probably make your build wickedly complex and fragile. 我不认为你可以完全通过Gradle配置来解决这个问题,即使你可以,它也可能会使你的构建变得复杂和脆弱。 Your best bet is to have your Java stuff rely only on Java stuff, isolating out the common protobuf dependencies there, and have the Android side depend on Java. 最好的办法是让你的Java东西只依赖于Java的东西,隔离那里常见的protobuf依赖,让Android端依赖Java。 You outlined an approach with Android versions of some of the classes; 您概述了使用某些类的Android版本的方法; that may work, but I don't know enough details about what you're doing to comment on it in detail. 这可能有用,但我不知道你正在做些什么来详细评论它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM