How to use the manifest command "replace" to replace an activity from the main package by an activity with the same name but in a flavor package?
com.name.project/main/
-ActivityA
replace by
com.name.project/pro/
-ActivityA
How to use the manifest command "replace" to replace an activity from the main package by an activity with the same name but in a flavor package?
com.name.project/main/
-ActivityA
replace by
com.name.project/pro/
-ActivityA
You can do this by creating an activity alias for each activity you want to override, and then overriding the alias's target_activity in the flavor's manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app">
<application>
<activity
android:name=".MainActivity"
/>
<activity-alias
android:name="${applicationId}.aliasMain"
android:targetActivity=".MainActivity"/>
</application>
</manifest>
It's important to use ${applicationId} when declaring the alias if your flavors have different package names. This is because you launch an activity alias using the built package name.
Change the intents that launch your activity so that they launch it using the alias
Intent intent = new Intent(); String packageName = context.getPackageName(); ComponentName componentName = new ComponentName(packageName, packageName + ".aliasMain"); intent.setComponent(componentName);
In the flavor's manifest, declare the replacement activity, override the alias's target activity to point at this new activity, and remove the base activity declaration so it will be an error if we try to launch it:
<application>
<activity
android:name=".flavor.MainActivity"/>
<activity-alias
tools:replace="android:targetActivity"
android:name="${applicationId}.aliasMain"
android:targetActivity=".flavor.MainActivity"/>
<activity
tools:node="remove"
android:name=".MainActivity"
/>
</application>
Note that the package name in the manifest tag is the base package name "com.company.app" instead of the flavor's package "com.company.app.flavor". I do this intentionally so I don't have to write out a whole package name anywhere else in the manifest.
© 2022 - 2024 — McMap. All rights reserved.