1
+ package processing.android
2
+
3
+ import org.gradle.api.Plugin
4
+ import org.gradle.api.Project
5
+ import org.gradle.api.plugins.PluginManager
6
+ import org.gradle.api.plugins.ExtensionContainer
7
+ import org.gradle.plugins.ide.idea.IdeaPlugin
8
+ import org.gradle.plugins.ide.idea.model.IdeaModel
9
+ import org.gradle.plugins.ide.idea.model.IdeaModule
10
+ import org.gradle.api.artifacts.Configuration
11
+ import org.gradle.api.artifacts.transform.ArtifactTransform
12
+
13
+ import static org.gradle.api.internal.artifacts.ArtifactAttributes.ARTIFACT_FORMAT
14
+ import com.android.build.gradle.internal.dependency.AarTransform
15
+ import com.android.build.gradle.internal.dependency.ExtractAarTransform
16
+ import com.android.build.gradle.internal.publishing.AndroidArtifacts
17
+ import com.android.builder.aar.AarExtractor
18
+ import com.google.common.collect.ImmutableList
19
+ import java.util.regex.Pattern
20
+ import static com.android.SdkConstants.FD_JARS
21
+ import static com.android.SdkConstants.FN_CLASSES_JAR
22
+
23
+ import java.io.File
24
+ import java.nio.file.Files
25
+ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING
26
+
27
+ /**
28
+ * Build Gradle plgin needed to use aar files as dependencies in a pure java library project.
29
+ * Adapted from the following plugin by nekocode
30
+ * https://github.com/nekocode/Gradle-Import-Aar
31
+ * Ported to Groovy, and made specific to the needs of the Android mode build process (i.e.: this plugin
32
+ * is not meant to be used with other projects).
33
+ */
34
+ class ImportAar implements Plugin<Project > {
35
+
36
+ final String CONFIG_NAME_POSTFIX = " Aar"
37
+
38
+ void apply (Project project ) {
39
+ def aar = AndroidArtifacts . TYPE_AAR
40
+ def jar = AndroidArtifacts . TYPE_JAR
41
+ def exp = AndroidArtifacts . TYPE_EXPLODED_AAR
42
+
43
+ // // Create aar configurations
44
+ Collection<Configuration > allConfigs = project. getConfigurations(). toList()
45
+ for (Configuration config : allConfigs) {
46
+ Configuration aarConfig = project. configurations. maybeCreate(config. name + CONFIG_NAME_POSTFIX )
47
+
48
+ // Add extracted jars to original configuration after project evaluating
49
+ aarConfig. getAttributes(). attribute(ARTIFACT_FORMAT , jar)
50
+ project. afterEvaluate {
51
+ for (File jarFile : aarConfig) {
52
+ // print "================================================> FILE "
53
+ // println jarFile
54
+ // println jarFile.getName()
55
+ // for (String s: project.sourceSets.main.compileClasspath) {
56
+ // println s
57
+ // }
58
+ // project.getDependencies().add(config.name, project.files(jarFile))
59
+
60
+ // Add jar file to classpath
61
+ project. sourceSets. main. compileClasspath + = project. files(jarFile)
62
+
63
+ File libraryFolder = new File (System . getProperty(" user.dir" ), " build/libs" )
64
+ libraryFolder. mkdirs()
65
+
66
+ // Strip version number when copying
67
+ String name = jarFile. getName()
68
+ int p = name. lastIndexOf(" -" )
69
+ String libName = name. substring(0 , p) + " .jar"
70
+ File libraryJar = new File (libraryFolder, libName)
71
+ Files . copy(jarFile. toPath(), libraryJar. toPath(), REPLACE_EXISTING )
72
+ }
73
+
74
+ }
75
+
76
+ // Tell Idea about our aar configuration
77
+ PluginManager pluginManager = project. getPluginManager()
78
+ pluginManager. apply(IdeaPlugin . class)
79
+ ExtensionContainer extensions = project. getExtensions()
80
+ // IdeaModel model = extensions.getByType(IdeaModel.class)
81
+ IdeaModel model = extensions. getByName(" idea" )
82
+ IdeaModule module = model. getModule()
83
+ module. scopes. PROVIDED . plus + = [aarConfig]
84
+ }
85
+
86
+ // Register aar transform
87
+ project. dependencies {
88
+ registerTransform {
89
+ from. attribute(ARTIFACT_FORMAT , aar)
90
+ to. attribute(ARTIFACT_FORMAT , jar)
91
+ artifactTransform(AarJarArtifactTransform . class)
92
+ }
93
+ }
94
+ }
95
+
96
+ static class AarJarArtifactTransform extends ArtifactTransform {
97
+ @Override
98
+ List<File > transform (File file ) {
99
+ // println "Transforming---------------------------------"
100
+ // println outputDirectory
101
+ // println file
102
+ File explodedDir = new File (getOutputDirectory(), " exploded" )
103
+ // println explodedDir
104
+
105
+ AarExtractor aarExtractor = new AarExtractor ()
106
+ aarExtractor. extract(file, explodedDir)
107
+ File classesJar = new File (new File (explodedDir, FD_JARS ), FN_CLASSES_JAR )
108
+ // println classesJar
109
+
110
+ // String[] names = file.getPath().split(Pattern.quote(File.separator))
111
+ // print "NAMES "
112
+ // println names
113
+ // print "NAME "
114
+ // println file.getName()
115
+ // String aarName = names[names.length - 4].replace(".aar", "")
116
+ String aarName = file. getName(). replace(" .aar" , " " )
117
+ // print "AAR NAME "
118
+ // println aarName
119
+ File renamedJar = new File (getOutputDirectory(), aarName + " .jar" )
120
+ renamedJar << classesJar. bytes
121
+
122
+ return ImmutableList . of(renamedJar)
123
+ }
124
+ }
125
+ }
0 commit comments