美文网首页手机移动程序开发Android知识
利用Android Studio将Protobuf文件生成Jav

利用Android Studio将Protobuf文件生成Jav

作者: Funzion | 来源:发表于2017-02-06 10:32 被阅读0次

    配置Gradle

    1、项目的build.gradle文件加入:

     classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.0'
    
    项目级

    2、模块的build.gradle

    • 顶部添加protobuf插件
    apply plugin: 'com.google.protobuf'
    
    • android结点增加proto文件位置配置
        sourceSets {
            main {
                proto {
                    srcDir 'src/main/proto'
                    include '**/*.proto'
                }
                java {
                    srcDir 'src/main/java'
                }
            }
        }
    
    • 添加依赖
        compile 'com.google.protobuf:protobuf-java:3.1.0'
        compile 'com.google.protobuf:protoc:3.1.0'
    
    • 增加protobuf结点
    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.1.0'
        }
    
        generateProtoTasks {
            all().each { task ->
                task.builtins {
                    remove java
                }
                task.builtins {
                    java {}
                    // Add cpp output without any option.
                    // DO NOT omit the braces if you want this builtin to be added.
                    cpp {}
                }
            }
        }
    
        generatedFilesBaseDir = "$projectDir/src/generated"
    }
    

    目录结构

    目录结构

    完整的模块的build.gradle

    apply plugin: 'com.android.application'
    apply plugin: 'com.google.protobuf'
    
    android {
        compileSdkVersion 25
        buildToolsVersion "25.0.0"
        defaultConfig {
            applicationId "com.ydtf.nbmobile.protobufdemo4"
            minSdkVersion 16
            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'
            }
        }
    
        sourceSets {
            main {
                proto {
                    srcDir 'src/main/proto'
                    include '**/*.proto'
                }
                java {
                    srcDir '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.0.1'
        compile 'com.google.protobuf:protobuf-java:3.1.0'
        compile 'com.google.protobuf:protoc:3.1.0'
        testCompile 'junit:junit:4.12'
    }
    
    protobuf {
        protoc {
            artifact = 'com.google.protobuf:protoc:3.1.0'
        }
    
        generateProtoTasks {
            all().each { task ->
                task.builtins {
                    remove java
                }
                task.builtins {
                    java {}
                    // Add cpp output without any option.
                    // DO NOT omit the braces if you want this builtin to be added.
                    cpp {}
                }
            }
        }
    
        generatedFilesBaseDir = "$projectDir/src/generated"
    }
    

    参考文章
    https://github.com/google/protobuf-gradle-plugin

    相关文章

      网友评论

        本文标题:利用Android Studio将Protobuf文件生成Jav

        本文链接:https://www.haomeiwen.com/subject/apmyittx.html