I have a app that downloads an apk from server, but I am unable to launch it like other android versions even though I have queries tag in android manifest
Here is the intent code:-
private void callInstallProcess() {
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(pathname);
Uri fileUri = Uri.fromFile(file);
// if (Build.VERSION.SDK_INT >= 24) {
// fileUri = FileProvider.getUriForFile(this, getPackageName(),
// file);
// }
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(fileUri , "application/vnd.android.package-archive");
startActivityForResult(intent, PERMISSIONS_REQUEST);
}
catch (Exception e)
{
e.printStackTrace();
}
}
Here is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.x.x">
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="application/vnd.android.package-archive" />
</intent>
</queries>
<application
android:allowBackup="false"
android:icon="@mipmap/appicon"
android:label="@string/app_name"
android:roundIcon="@mipmap/appicon"
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Gradle file:-
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "30.0.0-rc4"
defaultConfig {
applicationId "x.x.x"
minSdkVersion 22
targetSdkVersion 'R'
versionCode 3
versionName "1.11"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
I have allowed to my app the permission to install unknown apps on android R preview 4 emulator.
Log:-
W/System.err: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/x.x.x/files/ApkDownload/x.x.x.apk typ=application/vnd.android.package-archive flg=0x3 }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2067)
W/System.err: at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5323)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:5281)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at x.x.x.MainActivity.callInstallProcess(MainActivity.java:126)
at x.x.x.MainActivity.askPerm(MainActivity.java:98)
W/System.err: at x.x.x.MainActivity.access$000(MainActivity.java:42)
at x.x.x.MainActivity$1.onClick(MainActivity.java:79)
at androidx.appcompat.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7478)
at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
D/EGL_emulation: eglMakeCurrent: 0xe8ae0220: ver 2 0 (tinfo 0xe8e31210)
I am using android studio 4 and gradleversion is 4.0.0 Is it just a bug or am I doing something wrong as chrome manages to intent apk install.
Is it just a bug or am I doing something wrong
You are doing something wrong.
Uri.fromFile()
has been banned, in effect, since Android 7.0. Normally, your attempt to us it with an Intent
would crash with a FileUriExposedException
. Rather than fixing the problem, you decided to try to bypass the check, through your disableDeathOnFileUriExposure
hack. However, nobody else has to honor your file:///
Uri
, because, again, that has been banned for years. Apparently, Android R's installer no longer supports file:///
.
So, at minimum, you need to switch to using FileProvider
on Android 7.0+. Uri.fromFile()
is fine for Android 6.0 and older, and specifically for what you are trying to do (install an APK), you have no choice but to use Uri.fromFile()
for those older devices.
Also note that using ACTION_VIEW
for installing an APK has been deprecated on Android 10+. Temporarily it still works, but Google would like you to switch to the PackageInstaller
API instead.