项目中用到了RabbitMQ与Protobuf,特意记录一下
android studio中Protobuf配置
1.project的build.gradle配置
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2.moudle的build.gradle配置
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.wangyao.myapplication"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
include '**/*.proto'
}
java {
srcDir 'src/main/java'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.6.1'
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.builtins {
java {}
}
}
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//google protobuf传输协议
compile 'com.google.protobuf:protobuf-java:3.6.1'
}
上面代码中sourceSets节点配置出来的效果如下
3.安装Protobuf Support插件
如果无法网络安装,可以自己下载插件安装(不要告诉我不知道怎么安装啊,如果不知道可以百度)
http://plugins.jetbrains.com/plugin/8277-protobuf-support
4.在proto文件夹下面编写.proto文件
我的mi_log.proro文件是后台给的,也可以自己编写。编译完后再generated文件夹下面看到java文件
4.moudle的build.gradle配置RabbitMQ
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.wangyao.myapplication"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//rabbitmq 消息中间件与后台通信
implementation files('libs/amqp-client-4.1.1.jar')
implementation files('libs/slf4j-api-1.7.7.jar')
}
添加slf4j-api-1.7.7.jar是因为我build project时候报找不到 org.slf4j.LoggerFactory 这个类
5.最终的moudle的build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.wangyao.myapplication"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
sourceSets {
main {
proto {
srcDir 'src/main/proto'
include '**/*.proto'
}
java {
srcDir 'src/main/java'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.6.1'
}
generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.builtins {
java {}
}
}
}
generatedFilesBaseDir = "$projectDir/src/generated"
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//google protobuf传输协议
compile 'com.google.protobuf:protobuf-java:3.6.1'
//rabbitmq 消息中间件与后台通信
implementation files('libs/amqp-client-4.1.1.jar')
implementation files('libs/slf4j-api-1.7.7.jar')
}
6.最终的project的build.gradle文件
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.6'
}
}
allprojects {
repositories {
maven { url "https://jitpack.io" }
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
7.使用RabbitMQ发送Probobuf 数据
package com.wangyao.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeoutException;
import mi.log.MiLog;
public class MainActivity extends AppCompatActivity {
private Button button;
private ConnectionFactory factory;
private final static String EXCHANGE_NAME = "mi.log.notify";
private final static String ROUTING_KEY = "mi.log";
private Connection connection;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:sss");
private MiLog.Message message;
private MiLog.CreateLogNotify createLogNotify;
private MiLog.AppLog appLog;
private Thread publishThread;
private Channel ch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
Date date = new Date(System.currentTimeMillis());
String time = dateFormat.format(date);
appLog = MiLog.AppLog.newBuilder()
.setLevel(MiLog.ELevel.Information)
.setTime(time)
.setMessage("我是王燿发来的测试信息")
.setAppType(MiLog.EAPP.Android)
.setVersion("16")
.setOperator("王燿")
.build();
createLogNotify = MiLog.CreateLogNotify.newBuilder().setAppLog(appLog).build();
message = MiLog.Message.newBuilder().setNtfCreateLog(createLogNotify).build();
factory = new ConnectionFactory();
setUpConnectionFactory();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMessage();
}
});
}
private void sendMessage() {
publishThread = new Thread(new Runnable() {
@Override
public void run() {
try {
connection = factory.newConnection();
ch = connection.createChannel();
ch.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.toByteArray());
ch.close();
connection.close();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
});
publishThread.start();
}
/**
* 连接设置
*/
private void setUpConnectionFactory() {
factory.setHost(Constants.MQ_HOST);//主机地址:
factory.setPort(Constants.MQ_PORT);// 端口号:5672
factory.setUsername(Constants.MQ_USERNAME);// 用户名
factory.setPassword(Constants.MQ_PASSWORD);// 密码
factory.setAutomaticRecoveryEnabled(false);
}
@Override
protected void onDestroy() {
super.onDestroy();
publishThread.interrupt();
}
}
8.mi_log.proto文件
package mi.log;
//-------------------------全部消息
message Message{
// 主体
oneof content
{
// 请求:创建日志
CreateLogNotify ntf_create_log=1;
}
}
//-------------------------枚举
//错误类型
enum ELevel
{
//信息
Information=1;
//警告
Warning=2;
//错误
Error=3;
}
//APP类型
enum EAPP
{
//苹果
IOS=1;
//安卓
Android=2;
//苹果pad
IOSPad=3;
//安卓pad
AndroidPad=4;
}
//-------------------------内容定义
// APP信息
message AppLog
{
// 级别
required ELevel level=1;
//时间 yyyy-MM-dd HH:mm:ss.fff
required string time=2;
//描述
required string message=3;
//app版本
required string version=4;
//app类型
required EAPP app_type=5;
//操作人
required string operator=6;
//mibee地址
optional string address=7;
//页面
optional string page=8;
//api操作
optional string api_name=9;
//dp操作
optional string dp=10;
//原始调试信息
optional string original_message=11;
}
// 请求:创建日志
message CreateLogNotify
{
//日志信息
oneof content
{
AppLog app_log=1;
MibeeLog mibee_log=2;
ServerLog server_log=3;
}
}
网友评论