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

also extract duplicates from output folder #705

Merged
merged 1 commit into from
Jan 27, 2016
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 @@ -21,6 +21,7 @@
import com.android.sdklib.build.DuplicateFileException;
import com.android.sdklib.build.SealedApkException;
import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
import com.google.common.io.Files;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not using the normal commons-io but the google fork instead?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... sorry.. looks like its not a fork but a powerful replacement. I guess thats fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can change it. do you mean apache commons-io or is it also in the jdk?
On Jan 19, 2016 5:28 PM, "Manfred Moser" [email protected] wrote:

In
src/main/java/com/simpligility/maven/plugins/android/phase09package/ApkMojo.java
#705 (comment)
:

@@ -21,6 +21,7 @@
import com.android.sdklib.build.DuplicateFileException;
import com.android.sdklib.build.SealedApkException;
import com.simpligility.maven.plugins.android.AbstractAndroidMojo;
+import com.google.common.io.Files;

Why is this not using the normal commons-io but the google fork instead?


Reply to this email directly or view it on GitHub
https://github.com/simpligility/android-maven-plugin/pull/705/files#r50193127
.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant apache commons since I think its already used in the project... just to reduce different tools used. But have a look first .. if its too much effort .. using the google one is fine as well. Btw... is it part of guava?

import com.simpligility.maven.plugins.android.AndroidNdk;
import com.simpligility.maven.plugins.android.AndroidSigner;
import com.simpligility.maven.plugins.android.IncludeExcludeSet;
Expand Down Expand Up @@ -51,6 +52,7 @@

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
Expand Down Expand Up @@ -566,7 +568,27 @@ private void computeDuplicateFiles( File jar ) throws IOException
}
}

private void extractDuplicateFiles( List<File> jarFiles ) throws IOException
private void computeDuplicateFilesInSource( File folder )
{
String rPath = folder.getAbsolutePath();
for ( File file : Files.fileTreeTraverser().breadthFirstTraversal( folder ).toList() )
{
String lPath = file.getAbsolutePath();
if ( lPath.equals( rPath ) )
{
continue; //skip the root
}
lPath = lPath.substring( rPath.length() + 1 ); //strip root folder to make relative path

if ( jars.get( lPath ) == null )
{
jars.put( lPath, new ArrayList<File>() );
}
jars.get( lPath ).add( folder );
}
}

