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

AGP对象无法获取,ServiceLoader加载不到AGPInterfaceFactory #287

Closed
lizhangqu opened this issue Mar 5, 2022 · 6 comments
Closed

Comments

@lizhangqu
Copy link

lizhangqu commented Mar 5, 2022

gradle 7.4
AGP 7.1.2
JDK 11
OS mac

  implementation "com.didiglobal.booster:booster-android-gradle-api:4.4.0"
public class TestPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        
        project.afterEvaluate(p -> {
            p.getExtensions().findByType(AppExtension.class).getApplicationVariants().all(
                (Action<BaseVariant>)baseVariant -> {
                    BaseVariantData variantData = AGPInterfaceKt.getAGP().getVariantData(baseVariant);
                    p.getLogger().error("variantData:" + variantData);
                });
        });
    }
}
Caused by: java.util.NoSuchElementException: List is empty.
	at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:212)
	at com.didiglobal.booster.gradle.AGPInterfaceKt$AGP$2.invoke(AGPInterface.kt:219)
	at com.didiglobal.booster.gradle.AGPInterfaceKt$AGP$2.invoke(AGPInterface.kt)
	at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
	at com.didiglobal.booster.gradle.AGPInterfaceKt.getAGP(AGPInterface.kt)
	at com.kaola.TestPlugin.lambda$apply$0(TestPlugin.java:23)
	at 


如果在apply后先立即触发一次调用获取AGP,则正常

public class TestPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
        //这里先触发一次AGP调用则后续正常
        project.getLogger().error("project:" +   ProjectKt.isAapt2Enabled(project));
        project.afterEvaluate(p -> {
            p.getExtensions().findByType(AppExtension.class).getApplicationVariants().all(
                (Action<BaseVariant>)baseVariant -> {
                    BaseVariantData variantData = AGPInterfaceKt.getAGP().getVariantData(baseVariant);
                    p.getLogger().error("variantData:" + variantData);
                });
        });
    }
}

是已知问题还是使用姿势有问题?

@johnsonlee
Copy link
Collaborator

亲测在 buildSrc 中定义 TestPlugin 没有复现

Screen.Recording.2022-03-06.at.1.27.25.AM.mov

@lizhangqu
Copy link
Author

lizhangqu commented Mar 6, 2022

@johnsonlee 复现demo见 https://github.com/lizhangqu/booster-test
测试前建议先stop守护进程./gradlew --stop

@lizhangqu
Copy link
Author

lizhangqu commented Mar 6, 2022

➜ booster-test git:(master) ./gradlew :app:assembleDebug

FAILURE: Build failed with an exception.

  • What went wrong:
    A problem occurred configuring project ':app'.

List is empty.

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

Caused by: java.util.NoSuchElementException: List is empty.
at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:212)
at com.didiglobal.booster.gradle.AGPInterfaceKt$AGP$2.invoke(AGPInterface.kt:219)
at com.didiglobal.booster.gradle.AGPInterfaceKt$AGP$2.invoke(AGPInterface.kt)
at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
at com.didiglobal.booster.gradle.AGPInterfaceKt.getAGP(AGPInterface.kt)
at com.booster.test.TestPlugin.lambda$apply$0(TestPlugin.java:24)
......

BUILD FAILED in 1s
3 actionable tasks: 3 up-to-date
➜ booster-test git:(master)

@johnsonlee
Copy link
Collaborator

这个问题其实是已知问题,由于 Gradle 当前线程的 contextClassLoader 的问题导致,在 apply(Project) 直接调 getAGP() 时,contextClassLoader 是当前工程的 buildscriptClassLoader,所以能找到工程中所有的类,当在 afterEvaluate 中时,contextClassLoader 已经变了,所以就找不到当前工程中的类了,之所以 Booster 插件没有问题是因为在 Booster 插件一开始就已经初始化 AGPInterface 了,如果单独使用 AGPInterface 则需要注意线程上下文

@johnsonlee
Copy link
Collaborator

Fixed in v4.5.1

@lizhangqu
Copy link
Author

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

2 participants