I have written different flavors for my application and I have a service which I would like to run for just one of these.
How I could implement it?
I have written different flavors for my application and I have a service which I would like to run for just one of these.
How I could implement it?
Solution 1
if("debug".equals(BuildConfig.FLAVOR) {
// Start service A
} else if ("release".equals(BuildConfig.FLAVOR) {
// Start service B
}
Or
if("debug".equals(BuildConfig.FLAVOR) {
// Start service A onfly if using "debug" flavor
}
Solution 2
You can also use the method above to abort the service for specific flavors:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if("debug".equals(BuildConfig.FLAVOR) {
stopSelf();
}
....
}
Solution 3
Also, you can have different AndroidManifest.xml
for different flavors:
Base AndroidManifest.xml
Edit the file at: app/src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
...
<service android:name="com.test.Service" />
</application>
</manifest>
Flavor Specific AndroidManifest.xml
Edit/Create file at app/src/FLAVOR_A/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest>
<application>
...
<service android:name="com.test.Service" android:enabled="true" tools:node="merge"/>
</application>
</manifest>
This will add the flag android:enabled="true"
to FLAVOR_A
only.
You can find more info about mergin Manifest files here