Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by syamilsynz · Apr 20, 2015 at 03:21 PM · androidgamegoogle playupdates

Android - User only update certain file, not the whole .apk

I'm doing a quiz game for android.

my problem is, first of all, I set 100 questions and answers and publish in to google store.

then, after user install my game, they answer all the questions.

Several days, I update my game and added more questions and answers.

How do I want to make users only update the questions and answers only without update the whole .apk file?

currently question/answer are in .txt file.

Should I put questions/answers in a different method?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Bunny83 · Apr 20, 2015 at 03:37 PM

Where do you have that txt file located? Is it just in your project as TextAsset? In that case you can't just update it since all assets are packed into the apk itself as asset.

The Google play store doesn't allow to update "parts" of an apk. It can only update the apk, that's it. However you can, if you have a webservice of some sort, place your txt file on your server and have your app loading it manually over the internet with the WWW class.

You might want to use a simple encryption so it's not too easy for a user to get the txt manually and read out all answers ^^ A simple xor with a magic number would do to make it "hard to read".

You could use Dropbox or something similar to host your file. There you can update it whenever you want. If it's quite a bit of data you might want to use a "two files" approach. One that just contains a version number (which you increase at each update) and a second file which contains the actual data.

The app just has to query the "version" file and see if the locally stored file is outdated in which case you will load the data file. Once you have to data file you can store it on the device itself. Keep in mind to store the version number as well so you can compare it with the one on the server.

edit

Here's an example how that two-files thing could look like:

 public string VersionFileURL = "https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Data/Version.txt";
 public string DataFileURL = "https://dl.dropboxusercontent.com/u/7761356/UnityAnswers/Data/Data.txt";
 public string LocalVersionFileName = "Version.txt";
 public string LocalDataFileName = "Data.txt";

 public TextAsset defaultText;

 [HideInInspector]
 public string data = ""; // will contain your data
 [HideInInspector]
 public bool dataIsLoaded = false; // will be true when "data" contains useable data.


 IEnumerator GetCachedData()
 {
     dataIsLoaded = false;
     string versionFileName = System.IO.Path.Combine(Application.persistentDataPath, LocalVersionFileName);
     int oldVersion = -1;
     if (System.IO.File.Exists(versionFileName))
     {
         string versionString = System.IO.File.ReadAllText(versionFileName);
         if (!int.TryParse(versionString, out oldVersion))
             Debug.LogError("local 'version' file contains wrong data");
     }
     WWW www = new WWW(VersionFileURL);
     yield return www;
     if (!string.IsNullOrEmpty(www.error))
     {
         Debug.LogError("Version file could not be loaded. Wrong URL? No internet connection? " + www.error);
     }
     else if (string.IsNullOrEmpty(www.text))
     {
         Debug.LogError("Version file could not be loaded. Empty response");
     }
     else
     {
         string dataFileName = System.IO.Path.Combine(Application.persistentDataPath, LocalDataFileName);
         string versionString = www.text;
         int version = int.MaxValue;
         if (!int.TryParse(versionString, out version))
             Debug.LogError("'version' file contains wrong data");
         if (version > oldVersion)
         {
             www = new WWW(DataFileURL);
             yield return www;
             if (!string.IsNullOrEmpty(www.error))
             {
                 Debug.LogError("Data file could not be loaded. Wrong URL? No internet connection? " + www.error);
             }
             else if (string.IsNullOrEmpty(www.text))
             {
                 Debug.LogError("Data file could not be loaded. Empty response");
             }
             else
             {
                 data = www.text;
                 System.IO.File.WriteAllText(dataFileName, data);
                 System.IO.File.WriteAllText(versionFileName, version.ToString());
                 dataIsLoaded = true;
                 yield break;
             }
         }
         // This will be executed when we can't load the new file or when it's version is still up to date
         if (System.IO.File.Exists(dataFileName))
         {
             // load the data from the local stored file
             data = System.IO.File.ReadAllText(dataFileName);
         }
         else
         {
             // fallback if everything else failed use the text from the TextAsset.
             data = defaultText.text;
         }
         dataIsLoaded = true;
     }
 }

You just need to run that coroutine at start and wait for it to complete, or check "dataIsLoaded". The "data" variable will contain the data from your textfile.

 IEnumerator Start()
 {
     yield return StartCoroutine(GetCachedData());
     // use the "data" variable here.
 }

Keep in mind that depending on your internet reachability and speed GetCachedData() might take a few seconds to complete. You can't use "data" before GetCachedData finishes. Usually some kind of busy / loading animation should be displayed during that time.

Important: Everytime you update your data txt file you have to increase the version number in the version txt file. Make sure you first update the data and afterwards you increase the version number. You should never reset or decrease the version number or users which already have loaded a higher version won't get any updates until you reach and pass that version again.

You could increment the version number by as much as you want each update, but keep in mind if you set it to 1000 after the first update you can't go back. Usually you would simply increase it by 1 each time.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syamilsynz · Apr 20, 2015 at 03:55 PM 0
Share

@Bunny83 thanks for the answer!

ya, currently the txt file is located as TextAsset. So it will be part of apk after publish it. I may have to use different method as suggested by you.

by the way, where should the txt file being save in the phone if we use the method as you suggesting? how to read that file that contain new updates? sorry I'm kind of blur when it related to server site.

avatar image Bunny83 · Apr 21, 2015 at 03:20 AM 0
Share

Well, to store files on the device you might want to use Application.persistentDataPath. To write / read files just use the System.IO namespace classes (`System.IO.File` for example).

I'll add an example how you could implement that two-files-approach.

avatar image syamilsynz · May 07, 2015 at 03:29 PM 0
Share

@Bunny83 I'm sorry, I try to understand your code and test it. But it enter this block and give error message

if (!int.TryParse(versionString, out version)) Debug.LogError("'version' file contains wrong data");

sorry to trouble you.

avatar image Bunny83 · May 08, 2015 at 01:17 AM 0
Share

@syamilsynz: Well, have you setup that version file on your server? Or do you use my files? ^^ The "version" file must contain a simple integer value as text. $$anonymous$$ake sure there is nothing else in the file. No spaces no new line characters, just a number.

If you debug in $$anonymous$$onoDevelop, what does "versionString" contains? does the length of the string match it's content and does it contain a number?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Checking for updates on android 0 Answers

Vungle Ads Not Showing For One Specific Package Name 0 Answers

How to save and load string using google services ? 0 Answers

What is freezing my android game? (using Google Play Services) 0 Answers

Does Android delete PlayerPrefs when Updating the App? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges