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

host和plugin gradle插件脚本适配兼容支持AGP4.1.0版本。 #987

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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,49 @@
/*
* Copyright (C) 2005-2017 Qihoo 360 Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.qihoo360.replugin.gradle.compat

/**
* @author RePlugin Team
*/
class CompatUtil {
/**
* Android Gradle Plugin 是否大于等于 4.1.0
* @return
*/
static def isGeAGP410() {
return (getAndroidGradlePluginVersionCompat() >= '4.1.0')
}

/**
* 获取 AGP 版本号
* @return 版本号字符串
*/
static def getAndroidGradlePluginVersionCompat() {
String version = null
try {
Class versionModel = Class.forName("com.android.builder.model.Version")
def versionFiled = versionModel.getDeclaredField("ANDROID_GRADLE_PLUGIN_VERSION")
versionFiled.setAccessible(true)
version = versionFiled.get(null)
} catch (Exception e) {
}
return version
}

private CompatUtil() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.qihoo360.replugin.gradle.host

import com.android.build.gradle.AppExtension
import com.android.build.gradle.AppPlugin
import com.qihoo360.replugin.gradle.compat.CompatUtil
import com.qihoo360.replugin.gradle.compat.VariantCompat
import com.qihoo360.replugin.gradle.host.creator.FileCreators
import com.qihoo360.replugin.gradle.host.creator.IFileCreator
Expand Down Expand Up @@ -58,15 +59,15 @@ public class Replugin implements Plugin<Project> {
}

def generateBuildConfigTask = VariantCompat.getGenerateBuildConfigTask(variant)
def appID = generateBuildConfigTask.appPackageName
def appID = getAppIdCompat(variant)
def newManifest = ComponentsGenerator.generateComponent(appID, config)
println "${TAG} countTask=${config.countTask}"

def variantData = variant.variantData
def scope = variantData.scope

//host generate task
def generateHostConfigTaskName = scope.getTaskName(AppConstant.TASK_GENERATE, "HostConfig")
def generateHostConfigTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_GENERATE, "HostConfig")
def generateHostConfigTask = project.task(generateHostConfigTaskName)

generateHostConfigTask.doLast {
Expand All @@ -81,7 +82,7 @@ public class Replugin implements Plugin<Project> {
}

//json generate task
def generateBuiltinJsonTaskName = scope.getTaskName(AppConstant.TASK_GENERATE, "BuiltinJson")
def generateBuiltinJsonTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_GENERATE, "BuiltinJson")
def generateBuiltinJsonTask = project.task(generateBuiltinJsonTaskName)

