假设我的基本应用程序 A 带有 com.package.a 包名称,而 B 带有 com.package.b 是我的动态应用程序功能,将在安装基本 apk 后我的基本应用程序。 了解更多 dynamic Feature 现在我在我的 B(动态功能项目)中有一个布局,我想在我的基本应用程序 A 中访问它。我试过 this但它对我不起作用。
这是我想从动态特征应用程序 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>
这就是我在我的 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;
}
});
我只是在检查是否安装了动态功能。如果已安装,则我将 contentView 设置为动态功能的 com.package.B 中的布局。
最佳答案
如果您在本地进行测试,onDemand
模块将不会通过 PlayCore API
加载。
通常,在您可以从 onDemand 模块访问代码/资源之前,您需要确保 SplitCompat.install(context)
像这样在 attachBaseContext
中被调用或类似的:
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(newBase)
SplitCompat.install(this)
}
同时确保应用程序在查询包管理器时仍然是a
。
manager.getResourcesForApplication("com.package.a");
更有可能为给定资源产生结果。
从 UX 的角度来看,不建议在显示初始屏幕时下载 onDemand 模块,然后替换该 Activity 中的 View 。
也就是说,请检查 catch block 中的异常日志。通过 Log.v
而不是 e.printStackTrace()
记录它。
关于java - 在基本应用程序中使用动态应用程序功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322435/