Skip to content

Commit

Permalink
Support Andorid Gradle Plugin 3.0.0+ (#217)
Browse files Browse the repository at this point in the history
* * compat for android gradle plugin 3.0.0+

* * transform rootLocation compat

* * plugin gradle compat
  • Loading branch information
lizhangqu authored and wangyupeng1-iri committed Jul 18, 2017
1 parent 80f1bf0 commit cc2a0b2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,27 @@ public class Replugin implements Plugin<Project> {

variant.outputs.each { output ->
output.processManifest.doLast {
def manifestPath = output.processManifest.outputFile.absolutePath
def updatedContent = new File(manifestPath).getText("UTF-8").replaceAll("</application>", newManifest + "</application>")
new File(manifestPath).write(updatedContent, 'UTF-8')
output.processManifest.outputs.files.each { File file ->
def manifestFile = null;
//在gradle plugin 3.0.0之前,file是文件,且文件名为AndroidManifest.xml
//在gradle plugin 3.0.0之后,file是目录,且不包含AndroidManifest.xml,需要自己拼接
//除了目录和AndroidManifest.xml之外,还可能会包含manifest-merger-debug-report.txt等不相干的文件,过滤它
if ((file.name.equalsIgnoreCase("AndroidManifest.xml") && !file.isDirectory()) || file.isDirectory()) {
if (file.isDirectory()) {
//3.0.0之后,自己拼接AndroidManifest.xml
manifestFile = new File(file, "AndroidManifest.xml")
} else {
//3.0.0之前,直接使用
manifestFile = file
}
//检测文件是否存在
if (manifestFile != null && manifestFile.exists()) {
println "${AppConstant.TAG} handle manifest: ${manifestFile}"
def updatedContent = manifestFile.getText("UTF-8").replaceAll("</application>", newManifest + "</application>")
manifestFile.write(updatedContent, 'UTF-8')
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import com.qihoo360.replugin.gradle.plugin.injector.Injectors
import javassist.ClassPool
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.io.FileUtils
import org.gradle.api.GradleException
import org.gradle.api.Project

import java.util.regex.Pattern

/**
Expand Down Expand Up @@ -68,10 +68,20 @@ public class ReClassTransform extends Transform {
/* 读取用户配置 */
def config = project.extensions.getByName('repluginPluginConfig')

File rootLocation = outputProvider.rootLocation

File rootLocation = null
try {
rootLocation = outputProvider.rootLocation
} catch (Throwable e) {
//android gradle plugin 3.0.0+ 修改了私有变量,将其移动到了IntermediateFolderUtils中去
rootLocation = outputProvider.folderUtils.getRootFolder()
}
if (rootLocation == null) {
throw new GradleException("can't get transform root location")
}
println ">>> rootLocation: ${rootLocation}"
// Compatible with path separators for window and Linux, and fit split param based on 'Pattern.quote'
def variantDir = rootLocation.absolutePath.split(getName() + Pattern.quote(File.separator))[1]

println ">>> variantDir: ${variantDir}"

CommonData.appModule = config.appModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.qihoo360.replugin.gradle.plugin.manifest

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

import java.util.regex.Pattern
Expand Down Expand Up @@ -56,9 +57,20 @@ public class ManifestAPI {
if (processManifestTask) {
File result = null
//正常的manifest
File manifestOutputFile = processManifestTask.getManifestOutputFile()
File manifestOutputFile = null
//instant run的manifest
File instantRunManifestOutputFile = processManifestTask.getInstantRunManifestOutputFile()
File instantRunManifestOutputFile = null
try {
manifestOutputFile = processManifestTask.getManifestOutputFile()
instantRunManifestOutputFile = processManifestTask.getInstantRunManifestOutputFile()
} catch (Exception e) {
manifestOutputFile = new File(processManifestTask.getManifestOutputDirectory(), "AndroidManifest.xml")
instantRunManifestOutputFile = new File(processManifestTask.getInstantRunManifestOutputDirectory(), "AndroidManifest.xml")
}

if (manifestOutputFile == null && instantRunManifestOutputFile == null) {
throw new GradleException("can't get manifest file")
}

//打印
println " manifestOutputFile:${manifestOutputFile} ${manifestOutputFile.exists()}"
Expand Down

0 comments on commit cc2a0b2

Please sign in to comment.