当前位置: 首页 > 工具软件 > Apache Weex > 使用案例 >

weex sdk 安卓 android集成

林君博
2023-12-01

weex sdk集成到安卓,可以参考官网文档,但是会报错;这里总结一下可用的方法及方式;最后提供demo下载可以直接使用,不过为helloword版本;
其中weex开发网页部分,需要自行进行后续处理。

官方网址:week集成

  1. manifest
    添加权限:
<!--    网络-->
    <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" />

生命application

<application
        android:name=".MyApplication"
  1. gradle
    增加阿里的镜像,这样下载更快
repositories {
        google()
        jcenter()
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'}
    }
plugins {
    id 'com.android.application'
}
apply from: 'https://www.mobibrw.com/wp-content/uploads/2019/11/download_jsc.gradle'

android {
    compileSdkVersion 29
    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "tv.newtv.weexdemo"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation fileTree(dir: "libs", include: ["*.jar"])



    // 以下两个 weex_sdk 版本二选一
//    implementation 'org.apache.weex:sdk:0.28.0'
    implementation 'org.apache.weex:sdk_legacy:0.28.0'

    // fastjson
    implementation 'com.alibaba:fastjson:1.1.46.android'

    //support library dependencies


  1. application
public class MyApplication extends Application {
    private static final String TAG = "MyApplication";
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: .......");
        InitConfig config = new InitConfig.Builder()
                .setHttpAdapter(new DefaultWXHttpAdapter()).build();
        WXSDKEngine.initialize(this, config);

        WXBridgeManager.updateGlobalConfig("wson_on");
        WXEnvironment.setOpenDebugLog(true);
        WXEnvironment.setApkDebugable(true);
        WXSDKEngine.addCustomOptions("appName", "WXSample");
        WXSDKEngine.addCustomOptions("appGroup", "WXApp");

    }
}
  1. mainactivity
public  class MainActivity extends AppCompatActivity implements IWXRenderListener {

    private static final String TAG = "MainActivity";

    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);
        getSupportActionBar().hide();
        wxsdkInstance = new WXSDKInstance(this);
        wxsdkInstance.registerRenderListener(this);
        if (WXSDKEngine.isInitialized()) {
            startRender();
        } else {
            handler.sendEmptyMessageDelayed(1, 300);
        }


        setContentView(R.layout.activity_main);
    }

    /**
     * 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 wxsdkInstance, View view) {
        /**
         * 填充视图
         */
        setContentView(view);
        Log.d(TAG, "onViewCreated: .......");
    }

    @Override
    public void onRenderSuccess(WXSDKInstance wxsdkInstance, int i, int i1) {

    }

    @Override
    public void onRefreshSuccess(WXSDKInstance wxsdkInstance, int i, int i1) {

    }

    @Override
    public void onException(WXSDKInstance wxsdkInstance, String errCode, String msg) {
        Log.d("welog", "onException: " + errCode + "//" + msg);
    }

  1. demo地址

demo

 类似资料: