9

我有一个项目,我正在尝试添加 Android Auto 支持。我已将以下代码添加到我的清单中,如 Auto 文档中所示:

<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>

我还使用了不同的构建风格,在我的 gradle.build 文件中定义:

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

productFlavors {

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

当我使用“常规”风格构建和安装时,android auto 不起作用。但是,当我使用“不同”风格构建和安装时,一切都很好。如果我然后将常规更改为applicaitonId“com.menew”之类的其他内容,那么 Auto 再次运行良好。

构建风格如何applicationId制作或破坏 Android Auto 功能?

4

3 回答 3

1

您是否尝试创建风味维度?

你可以试试这个。

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

如果您想获取应用程序的版本

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

希望这会有所帮助。

于 2017-10-07T10:57:15.153 回答
1

看起来你基本上是对的。我会推荐这些更改(基于https://developer.android.com/training/auto/audio/index.html),看看是否可以解决它。

1)删除包名称,使其不会被锁定为一种口味。或者,您可以使用${applicationId}gradle 将插入正确的。

2) 设置要导出的服务(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>
于 2017-10-05T20:33:22.477 回答
1

我不确定,但我猜这与应用程序 ID 有关,例如,您可以通过使用相对名称来避免完全限定的包名称,您可以在所有地方的清单中使用它。检查这个:

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

对比

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

还要确保你的代码中没有硬编码的包,BuildCondig.APPLICATION_ID当你需要你的包名时总是使用它。

于 2017-10-03T11:23:59.103 回答