折腾了3天,总算解决了问题。
依赖如下
implementation 'androidx.core:core:1.0.2'
// implementation 'com.android.support:support-compat:28.0.0'
support包,试了几个版本总是提示一堆
Duplicate class android.support.v4. ……
切换到依赖 androidx,则不能忘记在 androidManifest.xml中替换 name值
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
>
android:name="androidx.core.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true" >
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepath" />
工具类如下
public static voidinstallApk(Activity activity, File apkFile){
Intent intent=newIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Uri uri = Uri.fromFile(apkFile);
Uri uri = null;//todo N FileProvider
//todo O install permission
if(Build.VERSION.SDK_INT >=Build.VERSION_CODES.N){
uri= androidx.core.content.FileProvider.getUriForFile(activity,activity.getPackageName()+".fileprovider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}else{
uri=Uri.fromFile(apkFile);
}
intent.setDataAndType(uri,"application/vnd.android.package-archive");
activity.startActivity(intent);
}
另外为了单纯一点,避免错误提示
Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.app.INotificationSideChannel$Stub$Proxy foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.IResultReceiver foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.IResultReceiver$Stub$Proxy foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.ResultReceiver foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.ResultReceiver$1 found in modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.ResultReceiver$MyResultReceiver foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Duplicate class android.support.v4.os.ResultReceiver$MyRunnable foundin modules classes.jar (androidx.core:core:1.0.2) and classes.jar (com.android.support:support-compat:25.3.1)
Go to the documentation to learn how to Fix dependency resolution errors.
解决办法为,在gradle.properties中添加如下代码
android.useAndroidX=trueandroid.enableJetifier=true
最后贴一下 androidManifest.xml中提到的2个xml文件,在res/xml文件夹下
network_security_config.xml
filepath.xml