如何防止 Android 应用自动备份?

How to prevent auto-backup of an Android app?

我在某些设备上遇到了非常奇怪的情况(Nexus 5x with Android 7):当我清理它的数据并卸载它,然后用 Studio 安装它时,应用程序没有初始化但它使用来自1 月 24 日!我在平板电脑上尝试了同样的程序,但应用程序没有数据。

我已经多次重复这个过程,我清理了我的项目,多次重建它,它总是从 1 月 24 日的数据(数据库和共享首选项)开始。

我什至尝试了 adb shell 和 运行 来清理数据:

bullhead:/data/data/lelisoft.com.lelimath.debug $ ls -l databases/
total 232
-rw-rw---- 1 u0_a259 u0_a259 98304 2017-02-05 11:03 lelimath.sqlite
-rw------- 1 u0_a259 u0_a259 16928 2017-02-05 11:03 lelimath.sqlite-journal

我删除了它们,应用程序似乎是空的 - 直到我删除它并重新安装 - 1 月 24 日回来了。

这是如何启动的日志:

$ adb shell am start -n "lelisoft.com.lelimath.debug/lelisoft.com.lelimath.activities.DashboardActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Waiting for application to come online: lelisoft.com.lelimath.debug | lelisoft.com.lelimath.debug.test
I/InstantRun: Instant Run Runtime started. Android package is lelisoft.com.lelimath.debug, real application class is lelisoft.com.lelimath.helpers.LeliMathApp.
D/l.c.l.h.LeliMathApp: onCreate()
D/l.c.l.h.BalanceHelper: Current points balance: 234

这是从调试器获得的数据库位置:

/data/user/0/lelisoft.com.lelimath.debug/databases/lelimath.sqlite

Gradle:

android {
signingConfigs {
}
compileSdkVersion 24
buildToolsVersion "23.0.3"
defaultConfig {
    applicationId "lelisoft.com.lelimath"
    resValue 'string', 'app_name', 'LeliMath'
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 300
    versionName '3.0.0'
    resValue "string", "app_build_number", getDate();
    resValue "string", "app_version", versionName;
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    debug {
        applicationIdSuffix ".debug"
        resValue 'string', 'app_name', 'LeliMath DEV'
    }
}

清单部分:

<application
    android:name=".helpers.LeliMathApp"
    android:allowBackup="true"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    tools:ignore="GoogleAppIndexingWarning">

我不想通过恢复出厂设置 phone 来删除这些数据。我认为这些数据不在我的构建中。我没有添加它们,安装时平板电脑中的应用程序是空的。

自 Android 6.0 (v 23) 起,Android 引入了一项名为 Auto-backup for apps 的新功能。它的作用是将应用程序的某些文件备份到用户的 Google 驱动器。它更新的文件列表包括:

  • 共享首选项文件
  • getFilesDir()
  • 返回的目录中的文件
  • getDatabasePath(String)
  • 返回的目录中的文件
  • 使用 getDir(String, int)
  • 创建的目录中的文件
  • getExternalFilesDir(String)
  • 返回的目录中的外部存储文件

现在 manifest.xml 中的这一行负责:

android:allowBackup="true"

如果您希望禁用备份,您是否应该选择将值设置为 false

此外,数据备份以 24 小时为间隔,Android 为此目的使用 JobScheduler API,因此这意味着用户无法控制该过程正在传输的数据。

此外,auto-backups 的 space 限制为 25MB,并且不计入用户的 space 配额。

另外,你可以设置为<include><exclude>某些类型的上传数据,比如你可能不需要保存用户机密数据,这样也很灵活, 有关更多信息,请访问:Android Auto Backup for Apps (100 Days of Google Dev)

我遇到了同样的问题,真的不得不挠头,因为 Google 没有突出显示应用程序备份 feature.Starting 从 API 级别 23 的细微变化,所有应用程序数据都将自动备份到 Google 驱动器中。当应用程序重新安装时,这些数据将被恢复。因此,即使您卸载应用程序,数据也不会消失。

大多数情况下,您的应用程序清单都会有这样的条目 -

android:allowBackup="true"

将值更改为 false 或按照以下 link 并配置一个 xml 让备份服务知道要备份什么和不备份什么。

https://developer.android.com/guide/topics/data/autobackup.html

<application ...
    android:allowBackup="false"
    android:fullBackupContent="false"
    tools:replace="android:allowBackup"
</application>

只需更改

android:allowBackup="true"

android:allowBackup="false"

如果您使用任何依赖项,要覆盖此 属性 使用

tools:replace="android:allowBackup"
android:allowBackup="false"

我知道这不是一个完美的解决方案,但它对我有用。

卸载特定应用程序后,我关闭设备并打开设备,设备 OS 未从云端调用备份检查