Android Studio 配置 proto

环境

Android Studio Koala | 2024.1.1

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

  • 下载protobuf相关plugin

  • protobuf配置

java 复制代码
<!-- /gradle/libs.versions.toml -->

[versions]
agp = "8.5.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
appcompat = "1.7.0"
material = "1.12.0"
proto = "0.9.3"

[libraries]
appcompat_appcompat-x-0-0 = { module = "androidx.appcompat_appcompat:1.0.0" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
google-protobuf = { id = "com.google.protobuf", version.ref = "proto" }
java 复制代码
<!-- /***Module/build.gradle -->

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.google.protobuf)
}

android {
    namespace 'com.android.settings.intelligence'
    compileSdk 34

    defaultConfig {
        minSdk 30

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    sourceSets {
        main {
            proto {
                srcDir 'src/main/proto'
            }
        }
    }
}

dependencies {
    implementation libs.appcompat
    implementation libs.material
    implementation 'com.google.protobuf:protobuf-java:3.22.3'
    api 'androidx.legacy:legacy-support-v4:1.0.0'
    api 'androidx.preference:preference:1.2.1'
    api 'androidx.cardview:cardview:1.0.0'
    api 'androidx.recyclerview:recyclerview:1.3.2'
}

protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.19.2'
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}// 生产java源码
            }
        }
    }
}
java 复制代码
<!-- /src/main/proto/settings_intelligence_log.proto -->

syntax = "proto2";

option java_outer_classname = "SettingsIntelligenceLogProto";

package com.android.settings.intelligence;

// Wrapper for SettingsIntelligence event.
// Next index: 3
message SettingsIntelligenceEvent {

    // Event type for this log.
    enum EventType {
        // Do not use
        UNUSED = 0;

        // Gets suggestion list
        GET_SUGGESTION = 1;

        // Dismisses a suggestion
        DISMISS_SUGGESTION = 2;

        // Launches a suggestion
        LAUNCH_SUGGESTION = 3;

        // Opens search page
        OPEN_SEARCH_PAGE = 4;

        // Leaves search page
        LEAVE_SEARCH_PAGE = 5;

        // User sends a query to settings search
        PERFORM_SEARCH = 6;

        // Clicks a search result
        CLICK_SEARCH_RESULT = 7;

        // Clicks a saved query
        CLICK_SAVED_QUERY = 8;

        // Search service indexes database
        INDEX_SEARCH = 9;

        // Displays the no result image in search
        SHOW_SEARCH_NO_RESULT = 10;

        // Displays some result in search
        SHOW_SEARCH_RESULT = 11;

        // Leaves search page without entering any query
        LEAVE_SEARCH_WITHOUT_QUERY = 12;

        // Queries search data during a search session
        SEARCH_QUERY_DATABASE = 13;

        // Queries installed app list during a search session
        SEARCH_QUERY_INSTALLED_APPS = 14;

        // Queries input device list (keyboards, game controller etc) during
        // a search session
        SEARCH_QUERY_INPUT_DEVICES = 15;

        // Queries accessiblity service list during a search session
        SEARCH_QUERY_ACCESSIBILITY_SERVICES = 16;
    }

    message SearchResultMetadata {
        // The id of the search result row in this event, this is an internally
        // generated key and does not associate with any user data.
        optional string search_result_key = 1;

        // The rank of the search result row in this event.
        optional int32 search_result_rank = 2;

        // The number of results in this query.
        optional int32 result_count = 3;

        // The length of query word.
        optional int32 search_query_length = 4;
    }

    // The type of suggestion event.
    optional EventType event_type = 1;

    // The name/id of the suggestion in this event.
    repeated string suggestion_ids = 2;

    // Data about search results in this event.
    optional SearchResultMetadata search_result_metadata = 3;

    // Latency for the current event.
    optional int64 latency_millis = 4;
}
  • 同步重新构建后生成的目录

Settings\SettingsIntelligence\build\generated\source\proto\debug\java\com\android\settings\intelligence\SettingsIntelligenceLogProto.java

详细代码可参考 https://github.com/Shigq-droid/Settings_Android13/tree/master/SettingsIntelligence.

相关推荐
硬件学长森哥3 小时前
Android影像基础--cameraAPI2核心流程
android·计算机视觉
前行的小黑炭7 小时前
Android 协程的使用:结合一个环境噪音检查功能的例子来玩玩
android·java·kotlin
阿华的代码王国7 小时前
【Android】内外部存储的读写
android·内外存储的读写
李少兄8 小时前
解决IntelliJ IDEA 提交代码时无复选框问题
java·ide·intellij-idea
sniper_fandc9 小时前
IDEA修改系统缓存路径,防止C盘爆满
java·ide·intellij-idea
CUIYD_19899 小时前
Eclipse 常用搜索功能汇总
java·ide·eclipse
inmK111 小时前
蓝奏云官方版不好用?蓝云最后一版实测:轻量化 + 不限速(避更新坑) 蓝云、蓝奏云第三方安卓版、蓝云最后一版、蓝奏云无广告管理工具、安卓网盘轻量化 APP
android·工具·网盘工具
giaoho11 小时前
Android 热点开发的相关api总结
android
byte轻骑兵11 小时前
365 天技术创作手记:从一行代码到四万同行者的相遇
ide·vscode·编辑器
点云侠13 小时前
解决Visual Studio 2022编译工程速度慢的问题
开发语言·c++·ide·算法·计算机视觉·visual studio