Android Auto Setup with Gradle Build Flavors
Asked Answered
H

3

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?

Hanna answered 28/9, 2017 at 18:56 Comment(13)
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.Obeded
@AnuragSingh - I have updated my question with the default config.Hanna
Did you try the suggestion that I shared regarding the application suffix and then try creating a build using regular flavor.Obeded
I did. However, this will make the final package id 'com.me.me', correct?Hanna
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.Obeded
@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.Hanna
You can try removing the applicationId from regular. And trying adding applicationIdSuffix in different flavor onlyObeded
Let us continue this discussion in chat.Obeded
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?Daladier
Did you happen to make it work?Obeded
No. I have not been able to figure this out.Hanna
Try to change applicationid to applicationIdSuffixBehlke
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'Hanna
I
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.

Ignoramus answered 3/10, 2017 at 11:23 Comment(1)
declaration of applicationId is not related to the package name of the code, they are two different fields.Daladier
S
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>
Sambar answered 5/10, 2017 at 20:33 Comment(0)
B
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.

Buzzer answered 7/10, 2017 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.