Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
It seems that the aar/classes.jar should NOT include the R class or B…
Browse files Browse the repository at this point in the history
…uildConfig. If the R is included, dex will fail with an error about finding the same R in multiple jars.

I haven't really found anyone saying this explicity nor any docs that say as much.  I do have a gradle build that depends on an aar that is generated with this mojo that will fail if R is in the classes.jar.  I also know that the android gradle build tools combine resources.  The only other evidence I have that hints at not including R is the following discussion on google groups:

https://groups.google.com/forum/#!msg/adt-dev/uWppd9l8UNw/w6Q_9N13mLcJ
  • Loading branch information
markrileybot committed Sep 27, 2013
1 parent a9b868a commit a480d33
Showing 1 changed file with 58 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ public class AarMojo extends AbstractAndroidMojo
* NOTE: This is inconsistent with APK where the folder is called "lib"
*/
public static final String NATIVE_LIBRARIES_FOLDER = "libs";

/**
* Build folder to place built native libraries into
*
* @parameter expression="${android.ndk.build.ndk-output-directory}"
* default-value="${project.build.directory}/ndk-libs"
*/
private File ndkOutputDirectory;

/**
* <p>Classifier to add to the artifact generated. If given, the artifact will be an attachment instead.</p>
*
Expand Down Expand Up @@ -95,11 +95,26 @@ public class AarMojo extends AbstractAndroidMojo
*/
@PullParameter
private String ndkClassifier;

private List<String> sourceFolders = new ArrayList<String>();

/**
* Specifies the files that should be included in the classes.jar within the aar
*
* @parameter
*/
@PullParameter
private String[] classesJarIncludes = new String[]{"**/*"};

/**
* Specifies the files that should be excluded from the classes.jar within the aar
*
* @parameter
*/
@PullParameter
private String[] classesJarExcludes = new String[]{"**/R.class", "**/R$*.class", "**/BuildConfig.class"};

private List<String> sourceFolders = new ArrayList<String>();

/**
* @throws MojoExecutionException
* @throws MojoFailureException
*/
Expand All @@ -108,18 +123,18 @@ public void execute() throws MojoExecutionException, MojoFailureException
String out = project.getBuild().getDirectory();
for ( String src : project.getCompileSourceRoots() )
{
if ( !src.startsWith( out ) )
if ( !src.startsWith( out ) )
{
sourceFolders.add( src );
}
}

generateIntermediateApk();

CommandExecutor executor = CommandExecutor.Factory.createDefaultCommmandExecutor();
executor.setLogger( this.getLog() );

File outputFile = createApkLibraryFile();
File outputFile = createApkLibraryFile( createAarClassesJar() );

if ( classifier == null )
{
Expand All @@ -134,11 +149,40 @@ public void execute() throws MojoExecutionException, MojoFailureException
}

/**
* Creates an appropriate aar/classes.jar that does not include R
*
* @return
* @throws MojoExecutionException
*/
protected File createApkLibraryFile() throws MojoExecutionException
protected File createAarClassesJar() throws MojoExecutionException
{
final File classesJar = new File( project.getBuild().getDirectory(),
project.getBuild().getFinalName() + ".aar.classes.jar" );
try
{
JarArchiver jarArchiver = new JarArchiver();
jarArchiver.setDestFile( classesJar );
jarArchiver.addDirectory( new File( project.getBuild().getOutputDirectory() ),
classesJarIncludes,
classesJarExcludes );
jarArchiver.createArchive();
return classesJar;
}
catch ( ArchiverException e )
{
throw new MojoExecutionException( "ArchiverException while creating ." + classesJar + " file.", e );
}
catch ( IOException e )
{
throw new MojoExecutionException( "IOException while creating ." + classesJar + " file.", e );
}
}

/**
* @return
* @throws MojoExecutionException
*/
protected File createApkLibraryFile( File classesJar ) throws MojoExecutionException
{
final File apklibrary = new File( project.getBuild().getDirectory(),
project.getBuild().getFinalName() + "." + AAR );
Expand All @@ -152,11 +196,8 @@ protected File createApkLibraryFile() throws MojoExecutionException
jarArchiver.addFile( androidManifestFile, "AndroidManifest.xml" );
addDirectory( jarArchiver, assetsDirectory, "assets" );
addDirectory( jarArchiver, resourceDirectory, "res" );

File jarFile = new File( project.getBuild().getDirectory()
+ File.separator + project.getBuild().getFinalName() + ".jar" );
jarArchiver.addFile( jarFile, "classes.jar" );

jarArchiver.addFile( classesJar, "classes.jar" );

File[] overlayDirectories = getResourceOverlayDirectories();
for ( File resOverlayDir : overlayDirectories )
{
Expand Down Expand Up @@ -205,7 +246,7 @@ private void addNativeLibraries( final JarArchiver jarArchiver ) throws MojoExec
}
else
{
getLog().info( nativeLibrariesDirectory
getLog().info( nativeLibrariesDirectory
+ " does not exist, looking for libraries in target directory." );
// Add native libraries built and attached in this build
String[] ndkArchitectures = NativeHelper.getNdkArchitectures( ndkArchitecture,
Expand All @@ -215,7 +256,7 @@ private void addNativeLibraries( final JarArchiver jarArchiver ) throws MojoExec
{
final File ndkLibsDirectory = new File( ndkOutputDirectory, ndkArchitecture );
addSharedLibraries( jarArchiver, ndkLibsDirectory, ndkArchitecture );

// Add native library dependencies
// FIXME: Remove as causes duplicate libraries when building final APK if this set includes
// libraries from dependencies of the AAR
Expand Down Expand Up @@ -324,10 +365,10 @@ protected void addDirectory( JarArchiver jarArchiver, File directory, String pre
getLog().debug( "Added files from " + directory );
}
}

/**
* Adds all shared libraries (.so) to a {@link JarArchiver} under 'libs'.
*
*
* @param jarArchiver The jarArchiver to add files to
* @param directory The directory to scan for .so files
* @param ndkArchitecture The prefix for where in the jar the .so files will go.
Expand Down

0 comments on commit a480d33

Please sign in to comment.