简体   繁体   中英

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. 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.

This is the layout I want to access from dynamic feature application 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.

Please anyone help in this.

In case you're testing this locally, the onDemand module won't be loaded through the 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:

    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.

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.

That being said, please check the exception log in the catch block. Logging it via Log.v rather than e.printStackTrace() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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