generateBuiltinJsonTask.doLast {
Expand All @@ -94,6 +95,12 @@ public class Replugin implements Plugin<Project> {
if (mergeAssetsTask) {
generateBuiltinJsonTask.dependsOn mergeAssetsTask
mergeAssetsTask.finalizedBy generateBuiltinJsonTask

if (CompatUtil.isGeAGP410()) {
def compressAssetsName = getTaskNameFromVariantScopeCompat(variant, "compress", "Assets")
def compressAssetsTask = project.tasks.getByName(compressAssetsName)
compressAssetsTask.mustRunAfter(generateBuiltinJsonTask)
}
}

variant.outputs.each { output ->
Expand Down Expand Up @@ -138,11 +145,30 @@ public class Replugin implements Plugin<Project> {
file.write(updatedContent, 'UTF-8')
}

def getAppIdCompat(def variant) {
if (CompatUtil.isGeAGP410()) {
return variant.variantData.variantDslInfo.applicationId.get()
}
return variant.generateBuildConfig.appPackageName
}

def getTaskNameFromVariantScopeCompat(def variant, String prefix, String suffix) {
def taskName = null
try {
def variantData = variant.variantData
def scope = variantData.scope
taskName = scope.getTaskName(prefix, suffix)
} catch(Exception e) {
//For AGP4.1.0
def componentProperties = variant.componentProperties
taskName = componentProperties.computeTaskName(prefix, suffix)
}
return taskName
}

// 添加 【查看所有插件信息】 任务
def addShowPluginTask(def variant) {
def variantData = variant.variantData
def scope = variantData.scope
def showPluginsTaskName = scope.getTaskName(AppConstant.TASK_SHOW_PLUGIN, "")
def showPluginsTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_SHOW_PLUGIN, "")
def showPluginsTask = project.task(showPluginsTaskName)

showPluginsTask.doLast {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,22 @@ public class RePluginHostConfigCreator implements IFileCreator {
this.variant = variant;
this.config = cfg
//make it generated in buildConfig output dir so that we don't need to hook anything
File buildConfigGeneratedDir = this.variant.getVariantData().getScope().getBuildConfigSourceOutputDir()
File buildConfigGeneratedDir = getBuildConfigGeneratedDirCompat(this.variant)
fileName = HOST_CONFIG_NAME;
fileDir = new File(buildConfigGeneratedDir, HOST_CONFIG_PATH)
}

def getBuildConfigGeneratedDirCompat(def variant) {
File buildConfigGeneratedDir = null
try {
buildConfigGeneratedDir = variant.getVariantData().getScope().getBuildConfigSourceOutputDir()
} catch(Exception e) {
//AGP4.1.0
buildConfigGeneratedDir = variant.componentProperties.paths.buildConfigSourceOutputDir
}
return buildConfigGeneratedDir
}

@Override
String getFileName() {
fileName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,9 @@ public class ReClassPlugin implements Plugin<Project> {
android.applicationVariants.all { variant ->
PluginDebugger pluginDebugger = new PluginDebugger(project, config, variant)

def variantData = variant.variantData
def scope = variantData.scope

def assembleTask = VariantCompat.getAssembleTask(variant)

def installPluginTaskName = scope.getTaskName(AppConstant.TASK_INSTALL_PLUGIN, "")
def installPluginTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_INSTALL_PLUGIN, "")
def installPluginTask = project.task(installPluginTaskName)

installPluginTask.doLast {
Expand All @@ -71,7 +68,7 @@ public class ReClassPlugin implements Plugin<Project> {
installPluginTask.group = AppConstant.TASKS_GROUP


def uninstallPluginTaskName = scope.getTaskName(AppConstant.TASK_UNINSTALL_PLUGIN, "")
def uninstallPluginTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_UNINSTALL_PLUGIN, "")
def uninstallPluginTask = project.task(uninstallPluginTaskName)

uninstallPluginTask.doLast {
Expand Down Expand Up @@ -114,14 +111,14 @@ public class ReClassPlugin implements Plugin<Project> {
installPluginTask.dependsOn assembleTask
}

def runPluginTaskName = scope.getTaskName(AppConstant.TASK_RUN_PLUGIN, "")
def runPluginTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_RUN_PLUGIN, "")
def runPluginTask = project.task(runPluginTaskName)
runPluginTask.doLast {
pluginDebugger.run()
}
runPluginTask.group = AppConstant.TASKS_GROUP

def installAndRunPluginTaskName = scope.getTaskName(AppConstant.TASK_INSTALL_AND_RUN_PLUGIN, "")
def installAndRunPluginTaskName = getTaskNameFromVariantScopeCompat(variant, AppConstant.TASK_INSTALL_AND_RUN_PLUGIN, "")
def installAndRunPluginTask = project.task(installAndRunPluginTaskName)
installAndRunPluginTask.doLast {
pluginDebugger.run()
Expand All @@ -139,6 +136,20 @@ public class ReClassPlugin implements Plugin<Project> {
android.registerTransform(transform)
}
}

def getTaskNameFromVariantScopeCompat(def variant, String prefix, String suffix) {
def taskName = null
try {
def variantData = variant.variantData
def scope = variantData.scope
taskName = scope.getTaskName(prefix, suffix)
} catch(Exception e) {
//AGP4.1.0
def componentProperties = variant.componentProperties
taskName = componentProperties.computeTaskName(prefix, suffix)
}
return taskName
}
}

class ReClassConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,14 @@ class PluginDebugger {
this.project = project
this.config = config
this.variant = variant
def variantData = this.variant.variantData
def scope = variantData.scope
def globalScope = scope.globalScope
def variantConfiguration = variantData.variantConfiguration
String archivesBaseName = globalScope.getArchivesBaseName();
def globalScope = getGlobalScopeCompat(this.variant)
def variantConfiguration = getVariantConfigurationCompat(this.variant)
String archivesBaseName = getArchivesBaseNameCompat(globalScope)
String apkBaseName = archivesBaseName + "-" + variantConfiguration.getBaseName()

File apkDir = new File(globalScope.getBuildDir(), "outputs" + File.separator + "apk")

String unsigned = (variantConfiguration.getSigningConfig() == null
String unsigned = (getSigningConfigCompat(this.variant) == null
? "-unsigned.apk"
: ".apk");
String apkName = apkBaseName + unsigned
Expand All @@ -61,6 +59,55 @@ class PluginDebugger {

}

def getArchivesBaseNameCompat(def globalScope) {
def archivesBaseName = null
try {
archivesBaseName = globalScope.getArchivesBaseName()
} catch(Exception e) {
//AGP4.1.0
archivesBaseName = globalScope.getProjectBaseName()
}
return archivesBaseName
}

def getVariantConfigurationCompat(def variant) {
def variantConfiguration = null
try {
variantConfiguration = variant.variantData.variantConfiguration
} catch(Exception e) {
//AGP4.1.0
variantConfiguration = variant.@componentProperties
}
return variantConfiguration
}

def getSigningConfigCompat(def variant) {
def signingConfig = null
def variantData = variant.variantData
try {
def variantConfiguration = variantData.variantConfiguration
signingConfig = variantConfiguration.getSigningConfig()
} catch(Exception e) {
//AGP4.1.0
def variantDslInfo = variantData.variantDslInfo
signingConfig = variantDslInfo.signingConfig
}
}

def getGlobalScopeCompat(def variant) {
def globalScope = null
try {
def variantData = variant.variantData
VariantScope scope = variantData.scope
globalScope = scope.globalScope
} catch(Exception e) {
//AGP4.1.0
def componentProperties = variant.componentProperties
globalScope = componentProperties.globalScope
}
return globalScope
}

/**
* 安装插件
* @return 是否命令执行成功
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ public class ReClassTransform extends Transform {

public ReClassTransform(Project p) {
this.project = p
def appPlugin = project.plugins.getPlugin(AppPlugin)
// taskManager 在 2.1.3 中为 protected 访问类型的,在之后的版本为 private 访问类型的,
// 使用反射访问
def taskManager = BasePlugin.metaClass.getProperty(appPlugin, "taskManager")
this.globalScope = taskManager.globalScope;
try {
def appPlugin = project.plugins.getPlugin(AppPlugin)
// taskManager 在 2.1.3 中为 protected 访问类型的,在之后的版本为 private 访问类型的,
// 使用反射访问
def taskManager = BasePlugin.metaClass.getProperty(appPlugin, "taskManager")
this.globalScope = taskManager.globalScope;
} catch(Exception e) {
//AGP1.4.0
def androidExtension = project.extensions.getByName("android")
this.globalScope = androidExtension.globalScope
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.qihoo360.replugin.gradle.plugin.manifest

import org.gradle.api.GradleException
import org.gradle.api.Project

import com.qihoo360.replugin.gradle.plugin.util.CompatUtil
import java.util.regex.Pattern

/**
Expand Down Expand Up @@ -66,13 +66,14 @@ public class ManifestAPI {
} catch (Exception e) {
// manifestOutputFile = new File(processManifestTask.getManifestOutputDirectory(), "AndroidManifest.xml")
// instantRunManifestOutputFile = new File(processManifestTask.getInstantRunManifestOutputDirectory(), "AndroidManifest.xml")
def dir = processManifestTask.getManifestOutputDirectory()
def dir = CompatUtil.isGeAGP410() ? processManifestTask.multiApkManifestOutputDirectory : processManifestTask.getManifestOutputDirectory()
if (dir instanceof File || dir instanceof String) {
manifestOutputFile = new File(dir, "AndroidManifest.xml")
} else {
manifestOutputFile = new File(dir.getAsFile().get(), "AndroidManifest.xml")
}
dir = processManifestTask.getInstantRunManifestOutputDirectory()
//AGP 4.1.0 NO InstantRun feature
dir = CompatUtil.isGeAGP410() ? processManifestTask.multiApkManifestOutputDirectory : processManifestTask.getInstantRunManifestOutputDirectory()
if (dir instanceof File || dir instanceof String) {
instantRunManifestOutputFile = new File(dir, "AndroidManifest.xml")
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2005-2017 Qihoo 360 Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.qihoo360.replugin.gradle.plugin.util

/**
* @author RePlugin Team
*/
class CompatUtil {
/**
* Android Gradle Plugin 是否大于等于 4.1.0
* @return
*/
static def isGeAGP410() {
return (getAndroidGradlePluginVersionCompat() >= '4.1.0')
}

/**
* 获取 AGP 版本号
* @return 版本号字符串
*/
static def getAndroidGradlePluginVersionCompat() {
String version = null
try {
Class versionModel = Class.forName("com.android.builder.model.Version")
def versionFiled = versionModel.getDeclaredField("ANDROID_GRADLE_PLUGIN_VERSION")
versionFiled.setAccessible(true)
version = versionFiled.get(null)
} catch (Exception e) {
}
return version
}

private CompatUtil() {}
}