Android使用ProtoBuf 适配 gradle7.5 gradle8.0

发布于:2024-04-27 ⋅ 阅读:(155) ⋅ 点赞:(0)

ProtoBuf 适配 Gradle7.5

圆规

gradle-wrapper.properties 配置

distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip

Project:build.gradle:

plugins {
    id 'com.android.application' version '7.4.2' apply false
    id 'com.android.library' version '7.4.2' apply false
    id 'com.google.protobuf' version '0.8.19' apply false
}

App:build.gradle:

proto文件

android {

    // android studio 生成 jniLibs 目录
    sourceSets {
        main {
            proto {
                // 指定.proto文件路径
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
        }
    }
}
 
dependencies {
    // 定义protobuf依赖
    implementation "com.google.protobuf:protobuf-java:3.6.1"
}

protobuf {
    protoc {
        // //配置 protoc 编译器
        artifact = 'com.google.protobuf:protoc:3.6.1'
    }

	// 配置生成目录,编译后会在 build 的目录下生成对应的java文件
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
            }
        }
    }
}

ProtoBuf 适配 Gradle8.0

gradle-wrapper.properties 配置

distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip

Project:build.gradle:

plugins {
    id 'com.android.application' version '8.1.3' apply false
    id 'com.android.library' version '8.1.3' apply false
    id 'com.google.protobuf' version '0.9.3' apply false
}

App:build.gradle:

android {
 	
    // android studio 生成 jniLibs 目录
    sourceSets {
        main {
            proto {
                // 指定.proto文件路径
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
        }
    }
}
 
protobuf {
    //配置 protoc 编译器
    protoc {
        artifact = 'com.google.protobuf:protoc:3.21.7'
    }
    //配置生成目录,编译后会在 build 的目录下生成对应的java文件
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
            }
        }
    }
}

dependencies {
    implementation 'com.google.protobuf:protobuf-java:3.21.7'
    //....
}

网站公告

今日签到

点亮在社区的每一天
去签到