在XML中使用包名称

时间:2014-06-22 23:05:24

标签: android xml gradle android-studio android-gradle

我正在使用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"/> 

谢谢!

4 个答案:

答案 0 :(得分:29)

这是Android Gradle插件获取support for placeholders in resource files之前的另一个临时解决方法。

您可以通过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"/> 

答案 1 :(得分:8)

虽然您可以使用${packageName}在Manifest中填写生成的包名称:

<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)不能处理资源文件。 : - (

相反,您必须为具有不同帐户类型的每种构建类型/变体创建单独的资源文件版本:

  • SRC
    • debug / res / xml / authenticator.xml(带调试包名称)
    • main / res / xml / authenticator.xml(使用默认包名称)

答案 2 :(得分:6)

除了 jenzz 之外,我必须这样做,因为我的情况就像问题一样,我为app的调试版添加了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_id

答案 3 :(得分:0)

我刚为此创建了一个gradle插件:com.inutilfutil.android-resource-placeholder

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