9

I have a project where I am attempting to add Android Auto support. I have added the following code to my manifest as shown in the Auto documentation:

<application
    ....
    <meta-data android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc"/>
    ....
    <service
        android:name="com.me.auto.MyMediaBrowserService"
        android:exported="false">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService" />
        </intent-filter>
    </service>
    ....
</applicaiton>

I'm also using different build flavors, defined in my gradle.build file:

defaultConfig {
    applicationId "com.me"
    minSdkVersion 16
    //noinspection OldTargetApi
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}

productFlavors {

    regular {
        applicationId "com.me"
    }
    different {
        applicationId "com.meother"
    }
}

When I build and install using the 'regular' flavor, android auto does not work. However, when I build and install using the 'different' flavor, everything works great. If I then change the regular applicaitonId to something else like 'com.menew', again Auto works great.

How is the applicationId in the build flavor making or breaking Android Auto functionality?

mattfred
  • 2,689
  • 1
  • 22
  • 38
  • Show me your defaultConfig{} definition and package name. Also try replacing regular { applicationId "com.me" } to regular { applicationIdSuffix ".me" }. It is quiet possible that your package name is conflicting with your applicationId. com.me is sub package of com.me.auto package. – Anurag Singh Oct 03 '17 at 06:57
  • @AnuragSingh - I have updated my question with the default config. – mattfred Oct 03 '17 at 15:41
  • Did you try the suggestion that I shared regarding the application suffix and then try creating a build using regular flavor. – Anurag Singh Oct 03 '17 at 15:45
  • I did. However, this will make the final package id 'com.me.me', correct? – mattfred Oct 03 '17 at 15:52
  • Yes, you are very correct but Android auto should work. If it works then it's just the collision between applicationId names. Do let me know the result. – Anurag Singh Oct 03 '17 at 15:57
  • @AnuragSingh - yes it does work using the applicationIdSuffix. So what do I need to do to make this work with a final package id of 'com.me'? This is an application that is already in production, so I can't change the package id. – mattfred Oct 03 '17 at 16:07
  • You can try removing the applicationId from regular. And trying adding applicationIdSuffix in different flavor only – Anurag Singh Oct 03 '17 at 16:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155866/discussion-between-anurag-singh-and-mattfred). – Anurag Singh Oct 04 '17 at 05:30
  • Could you add more details about the error that you're facing? Stacktrace? Is it an error happening within Android Studio or within the device? – ahasbini Oct 04 '17 at 14:55
  • Did you happen to make it work? – Anurag Singh Oct 05 '17 at 08:09
  • No. I have not been able to figure this out. – mattfred Oct 05 '17 at 13:13
  • Try to change applicationid to applicationIdSuffix – Amjad Khan Oct 05 '17 at 14:14
  • Yes I tried this, as mentioned above. However, this changes the applicationId, which is not a valid fix for me. I need this to work with the applicaitonId of 'com.me' – mattfred Oct 05 '17 at 14:58

3 Answers3

1

I am not absolutely sure, but I would guess this is related with the application id, e.g. you can make avoid full qualified package names by using the relative names you can use it in the manifest all all places. Check this:

<service
    android:name="com.me.auto.MyMediaBrowserService" ...>

vs.

<service
    android:name=".auto.MyMediaBrowserService" ...>

Make also sure that you have no hard coded packages in your code always use BuildCondig.APPLICATION_ID when you need your package name.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • declaration of ```applicationId``` is not related to the package name of the code, they are two different fields. – ahasbini Oct 04 '17 at 14:53
1

Looks like you have it mostly right. I would recommend these changes (based on https://developer.android.com/training/auto/audio/index.html) and see if this fixes it.

1) Remove the package name so it's not locked into one flavor. Alternatively, you can use ${applicationId} and gradle will insert the correct one.

2) Set the service to be exported (android:exported=true).

<application
    ....
    <meta-data android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc"/>
    ....
    <service
        android:name="${applicationId}.auto.MyMediaBrowserService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.media.browse.MediaBrowserService" />
        </intent-filter>
    </service>
    ....
</applicaiton>
James McCracken
  • 15,488
  • 5
  • 54
  • 62
1

Did you try to create a flavorDimensions?

You can try this.

flavorDimensions "mode"
productFlavors {
    regular {
        dimension = "mode"
    }
    different {
        dimension = "mode"
    }
}

if you want to get the version of your application

 if (BuildConfig.Flavor.contains("regular") || BuildConfig.Flavor.contains("different")) {
       // Your code goes here.
 }

Hope this will help.

dotGitignore
  • 1,597
  • 2
  • 15
  • 34