Android字符串动态加载

package com.su.shrinkResource

import org.gradle.api.Project

import java.nio.file.Files
import java.nio.file.Paths

/**
 * 建立一个文件夹
 * 文件夹模式如下:
 *  /shrink/../国家/ 语言和默认语言
 *
 */
public class ShrinkResource {
    String mVariantResourcePath;
    ArrayList<String> mShrinkResources;
    String mApptPath;
    String mCompileVersion;
    public String mProjectPath;


    public ShrinkResource(String variantResourcePath, ArrayList<String> shrinkResources, String compileVersion) {
        mVariantResourcePath = variantResourcePath;
        mShrinkResources = shrinkResources;
        this.mCompileVersion = compileVersion;
    }

    public ShrinkResource init(Project project) {
        if (project == null) {
            println("project is null");
            return null;
        }
        String sdkDir = getSDKDir(project);
        mProjectPath = project.projectDir.getAbsolutePath();
        println("============SDK Dir============" + sdkDir);
        this.mApptPath = new StringBuilder(sdkDir + File.separator + "build-tools" + File.separator + mCompileVersion + File.separator + "aapt").toString();
        println("============AAPT Path==========" + mApptPath);

        return this;
    }

    def String getSDKDir(Project project) {
        if (project == null) {
            return null;
        }
        Properties props = new Properties();
        props.load(new FileInputStream(project.rootProject.file("local.properties")));
        String dir = props.get('sdk.dir');
        return dir;
    }

    def boolean isEmpty(String str) {
        return null == str || str.length() == 0;

    }

    def void makeDir(String path) {
        if (isEmpty(path)) {
            return;
        }
        File file = new File(path);
        if (file != null) {
            file.mkdirs();
        }
    }

    def void executeCommand(String cmd) {
        if (isEmpty(cmd)) {
            return;
        }
        try {
            def execute = cmd;
            execute.execute();
            execute.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    def void makeResourceDirs() {
        if (mShrinkResources == null) {
            return;
        }
        for (String res : mShrinkResources) {
            String path = getResShrinkPath(res);
            makeDir(path)
        }
    }

    def void copyFiles(String fromPath, String targetPath) {
        if (isEmpty(fromPath) || isEmpty(targetPath)) {
            return;
        }
        try {
            Files.copy(Paths.get(fromPath), Paths.get(targetPath));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    def String getResShrinkPath(String s) {
        return mProjectPath + File.separator + "shrinkResource" + File.separator + s + File.separator;
    }

    def void startShrinking() {
        // first make tmp Workpath
        makeTmpWorkSpace();
        // 第二个拷贝文件
        copyResToTmpWorkSpace();

        // 准备 Android MainFest 以及 R 文件等

        prepareAndroidMainFest();

        prepareStringResources();

        // startToCompile
        compileStringResources();


    }

    def void compileStringResources() {
        println("======================COMPILE STRING RESOURCE==============================");

    }

    def void prepareStringResources() {
        println("=====================PREPARE STRING RESOURCE===============================");


    }

    def void prepareAndroidMainFest() {
        println("=====================PREPARE AndroidMainfest===============================");

    }


    def void copyResToTmpWorkSpace() {
        println("=================START TO COPY FILES ===========================");

        println("=================END TO COPY FILES ===========================");

    }
    private final static String S_TMP_WORKSPACE = "tmp";

    def void makeTmpWorkSpace() {
        String tmpWorkPath = getResShrinkPath(S_TMP_WORKSPACE);
        log("tmp path" + tmpWorkPath)
        deleteFiles(tmpWorkPath);
        try {
            makeDir(tmpWorkPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    def void log(Object object) {
        println(object);
    }

    def void deleteFiles(String path) {
        if (isEmpty(path)) {
            return;
        }
        try {
            new File(path).deleteDir();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}

猜你喜欢

转载自blog.csdn.net/gezihau/article/details/52751437