Skip to content

1.自定义(部分)

huangx edited this page Mar 7, 2024 · 2 revisions

如果不想用插件默认生成的代码,可通过下面实现自定义。注意,修改生成方式后必须先clean再build才生效

自定义生成包名

androidJunkCode {
    variantConfig {
        release {
            //...
            //设置此项后packageBase将无效
            packageCreator = { tuple2 ->
            //int:下标 [0,packageCount)
            def index = tuple2.first
            //StringBuilder: 生成包名格式xx.xx.xx
            def packageNameBuilder = tuple2.second
            packageNameBuilder.append("cn.hx.package" + index)
            }
        }
    }
}

自定义生成Activity

androidJunkCode {
    variantConfig {
        release {
            //...
            activityCreator = { tuple4 ->
                //int:下标 [0,activityCountPerPackage)
                def index = tuple4.first
                //StringBuilder: 生成Activity文件名
                def activityNameBuilder = tuple4.second
                //StringBuilder: 生成layout文件名
                def layoutNameBuilder = tuple4.third
                //StringBuilder: 生成layout内容
                def layoutContentBuilder = tuple4.fourth

                //
                activityNameBuilder.append("Activity${index}")
                layoutNameBuilder.append("activity_${index}")
                layoutContentBuilder.append('''<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>''')
            }
        }
    }
}

自定义生成类名

Activity除外,由前面activityCreator实现

androidJunkCode {
    variantConfig {
        release {
            //...
            classNameCreator = { tuple2 ->
                //int:下标 [0,otherCountPerPackage)
                def index = tuple2.first
                //StringBuilder: 生成Java文件名
                def classNameBuilder = tuple2.second

                //
                classNameBuilder.append("Class${index}")
            }
        }
    }
}

自定义生成方法名

androidJunkCode {
    variantConfig {
        release {
            //...
            methodNameCreator = { tuple2 ->
                //int:下标 [0,methodCountPerClass)
                def index = tuple2.first
                //StringBuilder: 生成的方法名
                def classNameBuilder = tuple2.second

                //
                classNameBuilder.append("method${index}")
            }
        }
    }
}

自定义生成drawable

目前只支持xml

androidJunkCode {
    variantConfig {
        release {
            //...
            drawableCreator = { tuple3 ->
                //int:下标 [0,drawableCount)
                def index = tuple3.first
                //StringBuilder: 生成drawable文件名
                def fileNameBuilder = tuple3.second
                //StringBuilder: 生成drawable文件内容
                def contentBuilder = tuple3.third

                //
                fileNameBuilder.append("drawable${index}")
                contentBuilder.append('''<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="4dp" />
    <stroke
        android:width="1dp"
        android:color="#333333" />
</shape>
''')
            }
        }
    }
}

自定义生成strings

androidJunkCode {
    variantConfig {
        release {
            //...
            stringCreator = { tuple3 ->
                //int:下标 [0,drawableCount)
                def index = tuple3.first
                //StringBuilder: 生成string名
                def keyBuilder = tuple3.second
                //StringBuilder: 生成string值
                def valueBuilder = tuple3.third

                //
                keyBuilder.append("string${index}")
                valueBuilder.append("value${index}")
            }
        }
    }
}

自定义生成keep.xml

androidJunkCode {
    variantConfig {
        release {
            //...
            keepCreator = { tuple2 ->
                //StringBuilder:生成文件名
                def fileNameBuilder = tuple2.first
                //StringBuilder: 生成的文件内容
                def contentBuilder = tuple2.second

                //
                fileNameBuilder.append("android_junk_code_keep")
                contentBuilder.append( "<resources xmlns:tools=\"http://schemas.android.com/tools\"\n" +
                        "    tools:keep=\"@layout/junk_*, @drawable/junk_*\" />\n")
            }
        }
    }
}

自定义生成proguardFile

androidJunkCode {
    variantConfig {
        release {
            //...
            proguardCreator = { tuple2 ->
                //List<String>:生成的包名列表
                def packageList = tuple2.first
                //StringBuilder: 生成的文件内容
                def contentBuilder = tuple2.second

                //
                for (i in 0..<packageList.size()) {
                    contentBuilder.append("-keep class ${packageList.get(i)}.**{*;}\n")
                }
            }
        }
    }
}

自定义生成方法

方法名已经实现随机,其它自己实现随机
MethodSpec.Builder用法请参考javapoet

androidJunkCode {
    variantConfig {
        release {
            //...
            methodGenerator = { methodBuilder ->
                //
                methodBuilder.addCode("" + "int total = 0;\n" + "for (int i = 0; i < 10; i++) {\n" + "  total += i;\n" + "}\n")
            }
        }
    }
}

自定义生成类

类名已经实现随机,Activity类已经实现了onCreate,其它自己实现随机
注意设置了此实现将忽略 methodGenerator,methodCountPerClass
TypeSpec.Builder用法请参考javapoet

androidJunkCode {
    variantConfig {
        release {
            //...
            typeGenerator = { typeBuilder ->
                //
                for (i in 0..<10) {
                    //注意这里要加上 import com.squareup.javapoet.MethodSpec
                    typeBuilder.addMethod(MethodSpec.methodBuilder("method" + i)
                            .addCode("" + "int total = 0;\n" + "for (int i = 0; i < 10; i++) {\n" + "  total += i;\n" + "}\n")
                            .build())
                }
            }
        }
    }
}