- Install the CodePush CLI 创建codepush账号
npm install -g code-push-cli
创建账号:code-push register (弹出注册窗口,注册后会生成一串码)
(可以用code-push login 登录,然后生成一个文件.注意网络问题会到只不成功,多操作几次)
注册应用名字 code-push app add MyApp
2.在项目的根目录下执行
npm install –save react-native-code-push
3.在android/settings.gradle 增加
include ‘:app’, ‘:react-native-code-push’
project(‘:react-native-code-push’).projectDir = new File(rootProject.projectDir, ‘../node_modules/react-native-code-push/android/app’)
修改 android/app/build.gradle 增加
…
dependencies {
…
compile project(‘:react-native-code-push’)
}
Plugin Configuration (Android)
Update the MainActivity.java file to use CodePush via the following changes:
…
// 1. Import the plugin class
import com.microsoft.codepush.react.CodePush;
// 2. Optional: extend FragmentActivity if you intend to show a dialog prompting users about updates.
public class MainActivity extends FragmentActivity implements DefaultHardwareBackBtnHandler {
…
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// 3. Initialize CodePush with your deployment key and an instance of your MainActivity.
CodePush codePush = new CodePush("d73bf5d8-4fbd-4e55-a837-accd328a21ba", this);
...
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
...
// 4. DELETE THIS LINE --> .setBundleAssetName("index.android.bundle")
// 5. Let CodePush determine which location to load the most updated bundle from.
// If there is no updated bundle from CodePush, the location will be the assets
// folder with the name of the bundle passed in, e.g. index.android.bundle
.setJSBundleFile(codePush.getBundleUrl("index.android.bundle"))
// 6. Expose the CodePush module to JavaScript.
.addPackage(codePush.getReactPackage())
...
}
}
….
修改android/app/build.gradle 中的android.defaultConfig.versionName,默认是1.0修改为1.0.0开始
android {
…
defaultConfig {
…
versionName “1.0.0”
…
}
…
}
插件用法:
最简单的方式就是在应用启动的组件中引入以下方法:
a.引入组件CodePush
var CodePush = require(“react-native-code-push”)
b.在应用启动的方法中调用
CodePush.sync();
提供2中方式:
一种是Silent mode (默认就是这种,会自动下载更新,并在下次重启应用的时候生效)
eg:
CodePush.sync();
另一种是Active mode(当有更新的时候会有提示,可以有一些模式提供选择可以参考接口)
eg:
codePush.sync({ updateDialog: true, installMode: codePush.InstallMode.IMMEDIATE });
codepush提供的接口如下:
checkForUpdate: 检查是否用东西要更新
getCurrentPackage: 获取最新包的一些信息如 ( description, installation time, size).
notifyApplicationReady: 通知 CodePush 你有新应用要发布
restartApp:更新应用并重启应用
sync: 检查更新、下载、并安装也可以定制化一些提示信息