|
|
|
@ -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<String, List<String>> 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<ResolvedArtifactResult> 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<String> matchUsesPermission(String text) { |
|
|
|
|
List<String> list = new ArrayList<>(8) |
|
|
|
|
Pattern pattern = Pattern.compile("<uses-permission.+./>") |
|
|
|
|
Matcher matcher = pattern.matcher(text) |
|
|
|
|
while (matcher.find()) { |
|
|
|
|
list.add(matcher.group()) |
|
|
|
|
} |
|
|
|
|
return list |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void writePermissionFile(HashMap<String, List<String>> 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") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
(逃~ |