在XML中使用包名称

spe*_*034 13 xml android gradle android-studio android-gradle-plugin

我正在使用Android Studio来构建我的应用程序.我想使用gradlebuildTypes.我使用applicationIdSuffix为包名添加后缀,以修改测试构建类型的包名.

buildTypes {       
    debug {
        runProguard false
        proguardFile 'proguard-rules.txt'
        applicationIdSuffix  '.dev'
        versionNameSuffix  '-dev'
    }
}
Run Code Online (Sandbox Code Playgroud)

是否可以在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"/> 
Run Code Online (Sandbox Code Playgroud)

谢谢!

jen*_*nzz 30

这是Android Gradle插件获取资源文件中占位符的支持之前的另一个临时解决方法.

您可以通过defaultConfig和/或buildTypes和/或productFlavors闭包中的Gradle动态创建String资源:

android {
    defaultConfig {
        applicationId 'com.foo.bar'
        resValue 'string', 'package_name', applicationId
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您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"/> 
Run Code Online (Sandbox Code Playgroud)

  • 很好,我在这个要点中更简单(因为`applicationId`可能会改变)https://gist.github.com/Takhion/74b67cb518e90faf2708 (9认同)

ian*_*ake 9

虽然您可以使用${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"/> 
Run Code Online (Sandbox Code Playgroud)

目前(Android Gradle 0.11)不适用于资源文件.:-(

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

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


Jem*_*rov 8

除了jenzz的答案之外,我必须这样做,因为我的情况就像问题一样,我添加applicationIdSuffix了app的调试版本.

android {
    ...

    defaultConfig {
        applicationId "..."
        ...
    }
    ....

    buildTypes {
        release {
            ...
            resValue 'string', 'application_id', android.defaultConfig.applicationId
        }

        debug {
            ...
            applicationIdSuffix '.debug'
            resValue 'string', 'application_id', android.defaultConfig.applicationId + applicationIdSuffix
        } 
}
Run Code Online (Sandbox Code Playgroud)

然后在任何xml资源中,我可以得到这样的应用程序ID:@string/application_id