Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multidex an Proguard fixes #509

Merged
merged 2 commits into from
Nov 20, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.jayway.maven.plugins.android.config.ConfigPojo;
import com.jayway.maven.plugins.android.config.PullParameter;
import com.jayway.maven.plugins.android.configuration.Proguard;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -19,6 +21,8 @@
import org.codehaus.plexus.interpolation.os.Os;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -193,15 +197,15 @@ public class ProguardMojo extends AbstractAndroidMojo

@PullParameter( defaultValue = "true" )
private Boolean parsedFilterManifest;

/**
* You can specify a custom filter which will be used to filter out unnecessary files from ProGuard input.
*
*
* @see http://proguard.sourceforge.net/manual/usage.html#filefilters
*/
@Parameter( property = "android.proguard.customfilter" )
private String proguardCustomFilter;

@PullParameter
private String parsedCustomFilter;

Expand Down Expand Up @@ -277,28 +281,26 @@ public ProGuardInput( String path, Collection< String > excludedFilter )
this.excludedFilter = excludedFilter;
}

public String toCommandLine()
public String toPath()
{
if ( excludedFilter != null && !excludedFilter.isEmpty() )
{
String startQuotes, middleQuote, endQuote;
String middleQuote, endQuote;

if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
{
startQuotes = "'\"";
middleQuote = "\"(";
endQuote = ")'";
middleQuote = "(";
endQuote = ")";
}
else
{
startQuotes = "\"'";
middleQuote = "'(";
endQuote = ")\"";
middleQuote = "(";
endQuote = ")";
}

StringBuilder sb = new StringBuilder( startQuotes );
sb.append( path );
sb.append( middleQuote );
StringBuilder sb = new StringBuilder();
sb.append( path );
sb.append( middleQuote );
for ( Iterator< String > it = excludedFilter.iterator(); it.hasNext(); )
{
sb.append( '!' ).append( it.next() );
Expand All @@ -312,20 +314,7 @@ public String toCommandLine()
}
else
{
String startQuotes, endQuote;

if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
{
startQuotes = "'\"";
endQuote = "\"'";
}
else
{
startQuotes = "\"'";
endQuote = "'\"";
}

return startQuotes + path + endQuote;
return path;
}
}

Expand Down Expand Up @@ -388,50 +377,73 @@ private void executeProguard() throws MojoExecutionException
commands.add( "-jar" );
commands.add( parsedProguardJarPath );

commands.add( "@\"" + parsedConfig + "\"" );
List<String> proguardCommands = new ArrayList<String>();

proguardCommands.add( "@" + parsedConfig + "" );

for ( String config : parsedConfigs )
{
commands.add( "@\"" + config + "\"" );
proguardCommands.add( "@" + config );
}

if ( proguardFile != null )
{
commands.add( "@\"" + proguardFile.getAbsolutePath() + "\"" );
proguardCommands.add( "@" + proguardFile.getAbsolutePath() );
}

collectInputFiles( commands );
collectInputFiles( proguardCommands );

commands.add( "-outjars" );
commands.add( "'\"" + obfuscatedJar + "\"'" );
proguardCommands.add( "-outjars" );
proguardCommands.add( obfuscatedJar );

commands.add( "-dump" );
commands.add( "'\"" + proguardDir + File.separator + "dump.txt\"'" );
commands.add( "-printseeds" );
commands.add( "'\"" + proguardDir + File.separator + "seeds.txt\"'" );
commands.add( "-printusage" );
commands.add( "'\"" + proguardDir + File.separator + "usage.txt\"'" );
proguardCommands.add( "-dump" );
proguardCommands.add( proguardDir + File.separator + "dump.txt" );
proguardCommands.add( "-printseeds" );
proguardCommands.add( proguardDir + File.separator + "seeds.txt" );
proguardCommands.add( "-printusage" );
proguardCommands.add( proguardDir + File.separator + "usage.txt" );

File mapFile = new File( proguardDir, "mapping.txt" );

commands.add( "-printmapping" );
commands.add( "'\"" + mapFile + "\"'" );
proguardCommands.add( "-printmapping" );
proguardCommands.add( mapFile.toString() );

commands.addAll( Arrays.asList( parsedOptions ) );
proguardCommands.addAll( Arrays.asList( parsedOptions ) );

final String javaExecutable = getJavaExecutable().getAbsolutePath();

getLog().debug( javaExecutable + " " + commands.toString() );
getLog().debug( javaExecutable + " " + commands.toString() + proguardCommands.toString() );

FileOutputStream tempConfigFileOutputStream = null;
try
{
File tempConfigFile = new File ( proguardDir , "temp_config.cfg" );

StringBuilder commandStringBuilder = new StringBuilder();
for ( String command : proguardCommands )
{
commandStringBuilder.append( command );
commandStringBuilder.append( SystemUtils.LINE_SEPARATOR );
}
tempConfigFileOutputStream = new FileOutputStream( tempConfigFile );
IOUtils.write( commandStringBuilder, tempConfigFileOutputStream );

executor.setCaptureStdOut( true );
commands.add( "@\"" + tempConfigFile.getAbsolutePath() + "\"" );
executor.executeCommand( javaExecutable, commands, project.getBasedir(), false );
}
catch ( ExecutionException e )
{
throw new MojoExecutionException( "", e );
}
catch ( IOException e )
{
throw new MojoExecutionException( "Error writing proguard commands to temporary file", e );
}
finally
{
IOUtils.closeQuietly( tempConfigFileOutputStream );
}

if ( parsedAttachMap )
{
Expand Down Expand Up @@ -472,15 +484,15 @@ private void collectInputFiles( List< String > commands ) throws MojoExecutionEx
{
getLog().debug( "Added injar : " + injar );
commands.add( "-injars" );
commands.add( injar.toCommandLine() );
commands.add( injar.toPath() );
}

final List< ProGuardInput > libraryJars = getLibraryInputFiles();
for ( final ProGuardInput libraryjar : libraryJars )
{
getLog().debug( "Added libraryJar : " + libraryjar );
commands.add( "-libraryjars" );
commands.add( libraryjar.toCommandLine() );
commands.add( libraryjar.toPath() );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@
public class DexMojo extends AbstractAndroidMojo
{

private static final String DEX = ".dex";
private static final String CLASSES = "classes";
/**
* Configuration for the dex command execution. It can be configured in the plugin configuration like so
*
Expand Down Expand Up @@ -207,24 +205,6 @@ public void execute() throws MojoExecutionException, MojoFailureException
if ( generateApk )
{
runDex( executor, outputFile );

if ( parsedMultiDex )
{

File assets = new File( targetDirectory,
"generated-sources" + File.separator + "combined-assets" );

if ( !assets.exists() && !assets.mkdirs() )
{
throw new IllegalStateException( "Unable to create combined-assets directory" );
}
int i = 2;
while ( copyAdditionalDex( outputFile, i, assets ) )
{
i++;
}

}
}

if ( attachJar )
Expand All @@ -242,27 +222,6 @@ public void execute() throws MojoExecutionException, MojoFailureException
}
}

private boolean copyAdditionalDex( File outputFile, int dexIndex, File assets ) throws MojoExecutionException
{
File secondDexFile = new File( outputFile, CLASSES + dexIndex + DEX );
if ( secondDexFile.exists() )
{
File copiedSecondDexFile = new File( assets, CLASSES + dexIndex + DEX );

try
{
FileUtils.moveFile( secondDexFile, copiedSecondDexFile );
}
catch ( IOException e )
{
throw new MojoExecutionException( "IOException while moving classes" + dexIndex + ".dex to "
+ "combined-assets directory", e );
}
return true;
}
return false;
}

/**
* Gets the input files for dex. This is a combination of directories and jar files.
*
Expand Down
Loading