Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assets not replaced by version upgrade?

I have written an updated version of an app that works with text files stored in directories within the assets resource folder. The app works fine if it is installed on a device with no previous version. However, if I install the updated version on a device that has a previous version installed, the resource files don't appear to have been updated. This happens both when installing using Android Studio, and when updating the app using the google play store. I have tried explicitly gaining access to the assets on start up using a call to:

    final Context context = getApplicationContext();
    context.getAssets();

but it did not help.

Thanks for any advice.

like image 914
Eric S Avatar asked Nov 19 '25 18:11

Eric S


1 Answers

AFAIK Assets folder of android application will be initialized once at the time of installation only. You cannot update the assets folder after the application has been packaged and installed. After installation if you do any changes in the resources it will not reflect to your older installed version.As Asset folder is read only , you cannot write or update any file present there.

If you want to update it then you need to uninstall the older version and install new version : Source. Or you can just put your text in resource raw folder. Something like this (the name of txt file is 'help') :

try {
        Resources res = getResources();
        InputStream in_s = res.openRawResource(R.raw.help);

        byte[] b = new byte[in_s.available()];
        in_s.read(b);
        txtHelp.setText(new String(b));
    } catch (Exception e) {
        // e.printStackTrace();
        txtHelp.setText("Error: can't show help.");
    }

And yes, basically you just delete the existing assets folder using :

public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib")) {
                    deleteDir(new File(appDir, s));
                    Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                }
            }
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        return dir.delete();
    }
like image 150
Randyka Yudhistira Avatar answered Nov 22 '25 09:11

Randyka Yudhistira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!