Skip to content

Commit

Permalink
自动生成proguard文件,无需要手动配置
Browse files Browse the repository at this point in the history
  • Loading branch information
qq549631030 committed Aug 8, 2023
1 parent 47ae6f8 commit 1b898d0
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ androidJunkCode {
}
```

如果APP开启了混淆,需要在混淆文件里配置
如果APP开启了混淆,需要在混淆文件里配置 (1.3.0之后不需求配置)

```
#cn.hx.plugin.ui为前面配置的packageBase
Expand Down
5 changes: 1 addition & 4 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,4 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keep class cn.hx.plugin.ui.** {*;}
-keep class cn.hx.package*.** {*;}
#-renamesourcefileattribute SourceFile
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:3.0.1"
if (PLUGIN_ENABLE.toBoolean()) {
classpath "com.github.qq549631030:android-junk-code:1.2.9"
classpath "com.github.qq549631030:android-junk-code:1.3.0"
}
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#project
GROUP=com.github.qq549631030
VERSION_NAME=1.2.9
VERSION_NAME=1.3.0

POM_ARTIFACT_ID=android-junk-code
POM_NAME=AndroidJunkCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class NewVariantApiPlugin implements Plugin<Project> {
javaOutputFolder.set(new File(junkCodeOutDir, "java"))
resOutputFolder.set(new File(junkCodeOutDir, "res"))
manifestOutputFile.set(new File(junkCodeOutDir, "AndroidManifest.xml"))
proguardOutputFile.set(new File(junkCodeOutDir, "proguard-rules.pro"))
}
if (variant.sources.java) {
variant.sources.java.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, {
Expand All @@ -50,6 +51,9 @@ class NewVariantApiPlugin implements Plugin<Project> {
.wiredWithFiles({ it.mergedManifest },
{ it.updatedManifest })
.toTransform(SingleArtifact.MERGED_MANIFEST.INSTANCE)
variant.proguardFiles.add(generateJunkCodeTaskProvider.flatMap {
it.proguardOutputFile
})
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ class OldVariantApiPlugin implements Plugin<Project> {
def javaDir = new File(junkCodeOutDir, "java")
def resDir = new File(junkCodeOutDir, "res")
def manifestFile = new File(junkCodeOutDir, "AndroidManifest.xml")
def proguardFile = new File(junkCodeOutDir, "proguard-rules.pro")
def generateJunkCodeTask = project.tasks.create("generate${variantName.capitalize()}JunkCode", AndroidJunkCodeTask) {
config = junkCodeConfig
namespace = junkCodeNamespace
javaOutDir = javaDir
resOutDir = resDir
manifestOutFile = manifestFile
proguardOutFile = proguardFile
}
//将自动生成的AndroidManifest.xml加入到一个未被占用的manifest位置(如果都占用了就不合并了,通常较少出现全被占用情况)
for (int i = variant.sourceSets.size() - 1; i >= 0; i--) {
Expand Down Expand Up @@ -84,6 +86,7 @@ class OldVariantApiPlugin implements Plugin<Project> {
} else {
variant.registerResGeneratingTask(generateJunkCodeTask, resDir)//AGP 1.1.0+
}
variant.getBuildType().buildType.proguardFile(proguardFile)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ abstract class AndroidJunkCodeTask extends DefaultTask {
@OutputFile
abstract File manifestOutFile

@OutputFile
abstract File proguardOutFile

private List<String> packageList = new ArrayList<>()
private List<String> activityList = new ArrayList<>()

@TaskAction
Expand All @@ -50,7 +54,10 @@ abstract class AndroidJunkCodeTask extends DefaultTask {
def list = JunkUtil.generateActivity(javaOutDir, resOutDir, namespace, packageName, config)
activityList.addAll(list)
JunkUtil.generateJava(javaOutDir, packageName, config)
packageList.add(packageName)
}
//生成混淆文件
JunkUtil.generateProguard(proguardOutFile, packageList)
}
if (config.resGenerator) {//自定义生成res逻辑
config.resGenerator.execute(resOutDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ abstract class GenerateJunkCodeTask extends DefaultTask {
@OutputFile
abstract RegularFileProperty getManifestOutputFile()

@Internal
List<String> activityList = new ArrayList<>()
@OutputFile
abstract RegularFileProperty getProguardOutputFile()


private List<String> packageList = new ArrayList<>()
private List<String> activityList = new ArrayList<>()

@TaskAction
void taskAction() {
Expand All @@ -52,7 +56,10 @@ abstract class GenerateJunkCodeTask extends DefaultTask {
def list = JunkUtil.generateActivity(javaDir, resDir, namespace, packageName, config)
activityList.addAll(list)
JunkUtil.generateJava(javaDir, packageName, config)
packageList.add(packageName)
}
//生成混淆文件
JunkUtil.generateProguard(getProguardOutputFile().get().asFile, packageList)
}
if (config.resGenerator) {
config.resGenerator.execute(resOutDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,21 @@ class JunkUtil {
writeStringToFile(manifestFile, sb.toString())
}


/**
* 生成proguard-rules.pro
*
* @param manifestFile
* @param activityList
*/
static void generateProguard(File proguardFile, List<String> packageList) {
StringBuilder sb = new StringBuilder()
for (i in 0..<packageList.size()) {
sb.append("-keep public class ${packageList.get(i)}.**{*;}\n")
}
writeStringToFile(proguardFile, sb.toString())
}

/**
* java写入文件
* @param javaDir
Expand Down

0 comments on commit 1b898d0

Please sign in to comment.