Skip to content

Commit

Permalink
【Gradle-17】动态删除清单文件中的某个权限
Browse files Browse the repository at this point in the history
  • Loading branch information
yechao committed May 12, 2024
1 parent 679a867 commit 14fc891
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 12 deletions.
14 changes: 13 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'com.yechaoa.plugin.gradleX'
// id 'com.yechaoa.plugin.gradleX'
}

/**
Expand All @@ -15,6 +15,7 @@ gradleX {
analysisSo = false
checkSnapshot = false
blockSnapshot = false
// permissionsToRemove = ['android.permission.RECORD_AUDIO','android.permission.WRITE_EXTERNAL_STORAGE']
}

/**
Expand Down Expand Up @@ -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)
// }
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<!-- 编译时动态删除权限 -->
<!-- <uses-permission-->
<!-- android:name="android.permission.RECORD_AUDIO"-->
<!-- tools:node="remove"/>-->

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand Down
40 changes: 32 additions & 8 deletions app/src/main/java/com/yechaoa/gradlex/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.yechaoa.gradlex

import android.content.pm.PackageManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import com.yechaoa.yutilskt.LogUtil
import com.yechaoa.yutilskt.ToastUtil
import com.yechaoa.yutilskt.YUtils
import com.yechaoa.yutilskt.show

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -19,16 +22,37 @@ class MainActivity : AppCompatActivity() {
findViewById<TextView>(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<out String>, 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()
}
}

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}

Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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 类
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ dependencies {
}

group 'com.yechaoa.plugin'
version '1.2'
version '1.6-SNAPSHOT'

publishing {
// 配置Plugin GAV
Expand Down
44 changes: 44 additions & 0 deletions plugin/src/main/java/com/yechaoa/plugin/base/GradleXPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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 是否打断编译
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -31,4 +34,9 @@ public class CommonPluginExtension {
* snapshot版本检查 如有,打断编译,默认关闭
*/
public boolean blockSnapshot = false;

/**
* 需要移除的权限
*/
public List<String> permissionsToRemove = new ArrayList<>();
}

0 comments on commit 14fc891

Please sign in to comment.