weex 集成到 Android 应用
weex官网
- 设置gradle依赖
在build.gradle中的dependencies添加支持Android开发的依赖1
2
3
4
5
6
7
8
9
10
11dependencies {
...
// weex sdk and fastjson
compile 'com.taobao.android:weex_sdk:0.20.0.2@aar'
compile 'com.alibaba:fastjson:1.1.46.android'
//support library dependencies
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.android.support:support-v4:23.1.1'
compile 'com.android.support:appcompat-v7:23.1.1'
}
- 配置混淆规则
1 | -keep class com.taobao.weex.bridge.** { *; } |
- 声明权限
在AndroidManifest.xml中声明权限1
2
3
4
5
6
7//网络
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
//sd卡读写
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- 初始化sdk
1 | InitConfig config = new InitConfig.Builder() |
- 创建WXSDKInstance
WXSDKInstance是weex 渲染页面的基本单元
- 通过
instance.render(url)拉取bundle - 在回调
IWXrenderListener的onViewCreated返回创建的view - 将返回的view添加到Activity的view上(rootView)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57public class MainActivity extends AppCompatActivity implements IWXRenderListener {
WXSDKInstance mWXSDKInstance;
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);
}
public void onViewCreated(WXSDKInstance instance, View view) {
setContentView(view);
}
public void onRenderSuccess(WXSDKInstance instance, int width, int height) {
}
public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {
}
public void onException(WXSDKInstance instance, String errCode, String msg) {
}
protected void onResume() {
super.onResume();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityResume();
}
}
protected void onPause() {
super.onPause();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityPause();
}
}
protected void onStop() {
super.onStop();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityStop();
}
}
protected void onDestroy() {
super.onDestroy();
if(mWXSDKInstance!=null){
mWXSDKInstance.onActivityDestroy();
}
}
}
- 运行app
运行app
- 扩展Android能力
Weex提供了能力扩展机制,可以根据自己的业务定制自己的功能。主要分为:
- Module 扩展,非UI的特定功能。例如sendHttp、openURL等。
- Component 扩展,实现特别功能的Native控件。例如:RichTextiew, RefreshListview 等。
- Adapter扩展,Weex对一些基础功能实现了统一接口,可实现这些接口来定制自己的业务。例如,图片下载等。