Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Auto Setup with Gradle Build Flavors

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?

like image 350
mattfred Avatar asked Sep 28 '17 18:09

mattfred


2 Answers

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.

like image 99
rekire Avatar answered Oct 01 '22 23:10

rekire


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>
like image 23
James McCracken Avatar answered Oct 01 '22 23:10

James McCracken