Update android debug app causes "app is not installed" (no google play)

60 Views Asked by At

I am trying to make possible for users to update ann app from device. We have a server where from user can download apk file with new version of an app. Here is the code which starts after button pressed:

Handler handler = new Handler(Looper.getMainLooper());
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        File folder = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString());
                        File file = new File(folder.getAbsolutePath(), "localdb.apk");
                        final Uri uri = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) ?
                                FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) :
                                Uri.fromFile(file);

                        if (file.exists())
                            file.delete();

                        //url to get app from server
                        String url = Names.UPDATE_URL;

                        InputStream input = null;
                        OutputStream output = null;
                        HttpURLConnection connection = null;
                        try {
                            URL sUrl = new URL(url);
                            connection = (HttpURLConnection) sUrl.openConnection();
                            connection.connect();

                            // download the file
                            input = connection.getInputStream();
                            output = new FileOutputStream(file);

                            byte data[] = new byte[4096];
                            int count;
                            while ((count = input.read(data)) != -1) {
                                // allow canceling with back button

                                output.write(data, 0, count);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (output != null)
                                    output.close();
                                if (input != null)
                                    input.close();
                            } catch (IOException ignored) {
                            }

                            if (connection != null)
                                connection.disconnect();
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Intent install = new Intent(Intent.ACTION_INSTALL_PACKAGE)
                                        .setData(uri)
                                        .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                                startActivity(install);
                            }
                        });

                    }
                });
                thread.start();

Here is what i have in manifest:

...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.QUICKBOOT_POWERON" />
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
...
 <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="ru.mob.myapp.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
...

and finally an xml file provider_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path name="external_files" path="."/>
</paths>

So, everything works fine, I download apk with new version, it is named same as already installed, system asks me to install (update) my application, but when everything is almoust done I get "App is not installed" message without any details. Does anyone have a solution, hint, adviсe?

0

There are 0 best solutions below