Question

Im trying to build two diffrent Android apps derived from the same code base using a library project.

The apps are using a intent service for various operations, however this seems to fail when I have two apps using the same code base installed simultaneously.

It seems as if only the first version of the app I install works, the second one don't seem to get the service to run.

Does anyone have any experience of this? How could this be solved?

EDIT: These are the manifests

Base:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codebase"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="CodeBase" android:process="@string/app_name" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar" android:debuggable="false">
        <activity android:configChanges="orientation" android:name="com.codenase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />
            </intent-filter>
        </service>

</manifest>

Target 1:

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="Target One" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar">
        <activity android:configChanges="orientation" android:name="com.codebase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />

            </intent-filter>
        </service>

        </activity>

    </application>

</manifest>

Target 2:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trg2"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="Target 2" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar">
        <activity android:configChanges="orientation" android:name="com.codebase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.codebase.service.UpdateService">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />
            </intent-filter>
        </service>

    </application>


</manifest>
Was it helpful?

Solution

First, there is no need to define intent-filter for the service in AndroidManifest.xml in this situation, the following definition is sufficient if you don't need provide additional access point from outside your app:

<service android:name="com.codebase.service.UpdateService"/>

Then using public Intent (Context packageContext, Class cls) to start service:

Intent intent = new Intent(getBaseContext(), UpdateService.class);
startService(intent);

Second, if you do need provide additional access point from outside your app, don't use the same action name in multiple app, as it will fool Android OS when try to pass the intent to the corresponding service (in case if you use public Intent (String action) to start your service):

Target 1:

<service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
  <intent-filter>
    <action android:name="com.codebase.service.UpdateService.TARGET_1" />
  </intent-filter>
</service>

Target 2:

<service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
  <intent-filter>
    <action android:name="com.codebase.service.UpdateService.TARGET_2" />
  </intent-filter>
</service>

Hope this helps.

OTHER TIPS

Ensure you have qualified your services correctly in the manifest.

e.g

if you had:

Main Project package name = com.johan.p1

Library Project package name = com.johan.library

Assuming your service is in the library project, your main project would define the service as:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.johan.p1"

     <service android:name="com.johan.library.MyServiceName" />

</manifest>

I have two versions of the same android app based on one code base as you do.

I don't have any problems. Both app have different package names

However, I ensure that both have the same (shared) userId. Maybe that is the trick

http://developer.android.com/guide/topics/manifest/manifest-element.html

(By the way, when I remember right, the userId required to have a dot in between)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top