From 14fc89180946394516fd884a9906ce858ed3f51f Mon Sep 17 00:00:00 2001 From: yechao Date: Mon, 13 May 2024 01:21:43 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90Gradle-17=E3=80=91=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=B8=85=E5=8D=95=E6=96=87=E4=BB=B6=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E6=9F=90=E4=B8=AA=E6=9D=83=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 14 +++++- app/src/main/AndroidManifest.xml | 5 +++ .../java/com/yechaoa/gradlex/MainActivity.kt | 40 +++++++++++++---- build.gradle | 4 +- gradle.properties | 2 + plugin/build.gradle | 2 +- .../yechaoa/plugin/base/GradleXPlugin.java | 44 +++++++++++++++++++ .../extension/CommonPluginExtension.java | 8 ++++ 8 files changed, 107 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index aaec615..9e0b93a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,7 +1,7 @@ plugins { id 'com.android.application' id 'org.jetbrains.kotlin.android' - id 'com.yechaoa.plugin.gradleX' +// id 'com.yechaoa.plugin.gradleX' } /** @@ -15,6 +15,7 @@ gradleX { analysisSo = false checkSnapshot = false blockSnapshot = false +// permissionsToRemove = ['android.permission.RECORD_AUDIO','android.permission.WRITE_EXTERNAL_STORAGE'] } /** @@ -72,6 +73,17 @@ android { output.versionNameOverride = "3.0" output.versionCodeOverride = 3 } + // 动态删除清单文件里的权限 +// output.processManifest.doLast { +// // 获取manifest文件 +// def manifestOutFile = output.processResourcesProvider.get().getManifestFile() +// // 读取manifest文件 +// def manifestContent = manifestOutFile.getText() +// // 删除指定权限 +// manifestContent = manifestContent.replaceAll('android.permission.RECORD_AUDIO', 'android.permission.INTERNET') +// // 再写回manifest文件 +// manifestOutFile.write(manifestContent) +// } } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f7bd96a..549dcee 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,11 @@ + + + + + (R.id.tv_text).setOnClickListener { LogUtil.e("YUtils.init") - AlertDialog.Builder(this@MainActivity).apply { - setTitle("有新版本") - setMessage("请问要现在升级吗?") - setPositiveButton("确定") { _, _ -> + showDialog() + + requestPermission() + } + } + + private fun requestPermission() { + requestPermissions(arrayOf(android.Manifest.permission.RECORD_AUDIO), 1000) + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if (requestCode == 1000) { + if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { + ToastUtil.show("权限申请成功") + } else { + ToastUtil.show("权限申请失败") + } + } + } + + private fun showDialog() { + AlertDialog.Builder(this@MainActivity).apply { + setTitle("有新版本") + setMessage("请问要现在升级吗?") + setPositiveButton("确定") { _, _ -> - } - setNegativeButton("取消", null) - create() - show() } + setNegativeButton("取消", null) + create() + show() } } diff --git a/build.gradle b/build.gradle index c0a2cbe..b7e6005 100644 --- a/build.gradle +++ b/build.gradle @@ -12,9 +12,9 @@ buildscript { * 2.再解开app > build.gradle里面的插件的注释 //id 'com.yechaoa.plugin.gradleX' * 3.还有app > build.gradle里面的gradleX{} */ -// classpath('com.yechaoa.plugin:gradleX:1.2') +// classpath('com.yechaoa.plugin:gradleX:1.6-SNAPSHOT') // 远端仓库地址 - classpath('com.github.yechaoa.GradleX:plugin:1.5') +// classpath('com.github.yechaoa.GradleX:plugin:1.5') } } diff --git a/gradle.properties b/gradle.properties index 0c0c2fe..0946df6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,6 +21,8 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library +#android.debug.obsoleteApi=true + # 停用 Jetifier android.enableJetifier=false # 配置非传递性 R 类 diff --git a/plugin/build.gradle b/plugin/build.gradle index beafaf6..07a58b4 100644 --- a/plugin/build.gradle +++ b/plugin/build.gradle @@ -18,7 +18,7 @@ dependencies { } group 'com.yechaoa.plugin' -version '1.2' +version '1.6-SNAPSHOT' publishing { // 配置Plugin GAV diff --git a/plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java b/plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java index 60ced45..bc0166f 100644 --- a/plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java +++ b/plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java @@ -10,7 +10,9 @@ import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.ModuleVersionIdentifier; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -60,12 +62,54 @@ public void apply(Project project) { System.out.println(TAG + "已关闭snapshot版本检查"); } + if (!extension.permissionsToRemove.isEmpty()) { + System.out.println(TAG + "已开启权限移除"); + permissionsToRemove(project, extension.permissionsToRemove); + } + + }); + } + + /** + * 移除权限 + * + * @param project target project + * @param permissionsToRemove 需要移除的权限 + */ + private void permissionsToRemove(Project project, List permissionsToRemove) { + if (permissionsToRemove.isEmpty()) { + return; + } + AppExtension androidExtension = project.getExtensions().getByType(AppExtension.class); + androidExtension.getApplicationVariants().all(applicationVariant -> { + applicationVariant.getOutputs().all(output -> { + output.getProcessManifest().doLast(task -> { + try { + // 获取manifest文件 + File manifestOutFile = output.getProcessResourcesProvider().get().getManifestFile(); + // 读取manifest文件 + String manifestContent = Files.readString(manifestOutFile.toPath()); + // 删除指定权限 + for (String permission : permissionsToRemove) { + System.out.println(TAG + "permission = " + permission); + manifestContent = manifestContent.replaceAll(permission, "android.permission.INTERNET"); + } + // 再写回manifest文件 + Files.writeString(manifestOutFile.toPath(), manifestContent); + } catch (IOException e) { + System.out.println(TAG + "permissionsToRemove 文件操作异常,请检查配置"); + throw new RuntimeException(e); + } + }); + } + ); }); } /** * snapshot版本检查 * to do :白名单 + * * @param project target project * @param blockSnapshot 是否打断编译 */ diff --git a/plugin/src/main/java/com/yechaoa/plugin/extension/CommonPluginExtension.java b/plugin/src/main/java/com/yechaoa/plugin/extension/CommonPluginExtension.java index 0c0ceee..a5838ac 100644 --- a/plugin/src/main/java/com/yechaoa/plugin/extension/CommonPluginExtension.java +++ b/plugin/src/main/java/com/yechaoa/plugin/extension/CommonPluginExtension.java @@ -1,6 +1,9 @@ package com.yechaoa.plugin.extension; +import java.util.ArrayList; +import java.util.List; + /** * GitHub : https://github.com/yechaoa * CSDN : http://blog.csdn.net/yechaoa @@ -31,4 +34,9 @@ public class CommonPluginExtension { * snapshot版本检查 如有,打断编译,默认关闭 */ public boolean blockSnapshot = false; + + /** + * 需要移除的权限 + */ + public List permissionsToRemove = new ArrayList<>(); }