Skip to content

Instantly share code, notes, and snippets.

@ipcjs
Created April 12, 2022 15:12
Show Gist options
  • Save ipcjs/9a63f12708c2ef7820d0f1ea5a7693b4 to your computer and use it in GitHub Desktop.
Save ipcjs/9a63f12708c2ef7820d0f1ea5a7693b4 to your computer and use it in GitHub Desktop.
尝试移除插件的原生代码=>不行,Flutter注册插件的时候会报错(
/**
* Hook <a href="https://github.com/flutter/flutter/blob/c8538873c80e34231d7a93f9ca1e5ec22c8804b1/packages/flutter_tools/gradle/flutter.gradle#L385">这个方法调用</a>
* 将`compileOnlyProjects`中指定的依赖改成通过`compileOnly`来依赖, 达到让插件类不被编译的目的!
* */
class ProjectMetaClass extends DelegatingMetaClass {
ProjectMetaClass(MetaClass metaClass) { super(metaClass) }
Object invokeMethod(Object object, String name, Object[] args) {
if (name == 'dependencies' && args.length == 1 && args[0] instanceof Closure) {
// println("project=>$name: $args")
Closure originClosure = args[0]
def closure = {
def compileOnlyProjects = [
'here_sdk',
'flutter_baidu_mapapi_map',
'flutter_baidu_mapapi_search',
'flutter_baidu_mapapi_utils',
'flutter_baidu_mapapi_base',
]
def originOwner = owner
def interceptor = ''
interceptor.metaClass.api = { notation ->
if (notation instanceof Project && compileOnlyProjects.contains(notation.name)) {
println("compileOnly $notation")
originOwner.compileOnly(notation)
return
} else {
// println("api $notation")
originOwner.api(notation)
}
}
originClosure.delegate = interceptor
originClosure.resolveStrategy = Closure.DELEGATE_ONLY
originClosure()
}
return super.invokeMethod(object, name, [closure])
}
return super.invokeMethod(object, name, args)
}
}
project.metaClass = new ProjectMetaClass(project.metaClass)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment