Notes on calling Installer on Android App

When I tried to create an application that updates and installs after a long time, there were various changes around the authority, so I wrote a memo

environment

compileSdkVersion 29 minSdkVersion 24 targetSdkVersion 29

Source code

Use the following for the permission when skipping to the installer with Intent

AndroidManifest.xml


<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

You have to go through FileProvider to install Dowloaded apk Many articles still use SupportLibrary.v4, but this time I will use androidx

build.gradle


implementation 'androidx.core:core:{current version}'

FileProvider is added as below

AndroidManifest.xml


<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true" >
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
</provider>

After that, as written in various articles, just skip it with the Intent that was suitable for each Android version

MainActivity.java


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Uri fileUri = FileProvider.getUriForFile(MainActivity.this,
                        MainActivity.this.getApplicationContext().getPackageName() + ".fileprovider",
                        file);
                Intent i = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                i.setData(fileUri);
                i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                MainActivity.this.startActivity(i);
            } else {
                Uri fileUri = Uri.fromFile(file);
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setDataAndType(fileUri, APK_MIME_TYPE);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MainActivity.this.startActivity(i);
            }

I think it may change depending on the environment, but this is enough that's all

Recommended Posts

Notes on calling Installer on Android App
[Android] Notes on xml
ROS app development on Android
Notes on Android (java) thread processing
Calling java from C ++ on Android NDK
Notes on creating android plugins for Unity
I made a calculator app on Android
Allows Youtube autoplay on Cordova's Android app
Android weather app
[Android] Detailed notes
Notes on getting Callback from Android text-to-speech function TextToSpeech
Notes on Protocol Buffers
python notes on docker
Android app Spectrum Analyzer
Notes on multiple inheritance
Notes on regular expressions
[2018 Preservation Version] Reverse Engineering Android App (.apk) on Mac
Notes in Android studio
About Android App Components
Customize list view on Android
Android app personal development kickoff
Use serial communication on Android
Make an android app. (Day 5)
Use native code on Android
Is it an Android app?
[Ruby] Notes on gets method
[Rails] New app creation --Notes--
Released the No Todo app instead of Todo. .. (And notes on ridgepole)