繁体   English   中英

Android Gradle:在一台设备上安装所有构建类型

[英]Android Gradle: Install all build types on one device

在使用GCM,ContentProvider,AccountType时,如何配置项目以便能够在发布版本旁安装调试版本? (不使用口味)

我一直收到错误,例如: INSTALL_FAILED_CONFLICTING_PROVIDERINSTALL_FAILED_DUPLICATE_PERMISSION

如果你只使用构建类型而不是风格( 为什么构建类型而不是风味 ),在同一设备上安装调试apk和发行版apk是棘手的

大多数博客文章要么过时(谈论packageName)要么强迫你使用flavor,因为他们提出的解决方案不支持applicationIdSuffixbuild type不能声明applicationId因此你需要使用flavors

我建议使用的解决方案

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

为此,我在Gradle文件中使用applicationIdSuffixmanifest占位符BuildConfigFieldresValue

剩下的唯一问题是当你想为app和每种语言设置一个不同的名字时,字符串没有被设置为可翻译( bug aosp tracker )这会强制你设置abortOnError false否则你将无法进行发布版本。

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

<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"
    .../>

内容提供商

我有一个常量,用于从BuildConfig获取它。

AUTHORITY = BuildConfig.AUTHORITY

帐户类型

要获取帐户类型,您也可以从BuildConfig中获取它。

BuildConfig.ACCOUNT_TYPE

多语言应用名称

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

调试/值恩/ strings.xml中

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

调试/值-FR / strings.xml中

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

主/值恩/ strings.xml中

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

主/值-FR / strings.xml中

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM