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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42051255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:20:07  来源:igfitidea点击:

How to prevent auto-backup of an Android app?

javaandroidandroid-manifestandroid-jobscheduler

提问by Leos Literak

I face very weird situation on certain device (Nexus 5x with Android 7): when I clean its data and uninstall it, then install it with Studio, the app is not unitialized but it uses data from 24th january! I tried the same procedure with a tablet and the app has no data.

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

I have repeated this procedure many times, I cleaned my project, rebuilt it multiple times and it always starts with 24th january data (both database and shared prefs).

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

I even tried adb shell and run as to clean up data:

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

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

I deleted them and the app seemed empty - until I deleted it and installed again - 24th january was back.

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

This is a log how it starts:

这是它如何启动的日志:

$ 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

This is a location of a database, got from a debugger:

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

/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'
    }
}

Manifest portion:

清单部分:

<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">

I do not want o factory reset my phone to get rid off this data. I do not think that this data is in my build. I have not added them and the app in the tablet was empty when installed.

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

回答by OBX

Since Android 6.0 (v 23) onward, Android introduced a new feature called Auto-backup for apps. What this does is, it performs a backup of certain files of an application to a user's Google drive. The list of files it updates include:

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

  • Shared preferences files
  • Files in the directory returned by getFilesDir()
  • Files in the directory returned by getDatabasePath(String)
  • Files in directories created with getDir(String, int)
  • Files on external storage in the directory returned by getExternalFilesDir(String)
  • 共享首选项文件
  • 返回的目录中的文件 getFilesDir()
  • 返回的目录中的文件 getDatabasePath(String)
  • 创建的目录中的文件 getDir(String, int)
  • 返回的目录中外部存储上的文件 getExternalFilesDir(String)

Now this line in the manifest.xmlis responsible for it :

现在这一行manifest.xml负责它:

android:allowBackup="true"

If you prefer to disable the backup, should you choose to set the value to false.

如果您更喜欢禁用备份,是否应该选择将该值设置为false.

Furthermore, the data backed up is in a 24 hour interval and Android uses the JobSchedulerAPI for this purpose, so which means a user has no control over the process of data being transferred.

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

Also, the space for auto-backups is limited to 25MB , and which is not counted against the user's space quota.

此外,自动备份的空间限制为25MB ,并且不计入用户的空间配额。

Also, you can set to <include>and <exclude>certain type of data being uploaded, for instance you may not need to save a user confidential data, so it is flexible for that as well, more info on that is available at : Android Auto Backup for Apps (100 Days of Google Dev)

此外,您还可以设置<include><exclude>某些类型的数据被上传,比如你可能不需要保存用户的机密数据,因此它是灵活的,他们作为好,关于它的更多信息,请访问:Android的自动备份的应用程序(谷歌开发 100 天)

回答by Vikash Bijarniya

Simply change

简单地改变

android:allowBackup="true"

to

android:allowBackup="false"

And if you are using any dependency, to override this property use

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

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

回答by Dibzmania

I had this same problem and really had to scratch my head because Google has not highlighted a subtle change in App backup feature.Starting from API level 23, all app data will be automatically backed up in Google drive. This data would be restored back when the app is installed back. So the data does not go away even when you uninstall the app.

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

Mostly your app manifest would have an entry like this -

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

android:allowBackup="true"

Change the value to falseor else follow the below link and configure an xml letting the backup service know what to backup and what not to.

将值更改为false或遵循以下链接并配置一个 xml,让备份服务知道要备份什么,不要备份什么。

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

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

回答by Ashwin Khadgi

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

回答by Mohd Qasim

i know this is not a perfect solution but its worked for me.

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

after uninstalling the specific app i switch off the device and switch on the device, Device OS not calling backup check from cloud

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