In the last article, we learned how to create a simple plug-in, and this section will continue to talk about how to configure your own Transform.

1.

The Transform converter is provided by Google to allow developers to perform additional interventions during the period after compilation and before packaging, providing opportunities to manipulate bytecode.

2 create Transform

Create a custom FreeCoderTransform class and then inherit to Transform.

public class FreeCoderTransform extends Transform {
    / / 1
    @Override
    public String getName(a) { 
        return "freecoder";
    }

    / / 2
    @Override
    public Set<QualifiedContent.ContentType> getInputTypes() {
        return TransformManager.CONTENT_CLASS;
    }

    / / 3
    @Override
    public Set<? super QualifiedContent.Scope> getScopes() {
        return TransformManager.SCOPE_FULL_PROJECT;
    }

    / / 4
    @Override
    public boolean isIncremental(a) {
        return false;
    }

    / / 5
    @Override
    public void transform(TransformInvocation transformInvocation) throws TransformException, InterruptedException, IOException {
        Collection<TransformInput> inputs = transformInvocation.getInputs();
        TransformOutputProvider outputProvider = transformInvocation.getOutputProvider();

        inputs.forEach(transformInput -> {
            transformInput.getJarInputs().forEach(jarInput -> {
                File dest = outputProvider.getContentLocation(jarInput.getName(), jarInput.getContentTypes(), jarInput.getScopes(), Format.JAR);
                try {
                    FileUtils.copyFile(jarInput.getFile(), dest);
                } catch(IOException e) { e.printStackTrace(); }}); transformInput.getDirectoryInputs().forEach(directoryInput -> { File dest = outputProvider.getContentLocation(directoryInput.getName(), directoryInput.getContentTypes(), directoryInput.getScopes(), Format.DIRECTORY);try {
                    FileUtils.copyFile(directoryInput.getFile(), dest);
                } catch(IOException e) { e.printStackTrace(); }}); }); }}Copy the code

Tags: 1 custom Transform Task name, synchronization is completed, will be displayed on the Gradle control panel, such as: transformClassesWithXXXForDebug.

Tags 2 and 3: define the specific types of files we are going to operate on during compilation, where 2 is of type:

CONTENT_CLASS:/ / the class filesCONTENT_JARS:// The jar package contains the class file and the resource file

CONTENT_RESOURCES // resource Resource file

CONTENT_NATIVE_LIBS / / local libs

CONTENT_DEX / / dex file

CONTENT_DEX_WITH_RESOURCES // Dex file and resource file
Copy the code

Mark 3: indicates the scope of the project to be used, and SCOPE_FULL_PROJECT indicates the entire project.

Mark 4: indicates whether incremental compilation is supported.

Mark 5: This is a core method that needs to be overwritten and output to the outputProvider output directory provided by Google, otherwise introducing this Transform will report an error.

TransformInvocation. GetInputs () to obtain all the compiled class files, transformInvocation. GetOutputProvider () for Google to provide the output directory, after we finish the compiled file processing, Then throw to the directory, and finally participate in the process of the dex package.

Then there’s the simple copying, and processing, which we’ll talk about in the next section, allows for aspect oriented programming.

3 the introduction of the Transform

As follows :(with a section of code)


public class FreeCoderPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        final FreeCoderExtension extension = project.getExtensions()
                .create("freecoder", FreeCoderExtension.class);
        project.afterEvaluate(innerProject -> System.out.println("extension: " + extension.name));

        // Create the converter
        FreeCoderTransform freeCoder = new FreeCoderTransform();
        / / 1
        BaseExtension baseExtension = project.getExtensions().getByType(BaseExtension.class);
        / / 2baseExtension.registerTransform(freeCoder); }}Copy the code

In tag 1: BaseExtension refers to the extension contained in the com.android.application plugin for the Android packaging process, making it easier for developers to intervene in the packaging process.

Mark 2: Register our Transform into the extension.

So far, we’ve configured our Transform, and in the next section, we’ll talk about how to achieve the effect of aspect oriented programming by changing the bytecode.