【问题标题】:Android Gradle: Install all build types on one deviceAndroid Gradle:在一台设备上安装所有构建类型
【发布时间】:2016-01-18 23:50:57
【问题描述】:

当使用 GCM、ContentProvider、AccountType 时,如何配置我的项目以在发布版本的同时安装调试版本? (不使用香料)

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

【问题讨论】:

    标签: android android-gradle-plugin android-productflavors android-build-type android-variants


    【解决方案1】:

    如果您只使用构建类型而不是风味,那么在同一设备上安装调试 apk 和发布 apk 会很棘手 (Why Build types and not flavors)

    大多数博文要么是过时的(谈论 packageName)要么是force you to use flavors,因为他们提出的解决方案不支持applicationIdSuffix,而build type 不能声明applicationId,因此您需要使用flavors

    我提出的解决方案使用

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

    为此,我在 Gradle 文件中使用了 applicationIdSuffixmanifest placeholdersBuildConfigFieldresValue

    剩下的唯一问题是当您想为应用程序和每种语言设置不同的名称时,字符串未设置为可翻译 (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 常量。

    AUTHORITY = BuildConfig.AUTHORITY
    

    帐户类型

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

    BuildConfig.ACCOUNT_TYPE
    

    多语言应用名称

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

    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>
    

    ma​​in/values-en/strings.xml

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

    ma​​in/values-fr/strings.xml

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

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 2013-05-22
      • 2023-04-06
      • 1970-01-01
      相关资源
      最近更新 更多