diff --git a/Example/testHotUpdate/ios/Podfile.lock b/Example/testHotUpdate/ios/Podfile.lock index cfff0a6..a89c746 100644 --- a/Example/testHotUpdate/ios/Podfile.lock +++ b/Example/testHotUpdate/ios/Podfile.lock @@ -337,7 +337,7 @@ SPEC CHECKSUMS: React-jsi: 32285a21b1b24c36060493ed3057a34677d58d09 React-jsiexecutor: 8909917ff7d8f21a57e443a866fd8d4560e50c65 React-jsinspector: 111d7d342b07a904c400592e02a2b958f1098b60 - react-native-update: 5c1c198cf30994de21e05bfff431814f3722948c + react-native-update: dd1ae7615b2281c9f2cd842ab672593074999549 React-RCTActionSheet: 89b037c0fb7d2671607cb645760164e7e0c013f6 React-RCTAnimation: e3cefa93c38c004c318f7ec04b883eb14b8b8235 React-RCTBlob: d26ac0e313fbf14e7203473fd593ccaaeee8329e diff --git a/Example/testHotUpdate/src/index.js b/Example/testHotUpdate/src/index.js index 2d2a62c..d072044 100644 --- a/Example/testHotUpdate/src/index.js +++ b/Example/testHotUpdate/src/index.js @@ -70,10 +70,6 @@ export default class App extends Component { }; checkUpdate = async () => { - if (__DEV__) { - // 开发模式不支持热更新,跳过检查 - return; - } let info; try { info = await checkUpdate(appKey); diff --git a/android/build.gradle b/android/build.gradle index d695592..228492e 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,3 +1,5 @@ +int MILLIS_IN_MINUTE = 1000 * 60 +int minutesSinceEpoch = System.currentTimeMillis() / MILLIS_IN_MINUTE def safeExtGet(prop, fallback) { rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback @@ -22,6 +24,17 @@ android { jniLibs.srcDirs = ['./lib'] } } + + buildTypes { + release { + buildConfigField("String", "PUSHY_BUILD_TIME", "\"${minutesSinceEpoch}\"") + resValue("string", "pushy_build_time", "${minutesSinceEpoch}") + } + debug { + buildConfigField("String", "PUSHY_BUILD_TIME", "\"0\"") + resValue("string", "pushy_build_time", "0") + } + } } repositories { diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java index e91a8e2..eec7e0f 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -59,6 +59,10 @@ public class UpdateContext { return null; } + public String getBuildTime() { + return BuildConfig.PUSHY_BUILD_TIME; + } + public interface DownloadFileListener { void onDownloadCompleted(); void onDownloadFailed(Throwable error); diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index 00f5e2e..7af5e79 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -41,6 +41,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{ constants.put("downloadRootDir", updateContext.getRootDir()); constants.put("packageVersion", updateContext.getPackageVersion()); constants.put("currentVersion", updateContext.getCurrentVersion()); + constants.put("buildTime", updateContext.getBuildTime()); boolean isFirstTime = updateContext.isFirstTime(); constants.put("isFirstTime", isFirstTime); if (isFirstTime) { diff --git a/ios/RCTHotUpdate/RCTHotUpdate.m b/ios/RCTHotUpdate/RCTHotUpdate.m index dca187f..6cb0728 100644 --- a/ios/RCTHotUpdate/RCTHotUpdate.m +++ b/ios/RCTHotUpdate/RCTHotUpdate.m @@ -152,6 +152,7 @@ RCT_EXPORT_MODULE(RCTHotUpdate); NSMutableDictionary *ret = [NSMutableDictionary new]; ret[@"downloadRootDir"] = [RCTHotUpdate downloadDir]; ret[@"packageVersion"] = [RCTHotUpdate packageVersion]; + ret[@"buildTime"] = [RCTHotUpdate buildTime]; ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked]; ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked]; NSDictionary *updateInfo = [defaults dictionaryForKey:keyUpdateInfo]; @@ -464,4 +465,15 @@ RCT_EXPORT_METHOD(markSuccess) return version; } ++ (NSString *)buildTime +{ +#if DEBUG + return @"0"; +#else + // To be replaced by scripts/generateiOSBuildTime.sh + NSString *pushy_build_time = 1570370091 + return pushy_build_time; +#endif +} + @end diff --git a/lib/index.js b/lib/index.js index 32faf76..f709ffd 100644 --- a/lib/index.js +++ b/lib/index.js @@ -8,6 +8,7 @@ export const packageVersion = HotUpdate.packageVersion; export const currentVersion = HotUpdate.currentVersion; export const isFirstTime = HotUpdate.isFirstTime; export const isRolledBack = HotUpdate.isRolledBack; +export const buildTime = HotUpdate.buildTime; /* Return json: @@ -49,8 +50,9 @@ export async function checkUpdate(APPKEY) { 'Content-Type': 'application/json', }, body: JSON.stringify({ - packageVersion: packageVersion, + packageVersion, hash: currentVersion, + buildTime, }), }); diff --git a/react-native-update.podspec b/react-native-update.podspec index af82212..fed95a8 100644 --- a/react-native-update.podspec +++ b/react-native-update.podspec @@ -17,6 +17,8 @@ Pod::Spec.new do |s| s.vendored_libraries = 'RCTHotUpdate/libRCTHotUpdate.a' s.pod_target_xcconfig = { 'USER_HEADER_SEARCH_PATHS' => '"$(SRCROOT)/../node_modules/react-native-update/ios"' } + s.script_phase = { :name => 'Generate build time', :script => '../../node_modules/react-native-update/scripts/generateiOSBuildTime.sh', :execution_position => :before_compile } + s.dependency 'React' s.dependency 'SSZipArchive' diff --git a/scripts/generateiOSBuildTime.sh b/scripts/generateiOSBuildTime.sh new file mode 100755 index 0000000..5145a99 --- /dev/null +++ b/scripts/generateiOSBuildTime.sh @@ -0,0 +1,9 @@ + +#!/bin/bash +set -x +# DEST=$CONFIGURATION_BUILD_DIR/$UNLOCALIZED_RESOURCES_FOLDER_PATH +# date +%s > "$DEST/pushy_build_time.txt" +OLD_TEXT="NSString \*pushy_build_time" +NEW_TEXT=" NSString \*pushy_build_time = $(date +%s)" +TARGET_FILE="../../node_modules/react-native-update/ios/RCTHotUpdate/RCTHotUpdate.m" +sed -i '' -e "s/.*$OLD_TEXT.*/$NEW_TEXT/" "$TARGET_FILE" \ No newline at end of file