1、工程结构

2、这样结构的好处
从图可以大致知道,通过不同的模块工程,已经将不同的分享平台隔离开来。
- 维护更加方便
- 模块工程单独发布
3、依赖方式
dependencies {
implementation 'com.ysbing.yshare:yshare:1.0.0'
implementation 'com.ysbing.yshare:qq:1.0.0'
implementation 'com.ysbing.yshare:wechat:1.0.0'
implementation 'com.ysbing.yshare:weibo:1.0.0'
}
依赖了四个,第一个是必须有的,是YShare的入口类,所有的交互只在这个库进行,其余的是按需接入的,比如qq,如果没依赖它的话,是不能使用的,因为处理的逻辑在qq模块里,所以要用哪个分享平台,就写上对应的分享平台依赖。
4、模块逻辑处理
看了依赖方式,应该对这种方式的实现有点好奇,我们先来看下'com.ysbing.yshare:yshare:1.0.0'本身,打开它的pom文件
<dependencies>
<dependency>
<groupId>com.ysbing.yshare</groupId>
<artifactId>base</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ysbing.yshare</groupId>
<artifactId>qq</artifactId>
<version>1.0.0-blank</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ysbing.yshare</groupId>
<artifactId>wechat</artifactId>
<version>1.0.0-blank</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.ysbing.yshare</groupId>
<artifactId>weibo</artifactId>
<version>1.0.0-blank</version>
<scope>compile</scope>
</dependency>
<dependency>
里面其实是有做了对qq、wechat和weibo的工程依赖,但是版本号有点奇怪,是1.0.0-blank,而不是1.0.0,这个blank版本是有什么特殊吗?来,看下qq模块工程的build.grale
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply from: new File(rootProject.projectDir, "gradle/gradle-on-demand.gradle")
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 14
targetSdkVersion 28
versionCode VERSION_CODE.toInteger()
versionName VERSION_NAME.toString()
if (VERSION_NAME.contains("blank")) {
defaultPublishConfig "blankRelease"
} else {
defaultPublishConfig "fullRelease"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "channel"
productFlavors {
blank {
dimension "channel"
}
full {
dimension "channel"
}
}
}
dependencies {
debugImplementation project(':base')
releaseImplementation 'com.ysbing.yshare:base:' + VERSION_NAME.replace("-blank", "")
fullImplementation files('libs/open_sdk_r6019_lite.jar')
}
这里配置了两种风味,分别是full和blank,full风味里是真正实现分享逻辑的类,而blank风味里面是空实现,在发布的时候,用yshare入口库引用了blank风味,所以就不会增大体积,按需引用full风味,根据不同的版本号,就将blank风味替换成full风味,这样就实现了真正逻辑上的替换。