private void extractDuplicateFiles( List<File> jarFiles, Collection<File> sourceFolders ) throws IOException
{
getLog().debug( "Extracting duplicates" );
List<String> duplicates = new ArrayList<String>();
Expand Down Expand Up @@ -600,12 +622,20 @@ private void extractDuplicateFiles( List<File> jarFiles ) throws IOException

for ( File file : jarToModify )
{
final File newJar = removeDuplicatesFromJar( file, duplicates, duplicatesAdded, zos );
getLog().debug( "Removed duplicates from " + newJar );
if ( newJar != null )
final int index = jarFiles.indexOf( file );
if ( index != -1 )
{
final int index = jarFiles.indexOf( file );
jarFiles.set( index, newJar );
final File newJar = removeDuplicatesFromJar( file, duplicates, duplicatesAdded, zos, index );
getLog().debug( "Removed duplicates from " + newJar );
if ( newJar != null )
{
jarFiles.set( index, newJar );
}
}
else
{
removeDuplicatesFromFolder( file, file, duplicates, duplicatesAdded, zos );
getLog().debug( "Removed duplicates from " + file );
}
}
//add transformed resources to duplicate-resources.jar
Expand Down Expand Up @@ -666,12 +696,17 @@ private void doAPKWithAPKBuilder( File outputFile, File dexFile, File zipArchive
jarFiles.add( artifact.getFile() );
}

for ( File src : sourceFolders )
{
computeDuplicateFilesInSource( src );
}

// Check duplicates.
if ( extractDuplicates )
{
try
{
extractDuplicateFiles( jarFiles );
extractDuplicateFiles( jarFiles, sourceFolders );
}
catch ( IOException e )
{
Expand Down Expand Up @@ -799,12 +834,14 @@ private String getNextDexFileName( int dexNumber )
}

private File removeDuplicatesFromJar( File in, List<String> duplicates,
Set<String> duplicatesAdded, ZipOutputStream duplicateZos )
Set<String> duplicatesAdded, ZipOutputStream duplicateZos, int num )
{
String target = targetDirectory.getAbsolutePath();
File tmp = new File( target, "unpacked-embedded-jars" );
tmp.mkdirs();
File out = new File( tmp, in.getName() );
String jarName = String.format( "%s-%d.%s",
Files.getNameWithoutExtension( in.getName() ), num, Files.getFileExtension( in.getName() ) );
File out = new File( tmp, jarName );

if ( out.exists() )
{
Expand Down Expand Up @@ -910,6 +947,66 @@ private File removeDuplicatesFromJar( File in, List<String> duplicates,
return out;
}

private void removeDuplicatesFromFolder( File root, File in, List<String> duplicates,
Set<String> duplicatesAdded, ZipOutputStream duplicateZos )
{
String rPath = root.getAbsolutePath();
try
{
for ( File f : in.listFiles() )
{
if ( f.isDirectory() )
{
removeDuplicatesFromFolder( root, f, duplicates, duplicatesAdded, duplicateZos );
}
else
{
String lName = f.getAbsolutePath();
lName = lName.substring( rPath.length() + 1 ); //make relative path
if ( duplicates.contains( lName ) )
{
boolean resourceTransformed = false;
if ( transformers != null )
{
for ( ResourceTransformer transformer : transformers )
{
if ( transformer.canTransformResource( lName ) )
{
getLog().info( "Transforming " + lName
+ " using " + transformer.getClass().getName() );
InputStream currIn = new FileInputStream( f );
transformer.processResource( lName, currIn, null );
currIn.close();
resourceTransformed = true;
break;
}
}
}
//if not handled by transformer, add (once) to duplicates jar
if ( !resourceTransformed )
{
if ( !duplicatesAdded.contains( lName ) )
{
duplicatesAdded.add( lName );
ZipEntry entry = new ZipEntry( lName );
duplicateZos.putNextEntry( entry );
InputStream currIn = new FileInputStream( f );
copyStreamWithoutClosing( currIn, duplicateZos );
currIn.close();
duplicateZos.closeEntry();
}
}
f.delete();
}
}
}
}
catch ( IOException e )
{
getLog().error( "Cannot removing duplicates : " + e.getMessage() );
}
}

/**
* Copies an input stream into an output stream but does not close the streams.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
/*
* Copyright (C) 2014 simpligility technologies inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.maven.plugins.android.phase09package;
package com.simpligility.maven.plugins.android.phase09package;

import io.takari.maven.testing.TestResources;
import io.takari.maven.testing.executor.MavenExecutionResult;
Expand Down Expand Up @@ -58,15 +43,17 @@ public void buildDeployAndRun() throws Exception {
result.assertErrorFreeLog();
result.assertLogText( "Duplicate file resourceA" );
result.assertLogText( "Duplicate file resourceB" );
result.assertLogText( "Duplicate file resourceC" );
File apk = new File(result.getBasedir().getAbsolutePath()+"/duplicates-app/target", "duplicates-app.apk");
assertNotNull("APK Not Null", apk);
ZipFile apkFile = new ZipFile(apk);
assertNotNull(apkFile.getEntry("resourceA"));
assertNotNull(apkFile.getEntry("resourceB"));
assertNotNull(apkFile.getEntry("resourceC"));

//test services
assertEntryContents(apkFile, "META-INF/services/com.jayway.maven.plugins.android.TestInterface",
"ImplementationA\nImplementationB\nImplementationC");
"ImplementationA\nImplementationB\nImplementationC\nImplementationApp");

//test xpath xml
assertEntryContents( apkFile, "META-INF/kmodule.xml", expectedKmodule );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ImplementationApp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test content