001/*
002 * Copyright (C) 2015-2021 KeepSafe Software
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.getkeepsafe.dexcount.treegen;
017
018import com.android.build.api.variant.BuiltArtifact;
019import com.android.build.api.variant.BuiltArtifacts;
020import com.android.build.api.variant.BuiltArtifactsLoader;
021import com.getkeepsafe.dexcount.treegen.workers.ApkishWorker;
022import org.gradle.api.file.Directory;
023import org.gradle.api.file.DirectoryProperty;
024import org.gradle.api.tasks.CacheableTask;
025import org.gradle.api.tasks.InputDirectory;
026import org.gradle.api.tasks.PathSensitive;
027import org.gradle.api.tasks.PathSensitivity;
028
029import java.io.File;
030import java.util.ArrayList;
031import java.util.List;
032
033@CacheableTask
034public abstract class ApkPackageTreeTask extends ModernGeneratePackageTreeTask<ApkishWorker.Params, ApkishWorker> {
035    @InputDirectory
036    @PathSensitive(PathSensitivity.RELATIVE)
037    public abstract DirectoryProperty getApkDirectory();
038
039    @Override
040    protected Class<ApkishWorker> getWorkerClass() {
041        return ApkishWorker.class;
042    }
043
044    @Override
045    protected void configureParams(ApkishWorker.Params params) {
046        super.configureParams(params);
047
048        Directory directory = getApkDirectory().get();
049        BuiltArtifactsLoader loader = getLoaderProperty().get();
050        BuiltArtifacts artifacts = loader.load(directory);
051        if (artifacts == null) {
052            throw new IllegalStateException("No output file found in " + directory.getAsFile().getAbsolutePath());
053        }
054        List<BuiltArtifact> elements = new ArrayList<>(artifacts.getElements());
055        BuiltArtifact artifact = elements.get(0);
056
057        params.getApkishFile().set(new File(artifact.getOutputFile()));
058    }
059}