From 150a28a992e4e77a13180303c9b3e13739f4e85a Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Mon, 9 Sep 2019 17:09:47 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BE=93=E5=87=BA=E9=A1=B9=E7=9B=AE=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E6=9D=83=E9=99=90=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/Android/Gradle/依赖权限信息.md | 105 ++++++++++++++++++++- 1 file changed, 104 insertions(+), 1 deletion(-) diff --git a/blogs/Android/Gradle/依赖权限信息.md b/blogs/Android/Gradle/依赖权限信息.md index c9c528d..67187f1 100644 --- a/blogs/Android/Gradle/依赖权限信息.md +++ b/blogs/Android/Gradle/依赖权限信息.md @@ -241,6 +241,109 @@ static String getArtifactName(ResolvedArtifactResult artifact) { 其次,更改一下不同的 ArtifactType,会发现惊喜哦~ -剩下还有一个需要完善的,那就是把整个流程封装成一个 Task 就好了,这就需要涉及到动态添加依赖了,毕竟这种方法必须要依赖 Gradle 源码了。 +然后就是把整个流程封装成一个 Task 就好了,源码如下: + +```groovy +package com.ehi.ehiplugin.task + +import com.ehi.ehiplugin.base.BaseTask +import com.android.build.gradle.internal.dependency.ArtifactCollectionWithExtraArtifact +import com.android.build.gradle.internal.publishing.AndroidArtifacts +import groovy.json.JsonOutput +import org.gradle.api.artifacts.ArtifactCollection +import org.gradle.api.artifacts.component.ComponentIdentifier +import org.gradle.api.artifacts.component.ModuleComponentIdentifier +import org.gradle.api.artifacts.component.ProjectComponentIdentifier +import org.gradle.api.artifacts.result.ResolvedArtifactResult +import org.gradle.internal.component.local.model.OpaqueComponentArtifactIdentifier + +import java.util.regex.Matcher +import java.util.regex.Pattern + +/** + * Author: Omooo + * Date: 2019/9/9 + * Version: v6.3.2 + * Desc: 输出 app module 依赖的 AAR 的权限信息 + * Use: ./gradlew listPermission + * Output: 项目根目录下的 permissions.json 文件 + * ReferTo: @see ProcessApplicationManifest#695 + */ +@SuppressWarnings("UnstableApiUsage") +class ListPermissionTask extends BaseTask { + + @Override + void doAction() { + println("/*********************************************/") + println("/********* -- ListPermissionTask -- **********/") + println("/***** -- projectDir/permissions.json -- *****/") + println("/*********************************************/") + + if (!project.hasProperty("android")) return + boolean isFirst = true + project.android.applicationVariants.all { variant -> + if (!isFirst) return + + // 获取 app module 的权限 + HashMap> map = new HashMap<>() + map.put(project.name, matchUsesPermission(variant.getCheckManifest().manifest.getText())) + + // 获取 app 依赖的 AAR 权限 + ArtifactCollection manifests = variant.getVariantData().getScope().getArtifactCollection( + AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH, + AndroidArtifacts.ArtifactScope.ALL, + AndroidArtifacts.ArtifactType.MANIFEST) + final Set artifacts = manifests.getArtifacts() + + for (ResolvedArtifactResult artifact : artifacts) { + if (!map.containsKey(getArtifactName(artifact)) + && matchUsesPermission(artifact.file.text).size() > 0) { +// println(artifact.getFile().absolutePath) +// println(getArtifactName(artifact)) + map.put(getArtifactName(artifact), matchUsesPermission(artifact.file.text)) + } + } + writePermissionFile(map) + isFirst = false + } + } + + static String getArtifactName(ResolvedArtifactResult artifact) { + ComponentIdentifier id = artifact.getId().getComponentIdentifier() + if (id instanceof ProjectComponentIdentifier) { + return ((ProjectComponentIdentifier) id).getProjectPath() + } else if (id instanceof ModuleComponentIdentifier) { + ModuleComponentIdentifier mID = (ModuleComponentIdentifier) id + return mID.getGroup() + ":" + mID.getModule() + ":" + mID.getVersion() + } else if (id instanceof OpaqueComponentArtifactIdentifier) { + return id.getDisplayName() + } else if (id instanceof ArtifactCollectionWithExtraArtifact.ExtraComponentIdentifier) { + return id.getDisplayName() + } else { + throw new RuntimeException("Unsupported type of ComponentIdentifier") + } + } + + static List matchUsesPermission(String text) { + List list = new ArrayList<>(8) + Pattern pattern = Pattern.compile("") + Matcher matcher = pattern.matcher(text) + while (matcher.find()) { + list.add(matcher.group()) + } + return list + } + + void writePermissionFile(HashMap> map) { + def jsonFile = new File("${project.parent.projectDir}/permissions.json") + if (jsonFile.exists()) { + jsonFile.delete() + } + jsonFile.createNewFile() + def json = JsonOutput.toJson(map) + jsonFile.write(JsonOutput.prettyPrint(json), "utf-8") + } +} +``` (逃~ \ No newline at end of file