bingoogolapple.github.io
bingoogolapple.github.io copied to clipboard
AnnotationProcessor 调试
- 在项目根目录下的 gradle.properties 文件中加入如下两条语句
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
org.gradle.parallel=true
- 点击 Edit Configurations 配置 Remote,直接点击确认用默认配置就行,注意 address 与 gradle.properties 中的 address 保持一致
- 点击运行 remote
- 给注解处理器项目打上断点,然后 Build->Rebuild Project
生成模块文件
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()));
}
}
通过 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();
}
}
}
}