weex指北针
https://weex.apache.org/zh/guide/develop/integrate-to-android-app.html
集成环境
IDE Android studio v3.1.2
JDK jdk1.8
根据文档说明,我一路next走下来,发现并不能正常跑起来,经baidu,google,github后,整理出一套完整的集成步骤;
首先说一下文档上的内容
1、app module下添加依赖(我的targetSdkVersion 28 )
implementation 'org.apache.weex:sdk:0.28.0'
// fastjson
implementation 'com.alibaba:fastjson:1.1.46.android'
//support library dependencies
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:28+'
2、app androidManifast 添加权限
//网络
//sd卡读写
此时先省略混淆方案
3、初始化SDK
InitConfig config = new InitConfig.Builder().build();
WXSDKEngine.initialize(applicationContext,config);
4、初始化WXSDKInstance 的 MainActivity代码
public class MainActivity extends AppCompatActivity implements IWXRenderListener {
WXSDKInstance mWXSDKInstance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWXSDKInstance = new WXSDKInstance(this);
mWXSDKInstance.registerRenderListener(this);
/**
* bundleUrl source http://dotwe.org/vue/38e202c16bdfefbdb88a8754f975454c
*/
String pageName = "WXSample";
String bundleUrl = "http://dotwe.org/raw/dist/38e202c16bdfefbdb88a8754f975454c.bundle.wx";
mWXSDKInstance.renderByUrl(pageName, bundleUrl, null, null,WXRenderStrategy.APPEND_ASYNC);
}
@Override
public void onViewCreated(WXSDKInstance instance, View view) {
setContentView(view);
}
@Override
public void onRenderSuccess(WXSDKInstance instance, int width, int height) {
}
@Override
public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {
}
@Override
public void onException(WXSDKInstance instance, String errCode, String msg) {
}
@Override
protected void onResume() {
super.onResume();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityResume();
}
}
@Override
protected void onPause() {
super.onPause();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityPause();
}
}
@Override
protected void onStop() {
super.onStop();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityDestroy();
}
}
}
WXSDKInstance是weex渲染页面的基本单元,此时注册了渲染监听,添加了视图.
到此时按照文档说明,应该是可以运行了,运行结果是 Hello World
run app.....
但是运行的结果 是一片空白
IWXRenderListener的方法 onException执行结果打印如下:
-1001//degradeToH5|createInstance fail|wx_create_instance_error isJSFrameworkInit==false reInitCount == 1
可以看出创建weex实例失败
此时我是一脸懵牛逼,作为官方文档,居然跑不通,简直匪夷所思。
5、查看一下源码:
WEEXSDK 初始化的方法
WXSDKEngine.initialize(MyWeex.this,config);
/**
- @param application
- @param config initial configurations or null
*/
public static void initialize(Application application,InitConfig config){
synchronized (mLock) {
if (mIsInit) {
return;
}
long start = System.currentTimeMillis();
WXEnvironment.sSDKInitStart = start;
if(WXEnvironment.isApkDebugable(application)){
WXEnvironment.sLogLevel = LogLevel.DEBUG;
}else{
WXEnvironment.sLogLevel = LogLevel.WARN;
}
doInitInternal(application,config);
registerApplicationOptions(application);
WXEnvironment.sSDKInitInvokeTime = System.currentTimeMillis()-start;
WXLogUtils.renderPerformanceLog("SDKInitInvokeTime", WXEnvironment.sSDKInitInvokeTime);
mIsInit = true;
}
}
private static void doInitInternal(final Application application,final InitConfig config){
WXEnvironment.sApplication = application;
if(application == null){
WXLogUtils.e(TAG, " doInitInternal application is null");
WXExceptionUtils.commitCriticalExceptionRT(null,
WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT,
"doInitInternal",
WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT.getErrorMsg() + "WXEnvironment sApplication is null",
null);
}
WXEnvironment.JsFrameworkInit = false;
WXBridgeManager.getInstance().postWithName(new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
WXSDKManager sm = WXSDKManager.getInstance();
sm.onSDKEngineInitialize();
if(config != null ) {
sm.setInitConfig(config);
}
WXSoInstallMgrSdk.init(application,
sm.getIWXSoLoaderAdapter(),
sm.getWXStatisticsListener());
final IWXUserTrackAdapter userTrackAdapter= config!=null?config.getUtAdapter():null;
final int version = 1;
mIsSoInit = WXSoInstallMgrSdk.initSo(V8_SO_NAME, version, userTrackAdapter);
WXSoInstallMgrSdk.copyJssRuntimeSo();
if(config!=null) {
for (String libraryName : config.getNativeLibraryList()) {
WXSoInstallMgrSdk.initSo(libraryName, version, userTrackAdapter);
}
}
if (!mIsSoInit) {
WXExceptionUtils.commitCriticalExceptionRT(null,
WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT,
"doInitInternal",
WXErrorCode.WX_KEY_EXCEPTION_SDK_INIT.getErrorMsg() + "isSoInit false",
null);
return;
}
sm.initScriptsFramework(config!=null?config.getFramework():null);
WXEnvironment.sSDKInitExecuteTime = System.currentTimeMillis() - start;
WXLogUtils.renderPerformanceLog("SDKInitExecuteTime", WXEnvironment.sSDKInitExecuteTime);
}
},null,"doInitWeexSdkInternal");
WXStateRecord.getInstance().startJSThreadWatchDog();
register();
}
看到这里可以看出,初始化时异步的。还有一种说法,是ndk架构问题,而文档第一行提示:
weex 支持
x86
armeabi-v7a
arm64-v8a
但是翻遍了文档,我也没发现在哪里下载so文件。根据这个哥们的指引,才发现是weex依赖版本问题,
由于weex 0.28.0的调整,以前版本自动引入的facebook提供的JS引擎js-android,现在被修改为需要手工引入,但是文档没有清晰的指出这个问题,导致运行的时候,会由于找不到libjsc.so而导致WXSDKEngine初始化失败。
https://www.mobibrw.com/2019/21684/comment-page-1
6、添加libjsc.so文件
app module build.gradle 中添加依赖
apply from: 'https://www.mobibrw.com/wp-content/uploads/2019/11/download_jsc.gradle'
同时配置ndk架构
ndk{
abiFilters "armeabi-v7a", "arm64-v8a", "x86"
}
build project后
还要补充一下WXSDKEngine属性和调试模式
7、创建application 并初始化代码
初始化源码,更多的配置
https://github.com/apache/incubator-weex-playground/blob/master/android/playground/src/main/java/org/apache/weex/WXApplication.java?spm=a2c7j.-zh-guide-develop-integrate-to-android-app.0.0.5a8441e9yort4j&file=WXApplication.java
public class MyWeex extends Application {
@Override
public void onCreate() {
super.onCreate();
InitConfig config = new InitConfig.Builder()
.setHttpAdapter(new DefaultWXHttpAdapter()).build();
WXSDKEngine.initialize(MyWeex.this,config);
WXBridgeManager.updateGlobalConfig("wson_on");
WXEnvironment.setOpenDebugLog(true);
WXEnvironment.setApkDebugable(true);
WXSDKEngine.addCustomOptions("appName", "WXSample");
WXSDKEngine.addCustomOptions("appGroup", "WXApp");
}
}
完整的代码如下:
app build.gradle文件
apply plugin: 'com.android.application'
apply from: 'https://www.mobibrw.com/wp-content/uploads/2019/11/download_jsc.gradle'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.nade.weex"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
ndk{
abiFilters "armeabi-v7a", "arm64-v8a", "x86"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'org.apache.weex:sdk:0.28.0'
// fastjson
implementation 'com.alibaba:fastjson:1.1.46.android'
//support library dependencies
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:appcompat-v7:28+'
}
MyWeex Application
public class MyWeex extends Application {
@Override
public void onCreate() {
super.onCreate();
InitConfig config = new InitConfig.Builder()
.setHttpAdapter(new DefaultWXHttpAdapter()).build();
WXSDKEngine.initialize(MyWeex.this,config);
WXBridgeManager.updateGlobalConfig("wson_on");
WXEnvironment.setOpenDebugLog(true);
WXEnvironment.setApkDebugable(true);
WXSDKEngine.addCustomOptions("appName", "WXSample");
WXSDKEngine.addCustomOptions("appGroup", "WXApp");
}
}
MainActivity
public class MainActivity extends AppCompatActivity implements IWXRenderListener {
private WXSDKInstance wxsdkInstance;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
/**
* 轮询访问 WXSDKEngine 初始化状态 防止异步造成的初始化失败问题
*/
if (msg.what == 1) {
if (WXSDKEngine.isInitialized()) {
startRender();
}else {
handler.sendEmptyMessageDelayed(1,300);
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wxsdkInstance = new WXSDKInstance(this);
wxsdkInstance.registerRenderListener(this);
if (WXSDKEngine.isInitialized()) {
startRender();
}else {
handler.sendEmptyMessageDelayed(1,300);
}
}
/**
* WXSDKEngine 初始化成功后 开始渲染
*/
private void startRender() {
String pageName = "WXSample";
/**
* 防止空指针
*/
if (wxsdkInstance == null) {
wxsdkInstance = new WXSDKInstance(this);
wxsdkInstance.registerRenderListener(this);
}
/**
* 渲染远程js
*/
String bundleUrl = "http://dotwe.org/raw/dist/38e202c16bdfefbdb88a8754f975454c.bundle.wx";
wxsdkInstance.renderByUrl(pageName, bundleUrl, null, null, WXRenderStrategy.APPEND_ASYNC);
/**
* 渲染本地js
*/
// String bundleUrl = "index.js";
// wxsdkInstance.render(pageName, WXFileUtils.loadAsset(bundleUrl,this), null, null, WXRenderStrategy.APPEND_ASYNC);
}
/**
* 重写生命周期方法
*/
@Override
protected void onResume() {
super.onResume();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityResume();
}
}
@Override
protected void onPause() {
super.onPause();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityPause();
}
}
@Override
protected void onStop() {
super.onStop();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityStop();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (wxsdkInstance != null) {
wxsdkInstance.onActivityDestroy();
}
}
@Override
public void onViewCreated(WXSDKInstance instance, View view) {
/**
* 填充视图
*/
setContentView(view);
}
@Override
public void onRenderSuccess(WXSDKInstance instance, int width, int height) {
}
@Override
public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {
}
@Override
public void onException(WXSDKInstance instance, String errCode, String msg) {
Log.d("welog", "onException: "+errCode+"//"+msg);
}
}
此时,就可以进行Run app 了 当然运行结果是hello world,欢迎讨论指正。