Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在lib中R取值问题 #28

Open
yebin254126 opened this issue Jan 5, 2022 · 0 comments
Open

在lib中R取值问题 #28

yebin254126 opened this issue Jan 5, 2022 · 0 comments

Comments

@yebin254126
Copy link

@page在app模块中不存在问题,如果在lib中使用,由于生成的R是静态变量,不能直接使用,参考butterknife中的R2复制一份R.
但只能保证在该模块中相同,与app中的并不一定相同,这里我借鉴了butterKnife 中的方法,通过jct来替换R.

 private Id elementToId(Element element, Class<Page> annotation, int value) {
        JCTree tree = (JCTree) trees.getTree(element, getMirror(element, annotation));
        if (tree != null) { // tree can be null if the references are compiled types and not source
            rScanner.reset();
            tree.accept(rScanner);
            if (!rScanner.resourceIds.isEmpty()) {
                return rScanner.resourceIds.values().iterator().next();
            }
        }
        return new Id(value);
    } 

然后通过该方法拿到R

                    Page page = element.getAnnotation(Page.class);
                    name = StringUtils.isEmpty(page.name()) ? element.getSimpleName().toString() : page.name();
                    extra = page.extra();
                    Id id = elementToId(element, Page.class, extra);

最后在创建的时候替换即可:

                    constructorBuilder.addStatement("mPages.add(new $T($S, $S, $S, $T.$L, $L))",
                            PageInfo.class,
                            name,
                            tm.toString(),
                            PageInfo.getParams(page.params()),
                            ClassName.get(CoreAnim.class),
                            page.anim(),
                            code);

暂时只找到这一中方法,如有更好的方法,请作者大大分享一下。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant