Skip to content

Commit 981aab0

Browse files
authored
Merge pull request simpligility#769 from simpligility/dont-generate-existing-R
Don't generate existing R
2 parents 2113894 + 9c49382 commit 981aab0

File tree

6 files changed

+130
-19
lines changed

6 files changed

+130
-19
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
<groupId>com.simpligility.maven.plugins</groupId>
2222
<artifactId>android-maven-plugin</artifactId>
23-
<version>4.5.0-jarfix</version>
23+
<version>4.5.1-SNAPSHOT</version>
2424
<packaging>maven-plugin</packaging>
2525

2626
<name>Android Maven Plugin - android-maven-plugin</name>

src/conf/maven_checks.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ Android Maven Plugin uses the APL v2 but with copyright mentions -->
130130

131131
<!-- Checks for Size Violations. -->
132132
<!-- See http://checkstyle.sf.net/config_sizes.html -->
133-
<module name="MethodLength"/>
133+
<module name="MethodLength">
134+
<property name="countEmpty" value="false"/>
135+
</module>
134136
<module name="ParameterNumber">
135137
<property name="max" value="10"/>
136138
</module>

src/main/java/com/simpligility/maven/plugins/android/AndroidSdk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private IAndroidTarget findPlatformByApiLevel( String apiLevel )
142142
// fallback to searching for platform on standard Android platforms (isPlatform() is true)
143143
for ( IAndroidTarget t: sdkManager.getAndroidTargetManager( null ).getTargets( null ) )
144144
{
145-
if ( t.isPlatform() && t.getVersionName().equals( apiLevel ) )
145+
if ( t.isPlatform() && apiLevel.equals( t.getVersionName() ) )
146146
{
147147
return t;
148148
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2009 Jayway AB
3+
* Copyright (C) 2007-2008 JVending Masa
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.simpligility.maven.plugins.android.phase01generatesources;
18+
19+
import java.io.File;
20+
import java.net.MalformedURLException;
21+
import java.net.URL;
22+
import java.net.URLClassLoader;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
/**
27+
* Creates a ClassLoader from a Collection of classpath elements.
28+
*
29+
* @author William Ferguson - william.ferguson@xandar.com.au
30+
*/
31+
final class ClassLoaderFactory
32+
{
33+
private final List<String> classpathElements;
34+
35+
ClassLoaderFactory( List<String> classpathElements )
36+
{
37+
this.classpathElements = classpathElements;
38+
}
39+
40+
/**
41+
* @return ClassLoader containing the classpaths.
42+
*/
43+
ClassLoader create()
44+
{
45+
final List<URL> urls = new ArrayList<>();
46+
for ( final String element : classpathElements )
47+
{
48+
try
49+
{
50+
urls.add( new File( element ).toURI().toURL() );
51+
}
52+
catch ( MalformedURLException e )
53+
{
54+
throw new IllegalArgumentException( "Could not resolve dependency : " + element, e );
55+
}
56+
}
57+
return new URLClassLoader(
58+
urls.toArray( new URL[urls.size()] ),
59+
Thread.currentThread().getContextClassLoader()
60+
);
61+
}
62+
}

src/main/java/com/simpligility/maven/plugins/android/phase01generatesources/GenerateSourcesMojo.java

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
import com.simpligility.maven.plugins.android.CommandExecutor;
2121
import com.simpligility.maven.plugins.android.ExecutionException;
2222
import com.simpligility.maven.plugins.android.common.AaptCommandBuilder;
23+
import com.simpligility.maven.plugins.android.common.AaptCommandBuilder.AaptPackageCommandBuilder;
2324
import com.simpligility.maven.plugins.android.common.DependencyResolver;
2425
import com.simpligility.maven.plugins.android.common.FileRetriever;
25-
import com.simpligility.maven.plugins.android.common.AaptCommandBuilder.AaptPackageCommandBuilder;
2626
import com.simpligility.maven.plugins.android.configuration.BuildConfigConstant;
27-
2827
import org.apache.commons.io.FileUtils;
2928
import org.apache.commons.io.IOUtils;
3029
import org.apache.commons.lang3.StringUtils;
3130
import org.apache.maven.artifact.Artifact;
31+
import org.apache.maven.artifact.DependencyResolutionRequiredException;
3232
import org.apache.maven.plugin.MojoExecutionException;
3333
import org.apache.maven.plugin.MojoFailureException;
3434
import org.apache.maven.plugins.annotations.Component;
@@ -53,7 +53,6 @@
5353
import javax.xml.transform.TransformerFactory;
5454
import javax.xml.transform.dom.DOMSource;
5555
import javax.xml.transform.stream.StreamResult;
56-
5756
import java.io.File;
5857
import java.io.FileWriter;
5958
import java.io.IOException;
@@ -812,14 +811,37 @@ private void generateR() throws MojoExecutionException
812811
throw new MojoExecutionException( "", e );
813812
}
814813

815-
final ResourceClassGenerator resGenerator = new ResourceClassGenerator( this, targetDirectory, genDirectory );
814+
final ClassLoader compileClassLoader = getCompileClassLoader();
815+
final ResourceClassGenerator resGenerator = new ResourceClassGenerator(
816+
this,
817+
targetDirectory,
818+
genDirectory,
819+
compileClassLoader
820+
);
816821
generateCorrectRJavaForApklibDependencies( resGenerator );
817822
generateCorrectRJavaForAarDependencies( resGenerator );
818823

819824
getLog().info( "Adding R gen folder to compile classpath: " + genDirectory );
820825
project.addCompileSourceRoot( genDirectory.getAbsolutePath() );
821826
}
822827

