Android Manifest How to replace Activity by Flavor Activity
Asked Answered
D

1

7

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
Denning answered 4/12, 2016 at 22:20 Comment(5)
I believe this is a repeated question you should be able to find your result here #32324713Kulda
This works only for the mainactivityDenning
Can you explain better whats the purpose? Is the code from activityA same as main ? so why you want to replace?Kulda
Ive got two flavors (free and paid). The Activities in the paid-package extend the default ones in the main(no flavor) and have additionally some licensing implementation that only the paid-flavor needs.Denning
So then I would create a baseActivityA in main and create a ActivityA in free flavor and other in the pro flavor and both extend the BaseActivityA in mainKulda
T
4

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.

  1. Add an alias for your activity to the main 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.

  1. 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);

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

Thesis answered 30/5, 2018 at 20:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.