Skip to content

Commit

Permalink
增加配置,可不生成Activity java文件,只生成layout和写入AndroidManifest.xml
Browse files Browse the repository at this point in the history
  • Loading branch information
huangx committed Dec 8, 2020
1 parent 3aad728 commit cb2300a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ android.applicationVariants.all { variant ->
packageBase = "cn.hx.plugin.ui" //生成java类根包名
packageCount = 30 //生成包数量
activityCountPerPackage = 3 //每个包下生成Activity类数量
excludeActivityJavaFile = false //是否排除生成Activity的Java文件,默认false(layout和写入AndroidManifest.xml还会执行),主要用于处理类似神策全埋点编译过慢问题
otherCountPerPackage = 50 //每个包下生成其它类的数量
methodCountPerClass = 20 //每个类下生成方法数量
resPrefix = "junk_" //生成的layout、drawable、string等资源名前缀
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ android.applicationVariants.all { variant ->
packageBase = "cn.hx.plugin.ui"
packageCount = 30
activityCountPerPackage = 3
excludeActivityJavaFile = false
otherCountPerPackage = 50
methodCountPerClass = 20
resPrefix = "junk_"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ buildscript {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.novoda:bintray-release:0.9.2'
classpath "cn.hx.plugin:android-junk-code:1.0.5"
classpath "cn.hx.plugin:android-junk-code:1.0.6"
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:2.0.0"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion library/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
project.name=AndroidJunkCode
project.groupId=cn.hx.plugin
project.artifactId=android-junk-code
project.artifactVersion=1.0.5
project.artifactVersion=1.0.6
project.siteUrl=https://github.com/qq549631030/AndroidJunkCode
project.gitUrl=https://github.com/qq549631030/AndroidJunkCode.git
project.desc=generate junk code for android
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class JunkCodeConfig {
@Input
int activityCountPerPackage = 0
@Input
boolean excludeActivityJavaFile = false
@Input
int otherCountPerPackage = 0
@Input
int methodCountPerClass = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,27 +129,29 @@ class AndroidJunkCodeTask extends DefaultTask {
def className = activityPreName.capitalize() + "Activity"
def layoutName = "${config.resPrefix.toLowerCase()}${packageName.replace(".", "_")}_activity_${activityPreName}"
generateLayout(layoutName)
def typeBuilder = TypeSpec.classBuilder(className)
typeBuilder.superclass(ClassName.get("android.app", "Activity"))
typeBuilder.addModifiers(Modifier.PUBLIC)
//onCreate方法
def bundleClassName = ClassName.get("android.os", "Bundle")
typeBuilder.addMethod(MethodSpec.methodBuilder("onCreate")
.addAnnotation(Override.class)
.addModifiers(Modifier.PROTECTED)
.addParameter(bundleClassName, "savedInstanceState")
.addStatement("super.onCreate(savedInstanceState)")
.addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(manifestPackageName, "R"))
.build())
//其它方法
for (int j = 0; j < config.methodCountPerClass; j++) {
def methodName = generateName(j)
def methodBuilder = MethodSpec.methodBuilder(methodName)
generateMethods(methodBuilder)
typeBuilder.addMethod(methodBuilder.build())
if (!config.excludeActivityJavaFile) {
def typeBuilder = TypeSpec.classBuilder(className)
typeBuilder.superclass(ClassName.get("android.app", "Activity"))
typeBuilder.addModifiers(Modifier.PUBLIC)
//onCreate方法
def bundleClassName = ClassName.get("android.os", "Bundle")
typeBuilder.addMethod(MethodSpec.methodBuilder("onCreate")
.addAnnotation(Override.class)
.addModifiers(Modifier.PROTECTED)
.addParameter(bundleClassName, "savedInstanceState")
.addStatement("super.onCreate(savedInstanceState)")
.addStatement("setContentView(\$T.layout.${layoutName})", ClassName.get(manifestPackageName, "R"))
.build())
//其它方法
for (int j = 0; j < config.methodCountPerClass; j++) {
def methodName = generateName(j)
def methodBuilder = MethodSpec.methodBuilder(methodName)
generateMethods(methodBuilder)
typeBuilder.addMethod(methodBuilder.build())
}
def fileBuilder = JavaFile.builder(packageName, typeBuilder.build())
fileBuilder.build().writeTo(javaDir)
}
def fileBuilder = JavaFile.builder(packageName, typeBuilder.build())
fileBuilder.build().writeTo(javaDir)
addToManifestByFileIo(className, packageName)
}

Expand Down

0 comments on commit cb2300a

Please sign in to comment.