Reputation: 588
I am using the following app.gradle to generate java classes from .proto classes inside an android sample project.
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:
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.
The output is
I searched for hours, but didn't manage to strip the buildType folder. The problem is, that the classes are generated as a duplicate, which leads to a failing build.
Upvotes: 1
Views: 1930
Reputation: 97
You can use relative path in 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. 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. Therefore, i use outputSubDir only, and the build type dir is left in the build dir, it is more graceful :)
Upvotes: 3
Reputation: 588
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.
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, we found a solution to work with.
Hope that helps!
Upvotes: 1