828+
/**
829+
* @return ClassLoader containing the compile paths.
830+
*/
831+
private ClassLoader getCompileClassLoader()
832+
{
833+
try
834+
{
835+
final List<String> runtimeClasspathElements = project.getCompileClasspathElements();
836+
final ClassLoaderFactory factory = new ClassLoaderFactory( runtimeClasspathElements );
837+
return factory.create();
838+
}
839+
catch ( DependencyResolutionRequiredException e )
840+
{
841+
throw new IllegalStateException( "Mojo should have resolved dependencies", e );
842+
}
843+
}
844+
823845
/**
824846
* Generate correct R.java for apklibs dependencies of a current project
825847
*
@@ -854,7 +876,7 @@ private void generateCorrectRJavaForApklibDependencies( ResourceClassGenerator r
854876
/**
855877
* Generate correct R.java for aar dependencies of a current project
856878
*
857-
* @throws MojoExecutionException
879+
* @throws MojoExecutionException if it could not generate the R java for one of the libraries.
858880
*/
859881
private void generateCorrectRJavaForAarDependencies( ResourceClassGenerator resourceGenerator )
860882
throws MojoExecutionException

src/main/java/com/simpligility/maven/plugins/android/phase01generatesources/ResourceClassGenerator.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
import com.android.builder.symbols.RGeneration;
55
import com.android.builder.symbols.SymbolIo;
66
import com.android.builder.symbols.SymbolTable;
7-
import com.android.utils.ILogger;
87
import org.apache.maven.artifact.Artifact;
9-
import org.apache.maven.plugin.MojoExecutionException;
108
import org.apache.maven.plugin.logging.Log;
119

1210
import java.io.File;
@@ -25,26 +23,34 @@ final class ResourceClassGenerator
2523
private final File targetDirectory;
2624
private final File genDirectory;
2725
private final Log log;
28-
private final ILogger androidUtilsLog;
26+
private final ClassLoader compileClassLoader;
2927

30-
ResourceClassGenerator( final GenerateSourcesMojo mojo, final File targetDirectory,
31-
final File genDirectory )
28+
ResourceClassGenerator( final GenerateSourcesMojo mojo,
29+
final File targetDirectory,
30+
final File genDirectory,
31+
final ClassLoader compileClassLoader
32+
)
3233
{
3334
this.mojo = mojo;
3435
this.targetDirectory = targetDirectory;
3536
this.genDirectory = genDirectory;
3637
this.log = mojo.getLog();
37-
this.androidUtilsLog = new MavenILogger( log );
38+
this.compileClassLoader = compileClassLoader;
3839
}
3940

4041
/**
41-
* see {@link com.android.builder.core.AndroidBuilder#processResources(com.android.builder.internal.aapt.Aapt,
42+
* Generates R java files those libraries that do not already have their R java in the compile classpath.
43+
*
44+
* If we are generating an integration test APK and the APK under test has a reference to a library for which
45+
* it generated an R java, then we don't want to generate that R java again for the test APK because otherwise
46+
* Proguard will fail when building the test APK because of duplication of the R java in the compile classpath.
47+
*
48+
* See {@link com.android.builder.core.AndroidBuilder#processResources(com.android.builder.internal.aapt.Aapt,
4249
* com.android.builder.internal.aapt.AaptPackageConfig.Builder, boolean)}
4350
*
44-
* @param libraries
45-
* @throws MojoExecutionException
51+
* @param libraries AAR libraries for which to generate R java files.
4652
*/
47-
public void generateLibraryRs( final Set<Artifact> libraries ) throws MojoExecutionException
53+
public void generateLibraryRs( final Set<Artifact> libraries )
4854
{
4955
// list of all the symbol tables
5056
final List<SymbolTable> symbolTables = new ArrayList<>( libraries.size() );
@@ -59,7 +65,12 @@ public void generateLibraryRs( final Set<Artifact> libraries ) throws MojoExecut
5965
{
6066
final File libManifestFile = new File( unpackedLibDirectory, "AndroidManifest.xml" );
6167
final String packageName = new DefaultManifestParser( libManifestFile ).getPackage();
62-
log.info( "Reading R for " + packageName + " at " + rFile );
68+
if ( rJavaAlreadyExists( packageName ) )
69+
{
70+
log.info( "Not creating R for " + packageName + " as it already exists" );
71+
continue;
72+
}
73+
log.info( "Generating R for " + packageName + " at " + rFile );
6374

6475
SymbolTable libSymbols = SymbolIo.read( rFile );
6576
libSymbols = libSymbols.rename( packageName, libSymbols.getTableName() );
@@ -80,4 +91,18 @@ public void generateLibraryRs( final Set<Artifact> libraries ) throws MojoExecut
8091
RGeneration.generateRForLibraries( mainSymbols, symbolTables, genDirectory.getAbsoluteFile(), false );
8192
}
8293

94+
private boolean rJavaAlreadyExists( String packageName )
95+
{
96+
final String rJavaClass = packageName + ".R";
97+
try
98+
{
99+
compileClassLoader.loadClass( rJavaClass );
100+
return true;
101+
}
102+
catch ( ClassNotFoundException e )
103+
{
104+
log.debug( "Could not resolve R java : " + rJavaClass );
105+
return false;
106+
}
107+
}
83108
}

0 commit comments

Comments
 (0)