Gradle插件获取各种build目录
apk在打包过程中会在build目录下产出很多临时文件,而我们在编写插件的时候也经常会有去修改某个临时文件内容的情况,这个时候如何找到对应的目录地址呢?
首先先上代码
/**
* 获取build临时产生的一些目录
* 先通过project来获取变体(variants)
* 然后通过variant的getArtifactFileCollection方法来获取各种目录
* @param project
*/
void getFileDir(Project project) {
project.afterEvaluate {
DefaultDomainObjectCollection<BaseVariant> variants
if (project.plugins.hasPlugin(AppPlugin.class)) {
variants = project.android.applicationVariants
} else if (project.plugins.hasPlugin(LibraryPlugin.class)) {
variants = project.android.libraryVariants
} else {
return
}
variants.all { variant ->
List<File> deps = new ArrayList<>()
FileCollection fileCollection = variant.variantData.scope
.getArtifactFileCollection(
AndroidArtifacts.ConsumedConfigType.RUNTIME_CLASSPATH,
AndroidArtifacts.ArtifactScope.PROJECT,
AndroidArtifacts.ArtifactType.JAVA_RES
)
fileCollection.each {file ->
deps.add(file)
}
}
}
}
variant.variantData.scope.getArtifactFileCollection方法有三个参数,你可以通过这个方法来获取各式各样的目录地址,
第一个选项的可选项,主要是用来控制要获取什么状态下的目录
COMPILE_CLASSPATH("compileClasspath", AndroidArtifacts.PublishedConfigType.API_ELEMENTS, true),
RUNTIME_CLASSPATH("runtimeClasspath", AndroidArtifacts.PublishedConfigType.RUNTIME_ELEMENTS, true),
ANNOTATION_PROCESSOR("annotationProcessorClasspath", AndroidArtifacts.PublishedConfigType.RUNTIME_ELEMENTS, false),
METADATA_VALUES("metadata", AndroidArtifacts.PublishedConfigType.METADATA_ELEMENTS, false);
第二个选项是控制在什么范围
/** The provenance of artifacts to include. */
public enum ArtifactScope {
/** Include all artifacts */
ALL,
/** Include all 'external' artifacts, i.e. everything but PROJECT, i.e. FILE + MODULE */
EXTERNAL,
/** Include all artifacts built by subprojects */
PROJECT,
/** Include all file dependencies */
FILE,
/** Include all module (i.e. from a repository) dependencies */
REPOSITORY_MODULE,
}
第三个选项是控制什么类型的文件
/** Artifact published by modules for consumption by other modules. */
public enum ArtifactType {
CLASSES(TYPE_CLASSES),
// classes.jar files from libraries that are not namespaced yet, and need to be rewritten to
// be namespace aware.
NON_NAMESPACED_CLASSES(TYPE_NON_NAMESPACED_CLASSES),
SHARED_CLASSES(TYPE_SHARED_CLASSES),
// Jar or processed jar, used for purposes such as computing the annotation processor
// classpath or building the model.
// IMPORTANT: Consumers should generally use PROCESSED_JAR instead of JAR, as the jars may
// need to be processed (e.g., jetified to AndroidX) before they can be used. Consuming JAR
// should be considered as an exception and the reason should be documented.
JAR(TYPE_JAR),
PROCESSED_JAR(TYPE_PROCESSED_JAR),
// published dex folder for bundle
DEX(TYPE_DEX),
// manifest is published to both to compare and detect provided-only library dependencies.
MANIFEST(TYPE_MANIFEST),
// manifests that need to be auto-namespaced.
NON_NAMESPACED_MANIFEST(TYPE_NON_NAMESPACED_MANIFEST),
MANIFEST_METADATA(TYPE_MANIFEST_METADATA),
// Resources static library are API (where only explicit dependencies are included) and
// runtime
RES_STATIC_LIBRARY(TYPE_ANDROID_RES_STATIC_LIBRARY),
RES_SHARED_STATIC_LIBRARY(TYPE_ANDROID_RES_SHARED_STATIC_LIBRARY),
RES_BUNDLE(TYPE_ANDROID_RES_BUNDLE),
// API only elements.
AIDL(TYPE_AIDL),
RENDERSCRIPT(TYPE_RENDERSCRIPT),
DATA_BINDING_ARTIFACT(TYPE_DATA_BINDING_ARTIFACT),
DATA_BINDING_BASE_CLASS_LOG_ARTIFACT(TYPE_DATA_BINDING_BASE_CLASS_LOG_ARTIFACT),
COMPILE_ONLY_NAMESPACED_R_CLASS_JAR(TYPE_ANDROID_NAMESPACED_R_CLASS_JAR),
// runtime and/or bundle elements
JAVA_RES(TYPE_JAVA_RES),
SHARED_JAVA_RES(TYPE_SHARED_JAVA_RES),
ANDROID_RES(TYPE_ANDROID_RES),
ASSETS(TYPE_ASSETS),
SHARED_ASSETS(TYPE_SHARED_ASSETS),
SYMBOL_LIST(TYPE_SYMBOL),
COMPILED_REMOTE_RESOURCES(TYPE_COMPILED_REMOTE_RESOURCES),
/**
* The symbol list with the package name as the first line. As the r.txt format in the AAR
* cannot be changed, this is created by prepending the package name from the
* AndroidManifest.xml to the existing r.txt file.
*/
SYMBOL_LIST_WITH_PACKAGE_NAME(TYPE_SYMBOL_WITH_PACKAGE_NAME),
DEFINED_ONLY_SYMBOL_LIST(TYPE_DEFINED_ONLY_SYMBOL),
JNI(TYPE_JNI),
SHARED_JNI(TYPE_SHARED_JNI),
ANNOTATIONS(TYPE_EXT_ANNOTATIONS),
PUBLIC_RES(TYPE_PUBLIC_RES),
CONSUMER_PROGUARD_RULES(TYPE_CONSUMER_PROGUARD_RULES),
AAPT_PROGUARD_RULES(TYPE_AAPT_PROGUARD_RULES),
LINT(TYPE_LINT_JAR),
APK_MAPPING(TYPE_MAPPING),
APK_METADATA(TYPE_METADATA),
APK(TYPE_APK),
// intermediate bundle that only contains one module. This is to be input into bundle-tool
MODULE_BUNDLE(TYPE_MODULE_BUNDLE),
// final bundle generate by bundle-tool
BUNDLE(TYPE_BUNDLE),
// apks produced from the bundle, for consumption by tests.
APKS_FROM_BUNDLE(TYPE_APKS_FROM_BUNDLE),
// the manifest to be used by bundle-tool
BUNDLE_MANIFEST(TYPE_BUNDLE_MANIFEST),
// intermediate library dependencies on a per module basis for eventual packaging in the
// bundle.
LIB_DEPENDENCIES(TYPE_LIB_DEPENDENCIES),
// Feature split related artifacts.
// file containing the metadata for the full feature set. This contains the feature names,
// the res ID offset, both tied to the feature module path. Published by the base for the
// other features to consume and find their own metadata.
FEATURE_SET_METADATA(TYPE_FEATURE_SET_METADATA),
FEATURE_SIGNING_CONFIG(TYPE_FEATURE_SIGNING_CONFIG),
// file containing the application ID to synchronize all base + dynamic feature. This is
// published by the base feature and installed application module.
FEATURE_APPLICATION_ID_DECLARATION(TYPE_FEATURE_APPLICATION_ID),
// ?
FEATURE_RESOURCE_PKG(TYPE_FEATURE_RESOURCE_PKG),
// File containing the list of transitive dependencies of a given feature. This is consumed
// by other features to avoid repackaging the same thing.
FEATURE_TRANSITIVE_DEPS(TYPE_FEATURE_TRANSITIVE_DEPS),
// The feature dex files output by the DexSplitter from the base. The base produces and
// publishes these files when there's multi-apk code shrinking.
FEATURE_DEX(TYPE_FEATURE_DEX),
// Metadata artifacts
METADATA_FEATURE_DECLARATION(TYPE_METADATA_FEATURE_DECLARATION),
METADATA_FEATURE_MANIFEST(TYPE_METADATA_FEATURE_MANIFEST),
METADATA_BASE_MODULE_DECLARATION(TYPE_METADATA_BASE_DECLARATION),
METADATA_CLASSES(TYPE_METADATA_CLASSES),
METADATA_JAVA_RES(TYPE_METADATA_JAVA_RES),
// types for querying only. Not publishable.
AAR(TYPE_AAR),
EXPLODED_AAR(TYPE_EXPLODED_AAR);
@NonNull private final String type;
ArtifactType(@NonNull String type) {
this.type = type;
}
@NonNull
public String getType() {
return type;
}
}
网友评论