Android Gradle:在一台设备上安装所有构建类型 [英] Android Gradle: Install all build types on one device

本文介绍了Android Gradle:在一台设备上安装所有构建类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用GCM,ContentProvider和AccountType时,如何配置项目以使其能够与发布版本一起安装调试版本? (不使用调味剂)

How do I configure my project to be able to install the debug version alongside the release version when using GCM, ContentProvider, AccountType? (without the use of flavors)

我不断收到错误消息,例如: INSTALL_FAILED_CONFLICTING_PROVIDER

I keep getting errors such as: INSTALL_FAILED_CONFLICTING_PROVIDER or INSTALL_FAILED_DUPLICATE_PERMISSION

推荐答案

如果仅使用构建类型而不使用风味(

Installing a debug apk and the release apk on the same device is tricky if you are only using build types and not flavors (Why Build types and not flavors)

大多数博客文章要么过时(谈论packageName),要么是

Most blog post are either outdated (talking about packageName) or force you to use flavors because the solution they propose does not support applicationIdSuffix and a build type cannot declare applicationId therefore you need to use a flavors

我建议的解决方案使用

  • 每种构建类型的权限
  • 每种构建类型的帐户类型
  • 每种构建类型的GCM权限

为此,我使用applicationIdSuffix清单文件中的占位符BuildConfigFieldresValue.

剩下的唯一问题是,当您想为应用使用不同的名称并且每种语言都未将字符串设置为可翻译时(

The only problem left is when you want to have a different name for app and per language the string is not set as translatable (bug aosp tracker) This forces you to set abortOnError false otherwise you won't be able to make a release build.

build.gradle

project.ext {
    defaultApplicationId = "com.myapp.package"
}
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId defaultApplicationId

        manifestPlaceholders = [ applicationIdWithSuffix: "${applicationId}" ]

        buildConfigField "String", "ACCOUNT_TYPE", "\"${applicationId}\""
        buildConfigField "String", "AUTHORITY", "\"${applicationId}.provider\""

        resValue "string", "account_type", "${applicationId}"
        resValue "string", "authority", "${applicationId}.provider"
    }
    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            debuggable true

            manifestPlaceholders = [ applicationIdWithSuffix: defaultApplicationId + ".debug" ]

            buildConfigField "String", "ACCOUNT_TYPE",  "\"${defaultApplicationId}.debug\""
            buildConfigField "String", "AUTHORITY", "\"${defaultApplicationId}.debug.provider\""

            resValue "string", "account_type", "${defaultApplicationId}.debug"
            resValue "string", "authority", "${defaultApplicationId}.debug.provider"
        }
    }
    lintOptions {
        abortOnError false
    }
}

AndroidManifest.xml

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mypackage" >

    <permission
        android:name="${applicationIdWithSuffix}.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="${applicationIdWithSuffix}.permission.C2D_MESSAGE" />

    <application
        android:label="@string/app_name" >

        <provider
            android:name=".MyContentProvider"
            android:authorities="${applicationIdWithSuffix}.provider"
            android:exported="false"
            android:multiprocess="true" />
    </application>
</manifest>

同步适配器xml

<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/authority"
    android:accountType="@string/account_type"/>

帐户身份验证器

<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/account_type"
    .../>

ContentProvider

我有一个Authority常量,它从BuildConfig中获取.

I have a constant for Authority which takes it from the BuildConfig.

AUTHORITY = BuildConfig.AUTHORITY

帐户类型

要获取帐户类型,也可以从BuildConfig中获取.

To get the account type you take it from the BuildConfig too.

BuildConfig.ACCOUNT_TYPE

多语言应用名称

如果您希望每个应用使用不同的名称&语言:

If you want different names per app & language:

debug/values-en/strings.xml

<resources>
    <string name="app_name">MyApp debug EN</string>
</resources>

debug/values-fr/strings.xml

<resources>
    <string name="app_name">MyApp debug FR</string>
</resources>

main/values-en/strings.xml

<resources>
    <string name="app_name">MyApp EN</string>
</resources>

main/values-fr/strings.xml

<resources>
    <string name="app_name">MyApp FR</string>
</resources>

这篇关于Android Gradle:在一台设备上安装所有构建类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