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

feat(core.gradle-plugin,core.loader):优化从AndroidManifest中解析Receiver的action信息的性能 #696

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Tencent is pleased to support the open source community by making Tencent Shadow available.
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.tencent.shadow.core.gradle

import groovy.xml.DOMBuilder
import org.w3c.dom.Document
import org.w3c.dom.Element
import java.io.File
import javax.xml.transform.OutputKeys
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult

/**
* 解析插件的AndroidManifest.xml文件
* 由于系统开放接口不提供广播的action信息,此处采用手动解析的方式处理,减少插件化的适配工作
* 后续对于AndroidManifest.xml的处理可在此基础上扩展
*
* @author [email protected]
*/
internal fun rebuildManifest(manifestFile: File) {
println("RebuildManifest task run")
val document = DOMBuilder.parse(manifestFile.reader(Charsets.UTF_8))
try {
buildReceiver(document)
}catch (e : Exception){
println("RebuildManifest fail , check <receiver> tag")
}
writeDocument(document,manifestFile)
}

fun writeDocument(document: Document,manifestFile : File) {
val newTransformer = TransformerFactory.newInstance().newTransformer()
newTransformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8")
newTransformer.setOutputProperty(OutputKeys.INDENT,"yes")
newTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"no")
newTransformer.transform(DOMSource(document),StreamResult(manifestFile.writer()))
}

@Throws(Exception::class)
fun buildReceiver(document: Document){
val receiverElements = document.getElementsByTagName("receiver")
for (receiverIndex in 0 until receiverElements.length){
val receiverNode = receiverElements.item(receiverIndex)
val element = receiverNode as Element
val receiveNodeValue = element.getAttribute("android:name")
for (intentFilterIndex in 0 until receiverNode.childNodes.length){
val intentFilterNode = receiverNode.childNodes.item(intentFilterIndex)
if(intentFilterNode.nodeName == "intent-filter"){
for (actionIndex in 0 until intentFilterNode.childNodes.length){
val actionNode = intentFilterNode.childNodes.item(actionIndex)
if(actionNode.nodeName == "action"){
val metaDateElement = document.createElement("meta-data")
metaDateElement.setAttribute("android:name",(actionNode as Element).getAttribute("android:name"))
metaDateElement.setAttribute("android:value",receiveNodeValue)
receiverNode.appendChild(metaDateElement)
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

package com.tencent.shadow.core.gradle

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.tasks.ProcessMultiApkApplicationManifest
import com.tencent.shadow.core.gradle.extensions.PackagePluginExtension
import com.tencent.shadow.core.transform.ShadowTransform
import com.tencent.shadow.core.transform_kit.AndroidClassPoolBuilder
Expand Down Expand Up @@ -61,6 +63,24 @@ class ShadowPlugin : Plugin<Project> {
initAndroidClassPoolBuilder(baseExtension, project)

createPackagePluginTasks(project)

parseManifest(project)
}
}

private fun parseManifest(project: Project){
val appExtension: AppExtension = project.extensions.getByType(AppExtension::class.java)
appExtension.applicationVariants.all { variant ->
variant.outputs.all { output ->
val processManifestTask = output.processManifestProvider.get()
processManifestTask.doLast {
val manifestFile = File(
(processManifestTask as ProcessMultiApkApplicationManifest).multiApkManifestOutputDirectory.get().asFile,
"AndroidManifest.xml"
)
rebuildManifest(manifestFile)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,9 @@ object LoadPluginBloc {
packageArchiveInfo
})

val buildManifestInfo = executorService.submit(Callable {
ParseManifestBloc.parse(hostAppContext, installedApk)
})

val buildPluginInfo = executorService.submit(Callable {
val packageInfo = getPackageInfo.get()
val manifestInfo = buildManifestInfo.get()
ParsePluginApkBloc.parse(packageInfo, manifestInfo, loadParameters, hostAppContext)
ParsePluginApkBloc.parse(packageInfo, loadParameters, hostAppContext)
})

val buildPackageManager = executorService.submit(Callable {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.tencent.shadow.core.loader.blocs

import android.content.Context
import android.content.pm.ActivityInfo
import android.content.pm.PackageInfo
import android.os.Build
import com.tencent.shadow.core.load_parameters.LoadParameters
Expand All @@ -41,7 +42,6 @@ object ParsePluginApkBloc {
@Throws(ParsePluginApkException::class)
fun parse(
packageArchiveInfo: PackageInfo,
manifestInfo: ManifestInfo,
loadParameters: LoadParameters,
hostAppContext: Context
): PluginInfo {
Expand Down Expand Up @@ -84,13 +84,12 @@ object ParsePluginApkBloc {
pluginInfo.putActivityInfo(PluginActivityInfo(it.name, it.themeResource, it))
}

val receiveMap = manifestInfo.receivers.map { it.name to it }.toMap()
packageArchiveInfo.receivers?.forEach {
pluginInfo.putReceiverInfo(
PluginReceiverInfo(
it.name,
it,
receiveMap[it.name]?.actions()
it.toReceiveActions()
)
)
}
Expand All @@ -100,4 +99,10 @@ object ParsePluginApkBloc {
}
return pluginInfo
}

private fun ActivityInfo.toReceiveActions(): List<String>? {
return this.metaData?.keySet()?.filter {
this.metaData?.getString(it) == this.name
}
}
}