How to support osgi in android gradle plugin enviroment?

I want to use apk as an osgi bundle, and then load it in a host application.

Now, it works ok. But I must manually do these things before push the osgi bundle to phone:

  1. Open osgi bundle apk file with WinRAR.
  2. Drag the built classes(in extension-a\build\intermediates\classes\debug\com) to the bundle file’s root directory.
  3. Drag the modified MANIFEST.MF (including OSGI config info) to the bundle file’s META-INFO/MANIFEST.MF .
  4. Sign the bundle apk at last.

I want to know how to write some gradle scripts to do these things automatically?

I use gradle 2.2.1, and ‘com.android.tools.build:gradle:1.2.3’.

ps:
I know there is a gradle plugin “osgi” can generate the MANIFEST.MF for osgi bunlde, but it only works with “java” gradle plugin.
I can’t use it with “android” gradle plugin.

Thanks!

I try to do it this way, but it not work:

	android.applicationVariants.all { variant ->
		variant.outputs[0].packageApplication.doLast {
			String zipFileName = variant.outputs[0].outputFile.name
			String inputFile = 'MANIFEST.MF'  
			
			def zipIn = variant.outputs[0].outputFile
			def zip = new ZipFile(zipIn.getAbsolutePath())
			def zipTemp = new File(zipFileName + "_temp") 
			zipTemp.deleteOnExit()
			def zos = new ZipOutputStream(new FileOutputStream(zipTemp))
			def toModify = 'META-INFO/MANIFEST.MF'

			for(e in zip.entries()) {
				if(!e.name.equalsIgnoreCase(toModify)) {
					zos.putNextEntry(e)
					zos << zip.getInputStream(e).bytes
				} else {
					zos.putNextEntry(new ZipEntry(toModify))
					zos << new File(inputFile).bytes
				}
				zos.closeEntry()
			}

			zos.close()
			zipIn.delete()
			zipTemp.renameTo(zipIn)
		}
	}
  1. If use “gradle clean build”, it report “out-file.apk” not found.
  2. If the out-file.apk exists, it report exception:
    java.util.zip.ZipException: invalid entry compressed size (expected 695 but got 693 bytes)
    at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)