回答

7

安装调试的APK,并在同一设备上发布APK是棘手的,如果你只使用生成类型,而不是口味( Why Build types and not flavors

大多数博客文章或者是过时的(谈论的packageName)或force you to use flavors,因为他们提出的解决方案不支持applicationIdSuffixbuild type不能因此而宣布applicationId你需要使用一个flavors

我提出的解决方案使用

  • 每构建类型的权威
  • 每构建类型
  • 每构建类型GCM许可

帐户类型对于这个工作我用applicationIdSuffixmanifest placeholdersBuildConfigFieldresValue在Gradle文件中。

唯一剩下的问题是,当你想有一个不同的名称为应用程序和每种语言的字符串没有被设置为翻译(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" 
    .../> 

的ContentProvider

我有一个常数管理局这需要它从BuildConfig。

AUTHORITY = BuildConfig.AUTHORITY 

账户类型

为了让你把它从BuildConfig太帐户类型。

BuildConfig.ACCOUNT_TYPE 

多语言应用程序的名称

如果你想每个应用程序不同的名字&语言:

调试/值恩/ 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> 

主/值烯/ strings.xml中

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

主/值-FR/strings.xml中

<resources> 
    <string name="app_name">MyApp FR</string> 
</resources> 
相关问题