Skip to content

Commit

Permalink
解决可动态调整manifestPlaceholders项目编译失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
huangx committed Jun 1, 2022
1 parent 2f6695b commit 0fd8ce9
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 49 deletions.
73 changes: 57 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'android-junk-code'
if (PLUGIN_ENABLE.toBoolean()) {
apply plugin: 'android-junk-code'
}
apply plugin: 'com.getkeepsafe.dexcount'
apply plugin: 'kotlin-kapt'

Expand Down Expand Up @@ -30,23 +32,62 @@ android {
checkReleaseBuilds false
abortOnError false
}
}

androidJunkCode {
def config = {
packageBase = "cn.hx.plugin.ui"
packageCount = 30
activityCountPerPackage = 3
excludeActivityJavaFile = false
otherCountPerPackage = 50
methodCountPerClass = 20
resPrefix = "junk_"
drawableCount = 300
stringCount = 300
flavorDimensions 'PRODUCT'

productFlavors {
free {
dimension 'PRODUCT'
}
pro {
dimension 'PRODUCT'
}
}

applicationVariants.all { variant ->
def mergedFlavor = variant.mergedFlavor
switch (variant.flavorName) {
case "free":
switch (variant.buildType.name) {
case "debug":
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey1"]
break
case "release":
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey2"]
break
}
break
case "pro":
switch (variant.buildType.name) {
case "debug":
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey3"]
break
case "release":
mergedFlavor.manifestPlaceholders += ["API_KEY": "appKey4"]
break
}
break
}
}
variantConfig {
debug config
release config
}

if (PLUGIN_ENABLE.toBoolean()) {
androidJunkCode {
def config = {
packageBase = "cn.hx.plugin.ui"
packageCount = 30
activityCountPerPackage = 30
excludeActivityJavaFile = false
otherCountPerPackage = 50
methodCountPerClass = 20
resPrefix = "junk_"
drawableCount = 300
stringCount = 300
}
variantConfig {
freeRelease config
proRelease config
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data android:name="API_KEY" android:value="${API_KEY}"/>
<activity
android:name=".MainActivity"
android:exported="true">
Expand Down
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.github.qq549631030:android-junk-code:1.1.2"
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:3.0.1"

// classpath project(":library")
classpath "com.github.qq549631030:android-junk-code:1.1.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ kotlin.code.style=official

org.gradle.warning.mode=all

RELEASE_SIGNING_ENABLED=true
RELEASE_SIGNING_ENABLED=true

PLUGIN_ENABLE = true
10 changes: 10 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.vanniktech:gradle-maven-publish-plugin:0.18.0'
}
}

apply plugin: 'groovy'
apply plugin: "com.vanniktech.maven.publish"

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.1.2
VERSION_NAME=1.1.3

POM_ARTIFACT_ID=android-junk-code
POM_NAME=AndroidJunkCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer

class AndroidJunkCodeExt {
Map<String, Closure<JunkCodeConfig>> configMap = [:]

NamedDomainObjectContainer<JunkCodeConfig> variantConfig

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AndroidJunkCodePlugin implements Plugin<Project> {

@Override
void apply(Project project) {
def android = project.extensions.getByType(AppExtension)
def android = project.extensions.findByType(AppExtension)
if (!android) {
throw IllegalArgumentException("must apply this plugin after 'com.android.application'")
}
Expand All @@ -25,27 +25,6 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
createGenerateJunkCodeTask(project, android, variant, junkCodeConfig)
}
}

project.afterEvaluate {
android.applicationVariants.all { variant ->
def variantName = variant.name
def generateJunkCodeTaskName = "generate${variantName.capitalize()}JunkCode"
def generateJunkCodeTask = project.tasks.findByName(generateJunkCodeTaskName)
if (generateJunkCodeTask) {
//已经用variantConfig方式配置过了
return
}
def closure = generateJunkCodeExt.configMap[variantName]
if (closure) {
def junkCodeConfig = new JunkCodeConfig()
closure.delegate = junkCodeConfig
closure.resolveStrategy = Closure.DELEGATE_FIRST
closure.call()
println("AndroidJunkCode: configMap配置方式已过时,请使用variantConfig配置方式")
createGenerateJunkCodeTask(project, android, variant, junkCodeConfig)
}
}
}
}

private def createGenerateJunkCodeTask = { project, android, variant, junkCodeConfig ->
Expand All @@ -69,11 +48,7 @@ class AndroidJunkCodePlugin implements Plugin<Project> {
for (int i = variant.sourceSets.size() - 1; i >= 0; i--) {
def sourceSet = variant.sourceSets[i]
if (!sourceSet.manifestFile.exists()) {
android.sourceSets."${sourceSet.name}".manifest.srcFile(manifestFile.absolutePath)
def processMainManifestTask = project.tasks.findByName("process${variantName.capitalize()}MainManifest")
if (processMainManifestTask) {
processMainManifestTask.dependsOn(generateJunkCodeTask)
}
sourceSet.manifest.srcFile(manifestFile.absolutePath)
break
}
}
Expand Down

0 comments on commit 0fd8ce9

Please sign in to comment.