Android 在XML中使用包名

Android 在XML中使用包名,android,xml,gradle,android-studio,android-gradle-plugin,Android,Xml,Gradle,Android Studio,Android Gradle Plugin,我正在使用Android Studio构建我的应用程序。我想使用gradlebuildTypes。我使用applicationIdSuffix为包名添加后缀,以修改测试生成类型的包名 buildTypes { debug { runProguard false proguardFile 'proguard-rules.txt' applicationIdSuffix '.dev' versionNameSuf

我正在使用Android Studio构建我的应用程序。我想使用
gradle
buildTypes。我使用applicationIdSuffix为包名添加后缀,以修改测试生成类型的包名

buildTypes {       
    debug {
        runProguard false
        proguardFile 'proguard-rules.txt'
        applicationIdSuffix  '.dev'
        versionNameSuffix  '-dev'
    }
}
是否可以在xml文件中使用此信息。我的计划是修改帐户类型:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType=<<PACKAGE NAME HERE ??>>
    android:icon="@drawable/icon"
    android:label="@string/app_name"/> 


谢谢

而uou可以使用
${packageName}
在清单中填写生成的包名:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType="${packageName}"
    android:icon="@drawable/icon"
    android:label="@string/app_name"/> 

当前(Android Gradle 0.11)无法处理资源文件:-(

相反,您必须使用不同的帐户类型为所需的每个生成类型/变体创建单独版本的资源文件:

<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
    android:accountType=<<PACKAGE NAME HERE ??>>
    android:icon="@drawable/icon"
    android:label="@string/app_name"/> 
  • src
    • debug/res/xml/authenticator.xml(带有调试包名称)
    • main/res/xml/authenticator.xml(使用默认包名)

    • 在Android Gradle插件实现之前,这里还有一个临时解决方案

      您可以在
      defaultConfig
      和/或
      buildTypes
      和/或
      productFlavors
      闭包中通过Gradle动态创建字符串资源:

      android {
          defaultConfig {
              applicationId 'com.foo.bar'
              resValue 'string', 'package_name', applicationId
          }
      }
      
      然后,您可以在
      authenticator.xml
      文件中引用它,就像您对标签所做的那样:

      <account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
          android:accountType="@string/package_name"
          android:icon="@drawable/icon"
          android:label="@string/app_name"/> 
      

      在jenzz的回答之上,我必须这样做,因为我的案例就像问题一样,我为应用程序的调试版本添加了
      applicationIdSuffix

      android {
          ...
      
          defaultConfig {
              applicationId "..."
              ...
          }
          ....
      
          buildTypes {
              release {
                  ...
                  resValue 'string', 'application_id', android.defaultConfig.applicationId
              }
      
              debug {
                  ...
                  applicationIdSuffix '.debug'
                  resValue 'string', 'application_id', android.defaultConfig.applicationId + applicationIdSuffix
              } 
      }
      

      然后在任何xml资源中,我都可以获得如下的应用程序Id:
      @string/application\u Id

      我刚刚为此创建了一个gradle插件:


      只需包含它,所有XML资源上的占位符都将被替换

      我使用的是
      ${applicationId}
      ,例如:

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.abnerescocio.embaixadordorei.presentation">
      
          ...
      
          <application
          ... 
          >
      
              <provider
                  android:name="androidx.core.content.FileProvider"
                  android:authorities="${applicationId}.android.fileprovider"
                  android:exported="false"
                  android:grantUriPermissions="true">
                  <meta-data
                      android:name="android.support.FILE_PROVIDER_PATHS"
                      android:resource="@xml/file_paths"/>
              </provider>
      
          ...
      
          </application>
      
      </manifest>
      
      
      ...
      ...
      
      一般来说,如果我阅读文档,这似乎可以解决问题。但gradle没有替换该值?我是否需要做任何其他工作?我可以在资源文件中也使用占位符吗?通过进一步查看,占位符似乎当前仅可用于清单文件。我已编辑了我的答案,说明了如何使用占位符您可以绕过资源文件的限制。如果以后有人看到此主题,则表示有人已请求此功能:您不需要复制整个(xml)资源,您可以在调试和主资源中定义两个
      值/strings.xml
      ,并在那里给出不同的字符串值,然后只引用
      accountType=“@string/packageName”
      。很好的一个,我在这个要点中做得更简单了(因为
      applicationId
      可能会更改)@takion您将该代码放在项目中的何处?编辑:您可以将其放在应用程序的build.gradle中。由于toLowerCase,我的资源文件无法保存found@Cheshire将该代码片段放在Android项目的
      build.gradle
      中:)您能否添加一个代码示例,说明如何使用插件并让它替换占位符?Nvm在不同的组合中使用了相同的
      manifestPlaceholders
      内置类型和样式,并正确地替换了占位符。谢谢。这只适用于清单,不适用于其他XML文件。这似乎不适用于快捷方式XML中的android:targetPackage,例如,
      android:targetPackage
      ,因为它需要实际的字符串而不是引用。适用于我的首选项android:targetPackage。idSuffix的好答案!!!