Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #64 from artyomd/development
Browse files Browse the repository at this point in the history
Version 0.4
  • Loading branch information
artyomd authored May 25, 2019
2 parents a360d45 + 4aba9e7 commit 159795b
Show file tree
Hide file tree
Showing 16 changed files with 340 additions and 262 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Apr 19 21:51:18 AMT 2019
#Mon May 13 23:41:32 AMT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
11 changes: 10 additions & 1 deletion injector-android-example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ buildscript {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.4.0"
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "app.artyomd.injector:injector:${VERSION_NAME}"
}
}

plugins {
id 'com.gradle.build-scan' version '2.3'
}

buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}

allprojects {
repositories {
google()
Expand Down
2 changes: 1 addition & 1 deletion injector-android-example/lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.0.2'
inject 'com.airbnb.android:lottie:3.0.1'
inject 'com.airbnb.android:lottie:3.0.3'
}
injectConfig {
enabled = true
Expand Down
Binary file modified injector-android-example/lib/src/main/assets/lottie.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion injector-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.android.tools.build:gradle:3.4.1'
}
}

Expand Down
3 changes: 2 additions & 1 deletion injector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ plugins {
apply from: '../versions.gradle'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
google()
Expand All @@ -19,7 +20,7 @@ dependencies {
compile 'com.squareup:javapoet:1.11.1'
//compile 'com.github.javaparser:javaparser-core:3.13.6'
compile 'commons-io:commons-io:2.6'
compile 'com.android.tools.build:gradle:3.4.0'
compile 'com.android.tools.build:gradle:3.4.1'
}

def checkSigning() {
Expand Down
73 changes: 21 additions & 52 deletions injector/src/main/java/app/artyomd/injector/InjectorPlugin.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package app.artyomd.injector;

import app.artyomd.injector.extension.InjectorExtension;
import app.artyomd.injector.model.AndroidArchiveLibrary;
import app.artyomd.injector.task.ExtractAarTask;
import app.artyomd.injector.util.Utils;
import com.android.build.gradle.AppExtension;
import com.android.build.gradle.LibraryExtension;
import com.android.build.gradle.api.BaseVariant;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencyResolutionListener;
import org.gradle.api.artifacts.ModuleVersionIdentifier;
import org.gradle.api.artifacts.ResolvableDependencies;
import org.gradle.api.artifacts.ResolvedArtifact;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

public class InjectorPlugin implements Plugin<Project> {

Expand All @@ -33,44 +31,20 @@ public class InjectorPlugin implements Plugin<Project> {
private Set<ResolvedArtifact> jars;
private Set<AndroidArchiveLibrary> aars;

private static void removeOldVersions(Set<? extends ResolvedArtifact> artifactsList) {
Map<String, Map<String, ResolvedArtifact>> artifacts = new HashMap<>();
for (ResolvedArtifact artifact : artifactsList) {
ModuleVersionIdentifier id = artifact.getModuleVersion().getId();
String name = id.getName();
String group = id.getGroup();
String version = id.getVersion();
if (artifacts.containsKey(group)) {
Map<String, ResolvedArtifact> names = artifacts.get(group);
if (names.containsKey(name)) {
ResolvedArtifact old = names.get(name);
if (Utils.cmp(old.getModuleVersion().getId().getVersion(), version)) {
names.put(name, artifact);
artifactsList.remove(old);
} else {
artifactsList.remove(artifact);
}
} else {
names.put(name, artifact);
}
} else {
Map<String, ResolvedArtifact> names = new HashMap<>();
names.put(name, artifact);
artifacts.put(group, names);
}
}
}

@Override
public void apply(@NotNull Project project) {
this.project = project;
extension = project.getExtensions().create("injectConfig", InjectorExtension.class);
createExtension();
createConfiguration();
createExtractAARsTask();
project.afterEvaluate(project1 -> {
resolveArtifacts();
removeOldVersions(jars);
removeOldVersions(aars);
createExtractAARsTask();
if (jars.isEmpty() && aars.isEmpty()) {
return;
}
Utils.removeOldArtifacts(jars);
Utils.removeOldArtifacts(aars);

Object extension = project1.getExtensions().getByName("android");
if (extension instanceof LibraryExtension) {
((LibraryExtension) extension).getLibraryVariants().all(this::processVariant);
Expand All @@ -80,19 +54,9 @@ public void apply(@NotNull Project project) {
});
}

private void createExtractAARsTask() {
Task extractAars = project.getTasks().create(EXTRACT_AARS_TASK_NAME, Task.class);
extractAars.doFirst(task -> aars.forEach((Consumer<ResolvedArtifact>) resolvedArtifact -> {
String extractedAarPath = ((AndroidArchiveLibrary) resolvedArtifact).getRootFolder().getAbsolutePath();
File extractedAar = new File(extractedAarPath);
if (!extractedAar.exists()) {
try {
Utils.unzip(resolvedArtifact.getFile(), extractedAarPath);
} catch (IOException e) {
e.printStackTrace();
}
}
}));
private void createExtension() {
extension = new InjectorExtension();
project.getExtensions().add(InjectorExtension.class, "injectConfig", extension);
}

private void createConfiguration() {
Expand All @@ -119,6 +83,11 @@ public void afterResolve(@NotNull ResolvableDependencies dependencies) {
});
}

private void createExtractAARsTask() {
TaskProvider<ExtractAarTask> extractAarTaskTaskProvider = project.getTasks().register(EXTRACT_AARS_TASK_NAME, ExtractAarTask.class);
extractAarTaskTaskProvider.configure(extractAarTask -> extractAarTask.setAndroidArchiveLibraries(aars));
}

private void resolveArtifacts() {
Set<ResolvedArtifact> jars = new HashSet<>();
Set<AndroidArchiveLibrary> aars = new HashSet<>();
Expand Down
Loading

0 comments on commit 159795b

Please sign in to comment.