Android studio配置protobuf

要在AS中引用fabric-java-sdk,需要用到protobuf,经过一番周折,终于实现。过程如下:

1. 在全局的build.gradle文件中添加protobuf插件

  • 如果引用protobuf版本是3.0以下,插件版本是0.8.0
  • 如果引用的protobuf版本是3.0以上(我的版本是3.1.0),插件版本改成0.8.2

buildscript {
    
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.2'  // 此处版本需要根据protobuf版本而定



        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

2. 在module的build.gradle中做如下修改:

  • 在顶部添加protobuf插件com.google.protobuf
  • android中添加sourceSets
  • 在denpendencies中添加库的链接
  • 在最外层添加protobuf

build.gradle文件如下:

apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'    //1.应用protobuf插件


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.chaos.fabricsdk"
        minSdkVersion 26
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    compileOptions{
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    /*
    * 2.添加sourceSets
    * 
    * */
    sourceSets {
        main {
            proto {
                srcDir 'src/main/proto'
                include '**/*.proto'
            }
            java {
                srcDir 'src/main/java'
            }
        }
    }
}



dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation files('libs/fabric-sdk-java-1.2.1-SNAPSHOT.jar')
    implementation files('libs/log4j-1.2.17.jar')
    implementation files('libs/netty-all-4.1.29.Final.jar')
    implementation files('libs/commons-io-2.6.jar')
    implementation files('libs/bcprov-jdk14-160.jar')
    implementation files('libs/bcpkix-jdk14-160.jar')
    implementation files('libs/protobuf.jar')
    compile 'com.google.protobuf:protobuf-java:3.1.0'    // 3.添加库的路径
    compile 'com.google.protobuf:protoc:3.1.0'          // 3.添加库的路径
}

/*
* 4.添加protobuf
* */
protobuf {
    protoc {
        artifact = 'com.google.protobuf:protoc:3.1.0'
    }
    generateProtoTasks {
        all().each { task ->
            task.builtins {
                remove java
            }
            task.builtins {
                java {}
                cpp {}
            }
        }
    }
    generatedFilesBaseDir = "$projectDir/src/generated"
}

经过上面两步,再次点击run,运行正常。