Skip to content

Commit

Permalink
1.解决issue: #170
Browse files Browse the repository at this point in the history
2.target sdk 提升至31
3.兼容Android12
  • Loading branch information
xuexiangjys committed Mar 20, 2024
1 parent 63cb5cd commit bb1958e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
android:theme="@style/AppTheme">
<activity
android:name=".activity.MainActivity"
android:configChanges="locale">
android:configChanges="locale"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".activity.UpdateActivity" />
<activity
android:name=".activity.UpdateActivity"
android:exported="false" />
</application>

</manifest>
44 changes: 44 additions & 0 deletions gradle/exported.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
4 changes: 2 additions & 2 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [:]
Expand Down
9 changes: 7 additions & 2 deletions xupdate-lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
<uses-permission
android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />

<application>
<service android:name="com.xuexiang.xupdate.service.DownloadService" />
<service
android:name="com.xuexiang.xupdate.service.DownloadService"
android:exported="false" />

<provider
android:name=".utils.UpdateFileProvider"
Expand All @@ -26,6 +30,7 @@

<activity
android:name=".widget.UpdateDialogActivity"
android:exported="false"
android:theme="@style/XUpdate_DialogTheme" />

</application>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -48,8 +53,6 @@

import java.io.File;

import static com.xuexiang.xupdate.entity.UpdateError.ERROR.DOWNLOAD_FAILED;

/**
* APK下载服务
*
Expand All @@ -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;

//=====================绑定服务============================//
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit bb1958e

Please sign in to comment.