Android Gradle applicationId 设置导致 "No resource found" 在 themes.xml

Android Gradle applicationId setting causing "No resource found" in themes.xml

我转换了开源 Google Authenticator from the existing Eclipse project to a Gradle-based Android Studio project. The project builds fine after the conversion. I made the fully working Android Studio project available on GitHub

但是,我现在将 build.gradle 中的 applicationId 更改为另一个 ID,以便能够将其与我的高效 Google 身份验证器一起安装在我的设备上。根据我对 ApplicationId versus PackageName 的理解,这应该是正确的方法。

build.gradle 现在看起来像这样:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 14
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "invalid.mydomain.project"
        minSdkVersion 7
        targetSdkVersion 14

        testApplicationId "invalid.mydomain.project.tests"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

AndroidManifest.xml 包含旧包名称:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.google.android.apps.authenticator2"
          android:versionCode="21" android:versionName="2.21">
...

现在执行 Gradle 构建时,这会导致错误,无法找到具有包名称的资源:

:app:processDebugResources
E:\Martin\Projects\google-authenticator-android\app\build\intermediates\res\debug\values\values.xml:199: error: Error: No resource found that matches the given name: attr 'com.google.android.apps.authenticator2:color'.

E:\Martin\Projects\google-authenticator-android\app\build\intermediates\res\debug\values-v11\values.xml:16: error: Error: No resource found that matches the given name: attr 'com.google.android.apps.authenticator2:color'.

实际失败的命令是:

D:\java_dev\Android\android-sdk\build-tools.1.2\aapt.exe package -f --no-crunch -I D:\java_dev\Android\android-sdk\platforms\android-14\android.jar -M E:\Martin\Projects\google-authenticator-android\app\build\intermediates\manifests\full\debug\AndroidManifest.xml -S E:\Martin\Projects\google-authenticator-android\app\build\intermediates\res\debug -A E:\Martin\Projects\google-authenticator-android\app\build\intermediates\assets\debug -m -J E:\Martin\Projects\google-authenticator-android\app\build\generated\source\r\debug -F E:\Martin\Projects\google-authenticator-android\app\build\intermediates\res\resources-debug.ap_ --debug-mode --custom-package com.google.android.apps.authenticator2 -0 apk

我现在的问题是,这是预期的行为吗?我现在真的需要 search/replace *.xml 文件中的包名称吗?据我了解,这正是 ApplicationId 试图避免的。

这可能是 Android SDK 构建工具的问题,而不是 Android Studio 的问题吗?

我在 Windows 和 Linux 上提供的代码的干净检查中重现了这个问题,所以它 应该 不是 OS相关。

我现在可以将范围缩小到 themes.xml:

<style name="AccountListWithVerificationCodesRowCountdownIndicator">
  <item name="com.google.android.apps.authenticator2:color">@color/countdown_indicator</item>
</style>

这似乎是硬编码 applicationId,而不是 packageName。删除限定并使用

<style name="AccountListWithVerificationCodesRowCountdownIndicator">
  <item name="color">@color/countdown_indicator</item>
</style>

有效。

我还必须在 preferences.xml 和类似文件中进行更改,其中包名称是硬编码的,因为在这些位置它实际上是应用程序 ID,在解析 Intents 时:

发件人:

<intent
  android:targetPackage="com.google.android.apps.authenticator2"
  android:targetClass="com.google.android.apps.authenticator.timesync.SettingsTimeCorrectionActivity">
</intent>

收件人:

<intent
  android:targetPackage="@string/app_package_name"
  android:targetClass="com.google.android.apps.authenticator.timesync.SettingsTimeCorrectionActivity">
</intent>

我必须在所有口味中添加一个特殊的 app_package_namestrings.xml,其中我记录了口味的 applicationId 属性。 (如果有其他引用 applicationId 的方法,请告诉我!)