bingoogolapple / bingoogolapple.github.io

个人主页。同时也通过 Issues 记录学习笔记
http://www.bingoogolapple.cn
86 stars 22 forks source link

AnnotationProcessor 调试 #203

Open bingoogolapple opened 6 years ago

bingoogolapple commented 6 years ago
bingoogolapple commented 5 years ago

生成模块文件

private void generateModuleFile(Filer filer, String fileName) {
    try {
        FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "assets", fileName);
        String tempFilePath = fileObject.getName();
        String schemaFilePath = tempFilePath.substring(0, tempFilePath.indexOf("build/intermediates/classes")) + fileName;
        FileUtils.writeStringToFile(new File(schemaFilePath), "", StandardCharsets.UTF_8);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(String.format(">>> 生成文件 %s 失败 %s", fileName, e.getMessage()));
    }
}
bingoogolapple commented 5 years ago

通过 assets 文件来区分模块

/**
 * 将模块名称信息写到
 * 1.「模块/build/intermediates/classes/debug/assets/xxxx/」目录下
 * 2.「aar 解压后目录/classes.jar 解压后目录/assets/xxxx/」目录下
 *
 * 存在多个同名文件时会中断编译
 *
 * Android 中通过 AssetManager assetManager = mApplication.getResources().getAssets();
 * String[] list = assetManager.list('xxxx'); 来获取
 */
private void writeModuleNameAssetFile(Filer filer, String moduleName) {
    Writer writer = null;
    try {
        String path = "assets/xxxx/" + moduleName;
        FileObject fileObject = filer.createResource(StandardLocation.CLASS_OUTPUT, "", path);

        writer = fileObject.openWriter();
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(String.format(">>> 写 assets 文件 %s 失败 %s", moduleName, e.getMessage()));
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}