现有的答案都没有令我满意,但 Liberty 已经很接近了。这就是我正在做的事情。
首先,目前我正在合作:
- Android Studio 测试版 0.8.2
- Gradle 插件 0.12.+
- 等级 1.12
My goal是运行Debug
版本以及Release
在同一设备上使用相同的版本ContentProvider
.
In 构建.gradle您的应用程序设置后缀用于调试构建:
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
In AndroidManifest.xml文件集android:authorities
你的财产ContentProvider
:
<provider
android:name="com.example.app.YourProvider"
android:authorities="${applicationId}.provider"
android:enabled="true"
android:exported="false" >
</provider>
In your code set AUTHORITY
可以在实现中任何需要的地方使用的属性:
public static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
Tip:之前是BuildConfig.PACKAGE_NAME
就是这样!它会像魅力一样发挥作用。如果您使用 SyncAdapter,请继续阅读!
SyncAdapter 更新 (2014 年 11 月 14 日)
我将再次从当前的设置开始:
- Android Studio 测试版 0.9.2
- Gradle 插件 0.14.1
- 摇篮2.1
基本上,如果您需要为不同的构建自定义一些值,您可以从 build.gradle 文件中完成:
- use 构建配置字段从访问它
BuildConfig.java
class
- use resValue从资源中访问它,例如@字符串/你的值
作为资源的替代方案,您可以创建单独的 buildType 或风味目录并覆盖其中的 XML 或值。但是,我不会在下面的示例中使用它。
Example
In 构建.gradle文件添加以下内容:
defaultConfig {
resValue "string", "your_authorities", applicationId + '.provider'
resValue "string", "account_type", "your.syncadapter.type"
buildConfigField "String", "ACCOUNT_TYPE", '"your.syncadapter.type"'
}
buildTypes {
debug {
applicationIdSuffix ".debug"
resValue "string", "your_authorities", defaultConfig.applicationId + '.debug.provider'
resValue "string", "account_type", "your.syncadapter.type.debug"
buildConfigField "String", "ACCOUNT_TYPE", '"your.syncadapter.type.debug"'
}
}
您将看到结果构建配置.java class
public static final String ACCOUNT_TYPE = "your.syncadapter.type.debug";
and in 构建/生成/res/生成/调试/值/生成.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Automatically generated file. DO NOT MODIFY -->
<!-- Values from default config. -->
<item name="account_type" type="string">your.syncadapter.type.debug</item>
<item name="authorities" type="string">com.example.app.provider</item>
</resources>
In your 验证器.xml使用 build.gradle 文件中指定的资源
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="@string/account_type"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name"
/>
In your 同步适配器.xml再次使用相同的资源并且@字符串/权威机构 too
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="@string/authorities"
android:accountType="@string/account_type"
android:userVisible="true"
android:supportsUploading="false"
android:allowParallelSyncs="false"
android:isAlwaysSyncable="true"
/>
Tip:自动完成(Ctrl+空格)不适用于这些生成的资源,因此您必须手动输入它们