Skip to content

Commit

Permalink
解决AGP7.0.0到7.3.0自动生成混淆失效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
qq549631030 committed Aug 10, 2023
1 parent e3a4361 commit 45f05b2
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ if (PLUGIN_ENABLE.toBoolean()) {

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

Expand Down
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.3.0"
classpath "com.github.qq549631030:android-junk-code:1.3.1"
}
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ org.gradle.warning.mode=all

RELEASE_SIGNING_ENABLED=true

#PLUGIN_ENABLE = false
PLUGIN_ENABLE = true
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,5 @@ java {
dependencies {
implementation gradleApi()
implementation 'com.squareup:javapoet:1.13.0'
compileOnly 'com.android.tools.build:gradle-api:7.1.0'
compileOnly 'com.android.tools.build:gradle-api:7.0.0'
}
2 changes: 1 addition & 1 deletion library/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#project
GROUP=com.github.qq549631030
POM_ARTIFACT_ID=android-junk-code
VERSION_NAME=1.3.0
VERSION_NAME=1.3.1
POM_PACKAGING=jar

POM_NAME=AndroidJunkCode
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package cn.hx.plugin.junkcode.plugin


import cn.hx.plugin.junkcode.utils.JunkUtil
import org.gradle.api.Plugin
import org.gradle.api.Project

Expand All @@ -12,10 +12,8 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
if (!android || !android.hasProperty("applicationVariants")) {
throw IllegalArgumentException("must apply this plugin after 'com.android.application'")
}
def androidComponents = project.extensions.findByName("androidComponents")
//AGP 7.4.0+
if (androidComponents && androidComponents.hasProperty("pluginVersion")
&& (androidComponents.pluginVersion.major > 7 || androidComponents.pluginVersion.minor >= 4)) {
//AGP 7.0.0+
if (JunkUtil.isAGP7_0_0(project)) {
new NewVariantApiPlugin().apply(project)
} else {
new OldVariantApiPlugin().apply(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package cn.hx.plugin.junkcode.plugin

import cn.hx.plugin.junkcode.ext.AndroidJunkCodeExt
import cn.hx.plugin.junkcode.ext.JunkCodeConfig
import cn.hx.plugin.junkcode.task.ManifestMergeTask
import cn.hx.plugin.junkcode.task.GenerateJunkCodeTask
import cn.hx.plugin.junkcode.task.ManifestMergeTask
import cn.hx.plugin.junkcode.utils.JunkUtil
import com.android.build.api.artifact.SingleArtifact
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.tasks.TaskProvider

/**
* AGP 7.0.0+
*/
class NewVariantApiPlugin implements Plugin<Project> {

@Override
void apply(Project project) {
def android = project.extensions.findByName("android")
def androidComponents = project.extensions.findByName("androidComponents")
def generateJunkCodeExt = project.extensions.create("androidJunkCode", AndroidJunkCodeExt, project.container(JunkCodeConfig))
androidComponents.onVariants(androidComponents.selector().all(), { variant ->
Expand All @@ -23,24 +26,26 @@ class NewVariantApiPlugin implements Plugin<Project> {
println("AndroidJunkCode: generate code for variant $variantName? ${junkCodeConfig != null}")
}
if (junkCodeConfig) {
def junkCodeOutDir = new File(project.buildDir, "generated/source/junk/${variantName}")
def junkCodeOutDir = project.layout.buildDirectory.dir("generated/source/junk/${variantName}")
def generateJunkCodeTaskProvider = project.tasks.register("generate${variantName.capitalize()}JunkCode", GenerateJunkCodeTask) {
config = junkCodeConfig
namespace = android.namespace
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, {
it.javaOutputFolder
})
namespace = variant.namespace
javaOutputFolder.set(junkCodeOutDir.map { it.dir("java") })
resOutputFolder.set(junkCodeOutDir.map { it.dir("res") })
manifestOutputFile.set(junkCodeOutDir.map { it.file("AndroidManifest.xml") })
proguardOutputFile.set(junkCodeOutDir.map { it.file("proguard-rules.pro") })
}
if (variant.sources.res) {
variant.sources.res.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, {
it.resOutputFolder
})
if (JunkUtil.isAGP7_4_0(project)) {
if (variant.sources.java) {
variant.sources.java.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, {
it.javaOutputFolder
})
}
if (variant.sources.res) {
variant.sources.res.addGeneratedSourceDirectory(generateJunkCodeTaskProvider, {
it.resOutputFolder
})
}
}
TaskProvider manifestUpdater = project.tasks.register('merge' + variantName.capitalize() + 'JunkCodeManifest', ManifestMergeTask) {
it.genManifestFile.set(generateJunkCodeTaskProvider.flatMap {
Expand All @@ -56,5 +61,21 @@ class NewVariantApiPlugin implements Plugin<Project> {
})
}
})
if (!JunkUtil.isAGP7_4_0(project)) {
def android = project.extensions.findByName("android")
android.applicationVariants.all { variant ->
def variantName = variant.name
def junkCodeConfig = generateJunkCodeExt.variantConfig.findByName(variantName)
if (junkCodeConfig) {
def generateJunkCodeTaskProvider = project.tasks.named("generate${variantName.capitalize()}JunkCode", GenerateJunkCodeTask)
variant.registerJavaGeneratingTask(generateJunkCodeTaskProvider, generateJunkCodeTaskProvider.get().javaOutputFolder.get().asFile)

variant.registerGeneratedResFolders(project.files(generateJunkCodeTaskProvider.map {
it.resOutputFolder.asFile
}).builtBy(generateJunkCodeTaskProvider))
variant.mergeResourcesProvider.configure { dependsOn(generateJunkCodeTaskProvider.get()) }
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.gradle.api.Plugin
import org.gradle.api.Project

/**
* before AGP 7.4.0
* before AGP 7.0.0
*/
class OldVariantApiPlugin implements Plugin<Project> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*

/**
* before AGP 7.4.0
* before AGP 7.0.0
*/
abstract class AndroidJunkCodeTask extends DefaultTask {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cn.hx.plugin.junkcode.utils.JunkUtil
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*

abstract class GenerateJunkCodeTask extends DefaultTask {
Expand All @@ -13,7 +14,7 @@ abstract class GenerateJunkCodeTask extends DefaultTask {
abstract JunkCodeConfig config

@Input
abstract String namespace
abstract Property<String> getNamespace()

@OutputDirectory
abstract DirectoryProperty getJavaOutputFolder()
Expand Down Expand Up @@ -53,7 +54,7 @@ abstract class GenerateJunkCodeTask extends DefaultTask {
packageName = config.packageBase + "." + JunkUtil.generateName(i)
}
}
def list = JunkUtil.generateActivity(javaDir, resDir, namespace, packageName, config)
def list = JunkUtil.generateActivity(javaDir, resDir, getNamespace().get(), packageName, config)
activityList.addAll(list)
JunkUtil.generateJava(javaDir, packageName, config)
packageList.add(packageName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.squareup.javapoet.ClassName
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.MethodSpec
import com.squareup.javapoet.TypeSpec
import org.gradle.api.Project

import javax.lang.model.element.Modifier
import java.nio.file.Files
Expand Down Expand Up @@ -397,4 +398,29 @@ class JunkUtil {
}
}
}

/**
* 是否是AGP7.0.0及以上
* @param project
* @return
*/
static boolean isAGP7_0_0(Project project) {
def androidComponents = project.extensions.findByName("androidComponents")
if (androidComponents && androidComponents.hasProperty("pluginVersion")) {
return true
}
return false
}
/**
* 是否是AGP7.4.0及以上
* @param project
* @return
*/
static boolean isAGP7_4_0(Project project) {
def androidComponents = project.extensions.findByName("androidComponents")
if (androidComponents && androidComponents.hasProperty("pluginVersion") && (androidComponents.pluginVersion.major > 7 || androidComponents.pluginVersion.minor >= 4)) {
return true
}
return false
}
}

0 comments on commit 45f05b2

Please sign in to comment.