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

Ben*_*oit 4 android android-gradle-plugin android-productflavors android-build-type android-variants

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

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

Ben*_*oit 7

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

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

我建议使用的解决方案

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

对于这个工作,我使用applicationIdSuffix,明显占位符,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
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

同步适配器xml

<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="@string/authority"
    android:accountType="@string/account_type"/>
Run Code Online (Sandbox Code Playgroud)

帐户验证者

<account-authenticator
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="@string/account_type"
    .../>
Run Code Online (Sandbox Code Playgroud)

内容提供商

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

AUTHORITY = BuildConfig.AUTHORITY
Run Code Online (Sandbox Code Playgroud)

帐户类型

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

BuildConfig.ACCOUNT_TYPE
Run Code Online (Sandbox Code Playgroud)

多语言应用名称

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

调试/值恩/ strings.xml中

<resources>
    <string name="app_name">MyApp debug EN</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

调试/值-FR/strings.xml中

<resources>
    <string name="app_name">MyApp debug FR</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

主/值恩/ strings.xml中

<resources>
    <string name="app_name">MyApp EN</string>
</resources>
Run Code Online (Sandbox Code Playgroud)

主/值-FR/strings.xml中

<resources>
    <string name="app_name">MyApp FR</string>
</resources>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

1449 次

最近记录:

9 年,10 月 前