Skip to content

Commit

Permalink
生成Activity及对应layout并合并到AndroidManifest.xml中,支持生成drawable、string
Browse files Browse the repository at this point in the history
  • Loading branch information
huangx committed Jun 11, 2020
1 parent f54f083 commit 12f88b2
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 77 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ dependencies {
androidJunkCode {
variants = ["debug"]
packages = ["cn.hx.test"]
fileCountPerPackage = 30
methodCountPerClass = 20
activityCountPerPackage = 2
otherCountPerPackage = 10
methodCountPerClass = 10
resPrefix = "hx_junk_"
drawableCount = 30
stringCount = 30
}
//
//afterEvaluate {
Expand Down
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def mavenUserPassword = properties.getProperty("mavenUserPassword")
uploadArchives {
repositories {
mavenDeployer {
// repository(url: 'http://192.168.20.155:8081/nexus/content/repositories/Pycredit/') {
// repository(url: 'your repositories') {
// authentication(userName: mavenUserName, password: mavenUserPassword)
// }
repository(url: uri("../repo"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package cn.hx.plugin.junkcode.ext
class AndroidJunkCodeExt {
String[] variants = []
String[] packages = []
int fileCountPerPackage = 0
int activityCountPerPackage = 0
int otherCountPerPackage = 0
int methodCountPerClass = 0
String resPrefix = "junk_"
int drawableCount = 0
int stringCount = 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.hx.plugin.junkcode.plugin
import cn.hx.plugin.junkcode.ext.AndroidJunkCodeExt
import cn.hx.plugin.junkcode.task.AndroidJunkCodeTask
import com.android.build.gradle.AppExtension
import com.android.build.gradle.api.ApplicationVariant
import org.gradle.api.Plugin
import org.gradle.api.Project

Expand All @@ -12,21 +13,58 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
void apply(Project project) {
def android = project.extensions.getByType(AppExtension)
if (!android) {
throw IllegalArgumentException("")
throw IllegalArgumentException("must apply this plugin after 'com.android.application'")
}
def generateJunkCodeExt = project.extensions.create("androidJunkCode", AndroidJunkCodeExt)
android.applicationVariants.all { variant ->
def variantName = variant.name
if (variantName in generateJunkCodeExt.variants) {
def dir = new File(project.buildDir, "generated/source/junk/$variantName")
String packageName = findPackageName(variant)
def generateJunkCodeTask = project.task("generateJunkCode${variantName.capitalize()}", type: AndroidJunkCodeTask) {
manifestPackageName = packageName
packages = generateJunkCodeExt.packages
fileCountPerPackage = generateJunkCodeExt.fileCountPerPackage
activityCountPerPackage = generateJunkCodeExt.activityCountPerPackage
otherCountPerPackage = generateJunkCodeExt.otherCountPerPackage
methodCountPerClass = generateJunkCodeExt.methodCountPerClass
resPrefix = generateJunkCodeExt.resPrefix
drawableCount = generateJunkCodeExt.drawableCount
stringCount = generateJunkCodeExt.stringCount
outDir = dir
}
//将自动生成的AndroidManifest.xml加入到一个未被占用的manifest位置(如果都占用了就不合并了,通常较少出现全被占用情况)
for (int i = 0; i < variant.sourceSets.size(); i++) {
def sourceSet = variant.sourceSets[i]
if (!sourceSet.manifestFile.exists()) {
android.sourceSets."${sourceSet.name}".manifest.srcFile(new File(dir, "AndroidManifest.xml").absolutePath)
break
}
}
android.sourceSets."main".res.srcDir(new File(dir, "res"))
variant.registerJavaGeneratingTask(generateJunkCodeTask, dir)
}
}
}


/**
* 从AndroidManifest.xml找到package name
* @param variant
* @return
*/
static String findPackageName(ApplicationVariant variant) {
String packageName = null
for (int i = 0; i < variant.sourceSets.size(); i++) {
def sourceSet = variant.sourceSets[i]
if (sourceSet.manifestFile.exists()) {
def parser = new XmlParser()
Node node = parser.parse(sourceSet.manifestFile)
packageName = node.attribute("package")
if (packageName != null) {
break
}
}
}
return packageName
}
}
Loading

0 comments on commit 12f88b2

Please sign in to comment.