简体   繁体   English

在基本应用程序中使用动态应用程序功能

[英]Using Dynamic App feature in Base application

Suppose I have my base application A with com.package.a package name and B with com.package.b is my Dynamic App feature which will be downloaded in my base application after installing the base apk. 假设我的基本应用程序A带有com.package.a软件包名称,而B带有com.package.b则是我的动态应用程序功能,它将在安装基本apk之后下载到我的基本应用程序中。 Know more about dynamic Feature Now I have a layout in my B (dynamic feature project) which i want to access in my Base application A. I tried this but It's not working for me. 了解更多关于动态特性现在我在B中的布局(动态特性项目),我想在我的基本应用答:我试图访问这个 ,但它不是为我工作。

This is the layout I want to access from dynamic feature application B : 这是我要从动态功能应用程序B访问的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/lottie_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".LottieAnimationActivity">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/lottie_animation_view"
        android:layout_width="match_parent"
        android:background="@color/white"
        app:lottie_fileName="animation.json"
        android:layout_height="wrap_content" />

</RelativeLayout>

And this is way I'm doing it in my Activity 这就是我在“活动”中这样做的方式

public class SplashActivity extends Activity {
@BindView(R.id.splash_logo)
ImageView splash_logo;

private int sessionID;
private boolean dynamicModule = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SplitInstallManager splitInstallManager = SplitInstallManagerFactory.create(this);
        SplitInstallRequest request = SplitInstallRequest
                .newBuilder()
                .addModule("lottie")
                .build();
        SplitInstallStateUpdatedListener listener = new SplitInstallStateUpdatedListener() {
            @Override
            public void onStateUpdate(SplitInstallSessionState splitInstallSessionState) {
                if(splitInstallSessionState.sessionId() == sessionID) {
                    switch (splitInstallSessionState.status()) {
                        case SplitInstallSessionStatus.INSTALLED:
                            Log.v("lottie", "lottie Module installed");

                            try
                            {
                                PackageManager manager = getPackageManager();
                                Resources resources = manager.getResourcesForApplication("com.package.b");
                                int resId = resources.getIdentifier("lottie_animation_view", "layout", "com.package.b");
                                RelativeLayout alayout = (RelativeLayout) resources.getLayout(resId);
                                setContentView(resId);

                                }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                                setContentView(R.layout.activity_splash);
                                Toast.makeText(SplashActivity.this, "error", Toast.LENGTH_LONG).show();

                            }
                            break;
                        case SplitInstallSessionStatus.CANCELED:
                            // TODO
                            break;
                        case SplitInstallSessionStatus.DOWNLOADED:
                            Toast.makeText(SplashActivity.this, " Downloaded but not installed", Toast.LENGTH_LONG).show();

                            // TODO
                            break;
                        case SplitInstallSessionStatus.PENDING:
                            // TODO
                            break;
                        case SplitInstallSessionStatus.FAILED:
                            // TODO
                            setContentView(R.layout.activity_splash);
                            break;
                        case SplitInstallSessionStatus.DOWNLOADING:
                            setContentView(R.layout.activity_splash);
                            break;
                    }
                }
            }
        };


        splitInstallManager.registerListener(listener);

        splitInstallManager.startInstall(request)
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                    }
                })
                .addOnSuccessListener(new OnSuccessListener<Integer>() {
                    @Override
                    public void onSuccess(Integer sessionId) {
                        sessionID = sessionId;
                    }
                });

I'm just checking if the dynamic feature is installed or not. 我只是在检查动态功能是否已安装。 If it is installed then I'm setting the contentView as the layout present in the com.package.B of dynamic feature. 如果已安装,则将contentView设置为动态功能com.package.B中的布局。

Please anyone help in this. 请任何人帮助。

In case you're testing this locally, the onDemand module won't be loaded through the PlayCore API . 如果您要在本地进行测试, onDemand模块将不会通过PlayCore API加载。

Generally, before you can access code / resources from an onDemand module, you'll have make sure SplitCompat.install(context) is called in attachBaseContext like this or similar: 通常,在可以从onDemand模块访问代码/资源之前,必须确保在attachBaseContext调用SplitCompat.install(context) ,如下所示:

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(newBase)
        SplitCompat.install(this)
    }

Also make sure that the application still will be a when querying the package manager. 另外,还要确保应用程序仍然将是a查询包管理器时。

manager.getResourcesForApplication("com.package.a");

is more likely to yield a result for the given resource. 对于给定的资源更有可能产生结果。

From a UX perspective it's not recommendable to download an onDemand module while a splash screen is displayed and then replace views in that Activity. 从UX角度来看,建议不要在显示初始屏幕时下载onDemand模块,然后替换该活动中的视图。

That being said, please check the exception log in the catch block. 话虽如此,请检查catch块中的异常日志。 Logging it via Log.v rather than e.printStackTrace() . 通过Log.v而不是e.printStackTrace()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM