3

Activity通过 Android Studio 运行配置在动态功能模块中启动时,我收到以下警告:The activity 'SomeActivity' is not declared in AndroidManifest.xml. (因为它是在AndroidManifest.xml动态功能模块中声明的)。作为参考,这是正在使用的库:

// https://developer.android.com/guide/app-bundle/playcore
api "com.google.android.play:core:1.6.4"

运行配置显示并部署了两个模块,但它只识别来自基本模块的活动AndroidManifest.xml如何在动态功能模块中启动 Activity?


旁注:尝试安装已部署的功能模块时,它似乎没有安装:

I/PlayCore: SplitInstallListenerRegistry : registerListener
I/PlayCore: SplitInstallInfoProvider : No metadata found in AndroidManifest.
I/PlayCore: SplitInstallService : startInstall([feature_module],[])
I/PlayCore: SplitInstallService : Initiate binding to the service.
I/PlayCore: SplitInstallService : ServiceConnectionImpl.onServiceConnected(ComponentInfo{com.android.vending/com.google.android.finsky.splitinstallservice.SplitInstallService})
I/PlayCore: SplitInstallService : linkToDeath
I/PlayCore: SplitInstallService : onError(-5)
I/PlayCore: SplitInstallService : Unbind from service.

Where-5意味着SplitInstallErrorCode.API_NOT_AVAILABLE(可能是因为它是一个调试版本);尽管如此getInstalledModules(),应该找到已部署的功能模块......它没有。SplitInstallInfoProvider : No metadata found in AndroidManifest似乎是这个问题- 当部署“默认 APK”而不是“来自应用程序包的 APK”时,会安装功能模块。

4

1 回答 1

2

由于无法在基本模块的 中引用功能模块活动,因此AndroidManifest.xml我编写了一个SplitInstallActivity,它驻留在debug基本模块的源集中,它也可用于测试。可以使用运行配置调用它,该配置传递启动标志:

-e "moduleName" "feature_module" -e "className" "com.acme.feature.SomeActivity"

它要么安装功能模块moduleName和/或启动Activityby className

这至少在部署“默认 APK”而不是“来自应用程序包的 APK”时有效。

ArgumentKeys.java

public class ArgumentKeys {

    /** {@link SplitInstallActivity} dynamic features, the module name */
    public static final String ARGUMENT_FEATURE_MODULE_MODULE_NAME = "moduleName";

    /** {@link SplitInstallActivity} dynamic features, the activity class name to launch */
    public static final String ARGUMENT_FEATURE_MODULE_CLASS_NAME = "className";
}

SplitInstallActivity.java

/**
 * Split-Install {@link AppCompatActivity}.
 * @author Martin Zeitler
**/
public class SplitInstallActivity extends AppCompatActivity implements SplitInstallStateUpdatedListener {

    private static final String LOG_TAG = SplitInstallActivity.class.getSimpleName();

    private SplitInstallRequest request;
    private SplitInstallManager sim;

    private String moduleName;
    private String className;

    public SplitInstallActivity() {}

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        /* instance the {@link SplitInstallManager}: */
        this.sim = SplitInstallManagerFactory.create(this.getApplicationContext());

        /* obtain the feature module & class name from arguments */
        if(this.getIntent() != null) {
            Bundle extras = this.getIntent().getExtras();
            if(extras != null) {
                this.moduleName = extras.getString(ArgumentKeys.ARGUMENT_FEATURE_MODULE_MODULE_NAME);
                this.className = extras.getString(ArgumentKeys.ARGUMENT_FEATURE_MODULE_CLASS_NAME);
                if(this.moduleName != null && this.className != null) {
                    this.startFeatureActivity(this.moduleName, this.className);
                } else {
                    Log.e(LOG_TAG, "module and class are required.");
                }
            }
        }
    }

    /** it listens for the split-install session state */
    @Override
    public void onStateUpdate(SplitInstallSessionState state) {
        if(state.errorCode() == SplitInstallErrorCode.NO_ERROR && state.status() == SplitInstallSessionStatus.INSTALLED) {
            Log.d(LOG_TAG, "dynamic feature " + this.moduleName + " had been installed.");
            this.startFeatureActivity(this.moduleName, this.className);
        } else {
            // this.OnSplitInstallStatus(state);
        }
    }

    /** it checks if the dynamic feature module is installed and then either installs it - or starts the desired activity */
    private void startFeatureActivity(@NonNull String moduleName, @NonNull String className) {
        if (this.sim.getInstalledModules().contains(moduleName)) {
            Log.d(LOG_TAG, "dynamic feature module " + moduleName + " already installed.");
            Intent intent = this.getIntent();
            intent.setClassName(BuildConfig.APPLICATION_ID, className);
            this.startActivity(intent);
            this.finish();
        } else {
            Log.d(LOG_TAG, "dynamic feature module " + moduleName + " is not installed.");
            this.installFeatureModule(moduleName);
        }
    }

    /** it installs a dynamic feature module on demand */
    private void installFeatureModule(@NonNull String moduleName) {
        Log.d(LOG_TAG, "dynamic feature module " + moduleName + " will be installed.");
        this.request = SplitInstallRequest.newBuilder().addModule(moduleName).build();
        this.sim.registerListener(this);
        this.sim.startInstall(this.request);
    }

    ...
}

Activity可以通过以下方式自动启动特定的ActivityTestRule<?>

@Rule
public ActivityTestRule<SplitInstallActivity> mRule = new ActivityTestRule<SplitInstallActivity>(SplitInstallActivity.class) {

    @Override
    protected Intent getActivityIntent() {
        Intent intent = new Intent();
        Bundle extras = new Bundle();
        extras.putString(ArgumentKeys.ARGUMENT_FEATURE_MODULE_MODULE_NAME, "feature_module");
        extras.putString(ArgumentKeys.ARGUMENT_FEATURE_MODULE_CLASS_NAME, "com.acme.feature.SomeActivity");
        intent.putExtras(extras);
        return intent;
    }
};

为了进行测试,必须在功能模块中提供这些依赖项build.gradle

androidTestDebugImplementation "com.google.android.gms:play-services-basement:17.1.1"
androidTestDebugImplementation "com.google.android.play:core:1.6.4"

否则它无法链接测试应用程序的资源并失败:

> Task :feature_module:processDebugAndroidTestResources FAILED
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"/home/user/.gradle/caches/transforms-2/files-2.1/7435b27a13269cffdd35a7dd69f0b9d2/core-1.6.4/AndroidManifest.xml","position":{"startLine":8,"startColumn":4,"endColumn":277}}],"original":"/home/user/.gradle/caches/transforms-2/files-2.1/7435b27a13269cffdd35a7dd69f0b9d2/core-1.6.4/AndroidManifest.xml:9:5-278: AAPT: error: resource style/Theme.PlayCore.Transparent (aka com.acme.feature.test:style/Theme.PlayCore.Transparent) not found.","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{"file":"/home/user/.gradle/caches/transforms-2/files-2.1/c1b8b45e2f49fbe83ea45d80000bd6e9/jetified-play-services-basement-17.0.0/AndroidManifest.xml","position":{"startLine":22,"startColumn":8,"endLine":24,"endColumn":68}}],"original":"/home/user/.gradle/caches/transforms-2/files-2.1/c1b8b45e2f49fbe83ea45d80000bd6e9/jetified-play-services-basement-17.0.0/AndroidManifest.xml:23:9-25:69: AAPT: error: resource integer/google_play_services_version (aka com.acme.feature.test:integer/google_play_services_version) not found.","tool":"AAPT"}

对于测试,还有:

于 2019-10-30T00:30:56.633 回答