Skip to content

Commit

Permalink
support merge proguard.txt
Browse files Browse the repository at this point in the history
  • Loading branch information
kezong committed Dec 12, 2020
1 parent 3f175c7 commit 67e9a8e
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.android.build.gradle.api.LibraryVariant;
import org.gradle.api.Project;

/**
* @author yangchao on 2020/12/8.
* Temp directory used by fat-aar
*/
class DirectoryManager {

Expand Down
66 changes: 66 additions & 0 deletions source/src/main/groovy/com/kezong/fataar/EmbedProguardTask.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.kezong.fataar

import com.android.build.gradle.internal.tasks.NonIncrementalTask
import com.android.utils.FileUtils
import com.google.common.base.Charsets
import com.google.common.io.FileWriteMode
import com.google.common.io.Files
import org.gradle.api.provider.Property
import org.gradle.workers.WorkerExecutor

/**
* The task is used to merge consumer proguard (proguard.txt)
* Finally, content of sub aar's proguard.txt will append to embed module's proguard.txt
*/
class EmbedProguardTask extends NonIncrementalTask {

Collection<File> inputFiles

File outputFile

@Override
protected void doTaskAction() throws Exception {
mergeFiles(inputFiles, outputFile)
}

@Override
WorkerExecutor getWorkerExecutor() {
return null
}

@Override
Property<Boolean> getEnableGradleWorkers() {
return project.objects.property(Boolean.class).value(false)
}

static void mergeFiles(Collection<File> inputFiles, File output) {
if (inputFiles == null) {
return
}
// filter out any non-existent files
Collection<File> existingFiles = inputFiles.findAll { it ->
it.exists()
}

if (existingFiles.size() == 1) {
FileUtils.copyFile(existingFiles[0], output)
return
}

// no input? done.
if (existingFiles.isEmpty()) {
return
}

// otherwise put all the files together append to output file
for (file in existingFiles) {
try {
def content = Files.asCharSource(file, Charsets.UTF_8).read()
Files.asCharSink(output, Charsets.UTF_8, FileWriteMode.APPEND).write("$content\n")
} catch (Throwable ignore) {
def content = Files.toString(file, Charsets.UTF_8)
Files.append("$content\n", output, Charsets.UTF_8)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import org.gradle.api.Project

/**
* process jars and classes
* Created by Vigi on 2017/1/20.
* Modified by kezong on 2018/12/18
*/
class ExplodedHelper {

Expand Down
4 changes: 0 additions & 4 deletions source/src/main/groovy/com/kezong/fataar/FatUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import org.gradle.api.Project

import java.lang.ref.WeakReference

/**
* Utils
* @author kezong @since 2018-12-10 17:28
*/
class FatUtils {

private static WeakReference<Project> mProjectRef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import javax.annotation.Nullable

/**
* FlavorArtifact
* @author kezong on 2019/4/25.
*/
class FlavorArtifact {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.jvm.tasks.Jar

/**
* R file processor
* @author kezong on 2019/7/16.
* Used to generate R classes
* generate R File -> R Class -> R Jar -> unzip aar -> reBundle with R.jar
* @deprecated Prefer {@code RClassesTransform}
*/
class RClassesGenerate {

Expand Down
35 changes: 15 additions & 20 deletions source/src/main/groovy/com/kezong/fataar/VariantProcessor.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import java.nio.file.Path
import java.nio.file.Paths

/**
* Core
* Processor for variant
* Created by Vigi on 2017/2/24.
* Modified by kezong on 2019/05/29
*/
class VariantProcessor {

Expand Down Expand Up @@ -73,7 +72,7 @@ class VariantProcessor {
processResources()
processAssets()
processJniLibs()
processProguardTxt(prepareTask)
processProguardTxt(bundleTask)
processDataBinding(bundleTask)
processRClasses(transform, bundleTask)
}
Expand Down Expand Up @@ -538,30 +537,26 @@ class VariantProcessor {
}

/**
* fixme
* merge proguard.txt
*/
private void processProguardTxt(TaskProvider prepareTask) {
String taskPath = 'merge' + mVariant.name.capitalize() + 'ConsumerProguardFiles'
TaskProvider mergeFileTask = mProject.tasks.named(taskPath)
private void processProguardTxt(TaskProvider bundleTask) {
String mergeTaskName = 'merge' + mVariant.name.capitalize() + 'ConsumerProguardFiles'
TaskProvider mergeFileTask = mProject.tasks.named(mergeTaskName)
if (mergeFileTask == null) {
throw new RuntimeException("Can not find task ${taskPath}!")
throw new RuntimeException("Can not find task ${mergeTaskName}!")
}

def proguardFiles = new ArrayList<File>()
for (archiveLibrary in mAndroidArchiveLibraries) {
List<File> thirdProguardFiles = archiveLibrary.proguardRules
for (File file : thirdProguardFiles) {
if (file.exists()) {
FatUtils.logInfo('add proguard file: ' + file.absolutePath)
proguardFiles.add(file)
}
}
String embedTaskName = "embed${mVariant.name.capitalize()}ConsumerProguardFiles"
TaskProvider embedTask = mProject.tasks.register(embedTaskName, EmbedProguardTask.class) {
dependsOn(mExplodeTasks)
dependsOn(mergeFileTask)
variantName = mVariant.name
inputFiles = mAndroidArchiveLibraries.stream().map { it.proguardRules }.collect()
outputFile = mProject.file(mergeFileTask.get().outputFile)
}

mergeFileTask.configure {
dependsOn(prepareTask)
inputs.files(proguardFiles)
bundleTask.configure {
dependsOn(embedTask)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ import org.gradle.api.UnknownTaskException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.tasks.TaskProvider

/**
* @author kezong on 2019/7/16.
*/
class VersionAdapter {

private Project mProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

import javax.xml.parsers.DocumentBuilderFactory;

/**
* Created by Vigi on 2017/2/16.
* Modify by kezong on 2019/7/16.
*/
public class AndroidArchiveLibrary {

private final Project mProject;
Expand Down Expand Up @@ -106,11 +102,8 @@ public File getLintJar() {
return new File(getRootFolder(), "lint.jar");
}

public List<File> getProguardRules() {
List<File> list = new ArrayList<>();
list.add(new File(getRootFolder(), "proguard-rules.pro"));
list.add(new File(getRootFolder(), "proguard-project.txt"));
return list;
public File getProguardRules() {
return new File(getRootFolder(), "proguard.txt");
}

public File getSymbolFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

/**
* ManifestMerger for Library
* @author kezong on 2019/7/8.
*/
public class LibraryManifestMerger extends InvokeManifestMerger {

Expand Down

0 comments on commit 67e9a8e

Please sign in to comment.