最近在做項目的插件化,選擇的是騰訊的開源框架shadow。下面介紹幾個無關於項目,相關與shadow開發的小知識點。
目錄
一、統一使用自動版本號。
1、生成版本號
文件路徑:Shadow\buildScripts\gradle\common.gradle
//tangwuke:changer for vpsclient
def getVersionValue(type) {
if ("big".equals(type)) {
return hasProperty("APP_BIG_VERSION") ? APP_BIG_VERSION : "2"
} else if ("small".equals(type)) {
return hasProperty("APP_SMALL_VERSION") ? APP_SMALL_VERSION : "0"
}
}
def getSelfDefinedVersion(type) {
int aa = getVersionValue("big").toInteger() //大版本號
int bb = getVersionValue("small").toInteger() //小版本號
printf("getversion version : $aa.$bb \n")
Process process = "git rev-list --count HEAD".execute()
process.waitFor()
int cccc = process.getText().toInteger()
if ("code".equals(type)) {
new Date().format("yyMMddHHmm").toInteger()
// aa * 100000000 + bb * 100000000 + today
} else if ("name".equals(type)) {
String today = new Date().format("yyMMddHHmm")
process = "git describe --always".execute()
process.waitFor()
String sha1 = process.getText().trim()
"$aa.$bb.$cccc.$today.$sha1"
}
}
//end chagner
allprojects {
ext.COMPILE_SDK_VERSION = 29
ext.MIN_SDK_VERSION = 14
ext.TARGET_SDK_VERSION = 28
ext.VERSION_CODE = getSelfDefinedVersion("code")
ext.VERSION_NAME = getSelfDefinedVersion("name")
if ("${System.env.PUBLISH_RELEASE}".equalsIgnoreCase("true")) {
ext.VERSION_SUFFIX = ""
} else if ("${System.env.ORANGE}".equalsIgnoreCase("true")) {
ext.VERSION_SUFFIX = "-${System.env.ORANGE_COMMIT_SHORT}-SNAPSHOT"
} else {
ext.VERSION_SUFFIX = "-${gitShortRev()}-SNAPSHOT"
}
ext.ARTIFACT_VERSION = ext.VERSION_NAME + ext.VERSION_SUFFIX
ext.TEST_HOST_APP_APPLICATION_ID = 'com.tencent.shadow.test.hostapp'
//tang:changer for vpsclient
ext.SAMPLE_HOST_APP_APPLICATION_ID = 'com.android.vpsclient'
//end changer
repositories {
google()
jcenter()
}
}
2、使用
android {
defaultConfig {
... ...
versionCode project.VERSION_CODE
versionName project.VERSION_NAME
... ...
}
... ...
}
二、plugin使用宿主類
1、增加方法
路徑:Shadow\projects\sample\source\sample-host-lib\src\main\java\com\tencent\shadow\sample\host\lib\HostUiLayerProvider.java
public class HostUiLayerProvider {
private static HostUiLayerProvider sInstance;
... ...
public int getVersion() {
try {
PackageManager manager = mHostApplicationContext.getPackageManager();
PackageInfo info = manager.getPackageInfo(mHostApplicationContext.getPackageName(),
0);
String version = info.versionName;
int versioncode = info.versionCode;
return versioncode;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
2、在宿主中使用
HostUiLayerProvider.getInstance().getVersion()
這個原理是什麼,怎麼通信的還沒有弄清楚。
結束語
以後shadow之間的小知識就在這篇文章中進行更新。