diff --git a/app/build.gradle b/app/build.gradle index e82ab4e..ae478a2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,7 +84,7 @@ dependencies { implementation deps.butterknife.runtime annotationProcessor deps.butterknife.compiler //如果开启了内存泄漏监测leak,就需要加上这个依赖 - debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6' + debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1' //网络请求的实现一 implementation 'com.zhy:okhttputils:2.6.2' @@ -96,3 +96,5 @@ dependencies { implementation 'io.reactivex.rxjava2:rxjava:2.2.20' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' } + +apply from: "$rootDir/gradle/exported.gradle" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 313f9ea..26b4e6b 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,8 @@ android:theme="@style/AppTheme"> + android:configChanges="locale" + android:exported="true"> @@ -22,7 +23,9 @@ - + \ No newline at end of file diff --git a/gradle/exported.gradle b/gradle/exported.gradle new file mode 100644 index 0000000..516ac59 --- /dev/null +++ b/gradle/exported.gradle @@ -0,0 +1,44 @@ +import groovy.xml.Namespace +import groovy.xml.XmlUtil + +/** + * 修改 Android 12 因为 exported 的构建问题 + */ +android.applicationVariants.all { variant -> + variant.outputs.all { output -> + output.processResources.doFirst { pm -> + String manifestPath = output.processResources.manifestFile + def manifestFile = new File(manifestPath) + def xml = new XmlParser(false, true).parse(manifestFile) + def exportedTag = "android:exported" + def nameTag = "android:name" + ///指定 space + def androidSpace = new Namespace('http://schemas.android.com/apk/res/android', 'android') + + def nodes = xml.application[0].'*'.findAll { + //挑选要修改的节点,没有指定的 exported 的才需要增加 + (it.name() == 'activity' || it.name() == 'receiver' || it.name() == 'service') && it.attribute(androidSpace.exported) == null + } + ///添加 exported,默认 false + nodes.each { + def isMain = false + it.each { + if (it.name() == "intent-filter") { + it.each { + if (it.name() == "action") { + if (it.attributes().get(androidSpace.name) == "android.intent.action.MAIN") { + isMain = true + println("......................MAIN FOUND......................") + } + } + } + } + } + it.attributes().put(exportedTag, "${isMain}") + } + PrintWriter pw = new PrintWriter(manifestFile) + pw.write(XmlUtil.serialize(xml)) + pw.close() + } + } +} \ No newline at end of file diff --git a/versions.gradle b/versions.gradle index 0f98556..3bf1f39 100644 --- a/versions.gradle +++ b/versions.gradle @@ -104,8 +104,8 @@ ext.deps = deps def build_versions = [:] build_versions.min_sdk = 19 -build_versions.target_sdk = 29 -build_versions.build_tools = "29.0.3" +build_versions.target_sdk = 31 +build_versions.build_tools = "31.0.0" ext.build_versions = build_versions def app_release = [:] diff --git a/xupdate-lib/src/main/AndroidManifest.xml b/xupdate-lib/src/main/AndroidManifest.xml index 16acf92..1e37541 100644 --- a/xupdate-lib/src/main/AndroidManifest.xml +++ b/xupdate-lib/src/main/AndroidManifest.xml @@ -7,12 +7,16 @@ - + - + diff --git a/xupdate-lib/src/main/java/com/xuexiang/xupdate/service/DownloadService.java b/xupdate-lib/src/main/java/com/xuexiang/xupdate/service/DownloadService.java index e5cc3a5..8c49663 100644 --- a/xupdate-lib/src/main/java/com/xuexiang/xupdate/service/DownloadService.java +++ b/xupdate-lib/src/main/java/com/xuexiang/xupdate/service/DownloadService.java @@ -16,6 +16,10 @@ package com.xuexiang.xupdate.service; +import static android.app.PendingIntent.FLAG_IMMUTABLE; +import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; +import static com.xuexiang.xupdate.entity.UpdateError.ERROR.DOWNLOAD_FAILED; + import android.app.Notification; import android.app.NotificationChannel; import android.app.NotificationManager; @@ -34,6 +38,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; import com.xuexiang.xupdate.R; import com.xuexiang.xupdate.XUpdate; @@ -48,8 +53,6 @@ import java.io.File; -import static com.xuexiang.xupdate.entity.UpdateError.ERROR.DOWNLOAD_FAILED; - /** * APK下载服务 * @@ -65,7 +68,7 @@ public class DownloadService extends Service { private static final String CHANNEL_ID = "xupdate_channel_id"; private static final CharSequence CHANNEL_NAME = "xupdate_channel_name"; - private NotificationManager mNotificationManager; + private NotificationManagerCompat mNotificationManager; private NotificationCompat.Builder mBuilder; //=====================绑定服务============================// @@ -120,7 +123,7 @@ public static boolean isRunning() { @Override public void onCreate() { super.onCreate(); - mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE); + mNotificationManager = NotificationManagerCompat.from(this); } @Nullable @@ -491,7 +494,7 @@ private void showDownloadCompleteNotification(File file) { //App后台运行 //更新参数,注意flags要使用FLAG_UPDATE_CURRENT Intent installAppIntent = ApkInstallUtils.getInstallAppIntent(file); - PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, installAppIntent, PendingIntent.FLAG_UPDATE_CURRENT); + PendingIntent contentIntent = PendingIntent.getActivity(DownloadService.this, 0, installAppIntent, FLAG_UPDATE_CURRENT | FLAG_IMMUTABLE); if (mBuilder == null) { mBuilder = getNotificationBuilder(); }