Как установить apk файл на 11 Android с помощью java
С толкнулся с такой проблемой, что при установке apk файла на 11 Android вылезает "Не удалось обработать пакет", перепробовал несколько способов с FileProvider, но всё-равно получаю такую ошибку. Пишу на java в Android Studio. Буду благодарен, если вы напишите свой пример установки apk файла;)
build.gradle
`
plugins {
id 'com.android.application'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.nevskiy619.launcher"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileSdkVersion 31
buildToolsVersion '30.0.2'
}
dependencies {
implementation 'net.lingala.zip4j:zip4j:1.3.2'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
` AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.nevskiy619.launcher">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"
tools:ignore="ProtectedPermissions" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sentrium"
android:usesCleartextTraffic="true">
<activity
android:name=".InstallActivity"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="com.nevskiy619.launcher.provider" android:exported="false" android:grantUriPermissions="true" android:name="androidx.core.content.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/>
</provider>
</application>
</manifest>
Сам код
case R.id.buttonClient:
boolean isNonPlayAppAllowed = false;
try {
isNonPlayAppAllowed = Settings.Secure.getInt(getContentResolver(), Settings.Secure.INSTALL_NON_MARKET_APPS) == 1;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
if (!isNonPlayAppAllowed) {
startActivity(new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS));
} else {
installApk();
buttonComplete.setVisibility(View.VISIBLE);
}
break;
public void installApk() {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setDataAndType(FileProvider.getUriForFile(this, "com.nevskiy619.launcher.provider", new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + "1client.apk" )), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
/*File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + "1client.apk");
if (Build.VERSION.SDK_INT >= 24) {
Uri uriForFile = FileProvider.getUriForFile(this, "com.nevskiy619.launcher.provider", file);
Intent intent = new Intent("android.intent.action.INSTALL_PACKAGE");
intent.setData(uriForFile);
intent.setFlags(1);
startActivity(intent);
return;
}
Uri fromFile = Uri.fromFile(file);
Intent intent2 = new Intent("android.intent.action.VIEW");
intent2.setDataAndType(fromFile, "application/vnd.android.package-archive");
intent2.setFlags(268435456);
startActivity(intent2);*/
}
Файл provider_paths
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:aapt="http://schemas.android.com/aapt">
<external-path name="external_download" path="Download"/>
</paths>