简体   繁体   English

Protobuf Android 在生成的文件夹中包含构建类型

[英]Protobuf Android includes build type in generated folders

I am using the following app.gradle to generate java classes from.proto classes inside an android sample project.我正在使用以下 app.gradle 从 android 示例项目中的 proto 类生成 java 类。

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "protobuf.example.com.protobuftestapp"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.0.0"
    }
    plugins {
        lite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.plugins {
                lite {
                    outputSubDir = ''
                }
            }
        }
    }
    generatedFilesBaseDir = "$projectDir/src/main/java/"
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.protobuf:protobuf-lite:3.0.0'
    testCompile 'junit:junit:4.12'
}

The.proto file looks like this: .proto 文件如下所示:

syntax = "proto3";
package my_test_package;
option java_package = "protoclasses";
option java_outer_classname = "MyTestClass";
message TestClass {
    string test_string_a = 1;
    int32 test_int_b = 2;
    bool test_bool_c = 3;
}

The classes are generated, but the folder structure depends on the the buildType.类已生成,但文件夹结构取决于 buildType。

The output is输出是

  • src->main->java-> DEBUG ->protoclasses->MyTestClass.java src->main->java-> DEBUG ->protoclasses->MyTestClass.java
  • src->main->java-> RELEASE ->protoclasses->MyTestClass.java src->main->java-> RELEASE ->protoclasses->MyTestClass.java

I searched for hours, but didn't manage to strip the buildType folder.我搜索了几个小时,但没能删除 buildType 文件夹。 The problem is, that the classes are generated as a duplicate, which leads to a failing build.问题是,这些类是作为副本生成的,这会导致构建失败。

I managed to "solve" the problem, editing this line我设法“解决”了问题,编辑了这一行

generatedFilesBaseDir = "$projectDir/src/main/proto_gen"

Not generating the classes in src / main / java (but one hierarchy level up) does not add both directories (debug, release) to the build path, only the debug folder is added.不在src / main / java中生成类(但向上一层)不会将两个目录(调试、发布)添加到构建路径,只添加调试文件夹。

The debug folder also gets an icon marking it as a generated source root.调试文件夹还有一个图标,将其标记为生成的源根目录。

Even though both folders are still generated, there are no class conflicts anymore.即使仍然生成两个文件夹,也不再有类冲突。 Using the debug version of the generated class is fine as well, as it is the exact same version as the release version.使用生成类的调试版本也很好,因为它与发布版本完全相同。

Although it does not make sense (to me.) to generate different variants (for each android buildType) of a,proto definition.尽管(对我而言)生成原型定义的不同变体(对于每个 android buildType)没有意义。 we found a solution to work with.我们找到了一个解决方案。

Hope that helps!希望有帮助!

You can use relative path in outputSubDir.您可以在 outputSubDir 中使用相对路径。

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.13.0'
    }
//    generatedFilesBaseDir = "$projectDir/src/main/java/"
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {
                    option "lite"
                    outputSubDir = '../../../../../src/main/java/'
                }
            }
        }
    }
}

In my case, I want protoc generate java code and put into src/main/java auto.在我的例子中,我希望 protoc 生成 java 代码并放入src/main/java auto。 However, there is still a problem that if i set generatedFiledBaseDir to $projectDir/src/main/java/ and set outputSubDir to '../' , it will still leave the build type dir(debug | release) at generatedFiledBaseDir .但是,仍然存在一个问题,如果我将generatedFiledBaseDir设置为$projectDir/src/main/java/并将outputSubDir设置为'../' ,它仍然会将构建类型 dir(debug | release)保留在generatedFiledBaseDir Therefore, i use outputSubDir only, and the build type dir is left in the build dir , it is more graceful:)因此,我只使用outputSubDir ,构建类型 dir 留在构建目录中,它更优雅:)

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

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