>假设我的基本应用程序 A 带有 com.package.a 包名称,B 带有 com.package.b 是我的动态应用程序功能,将在安装基本 apk 后下载到我的基本应用程序中。了解有关动态功能的详细信息现在,我的 B(动态功能项目)中有一个布局,我想在我的 Base 应用程序 A 中访问该布局。我试过这个,但它对我不起作用。
这是我想要从动态功能应用程序 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>
这就是我在活动中的做法
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;
}
});
我只是在检查是否安装了动态功能。如果已安装,那么我将内容视图设置为动态功能的com.package.B中存在的布局。
如果您在本地测试此功能,则不会通过PlayCore API
加载onDemand
模块。
通常,在从 onDemand 模块访问代码/资源之前,您需要确保在类似或类似attachBaseContext
中调用SplitCompat.install(context)
:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase)
SplitCompat.install(this)
}
还要确保在查询包管理器时仍a
应用程序。
manager.getResourcesForApplication("com.package.a");
更有可能为给定资源生成结果。
从 UX 的角度来看,不建议在显示初始屏幕时下载按需模块,然后替换该活动中的视图。
话虽如此,请检查 catch 块中的异常日志。通过Log.v
而不是e.printStackTrace()
记录它。