如何在android gradle插件环境中支持osgi? [英] How to support osgi in android gradle plugin environment?

查看:186
本文介绍了如何在android gradle插件环境中支持osgi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用apk作为osgi包,然后将其加载到宿主应用程序中。



现在,它工作正常。但是我必须在将osgi包推送到手机之前手动执行以下操作:
$ b


  1. 使用WinRAR打开osgi bundle apk文件。

  2. 将构建的类(在extension-a\build\intermediates\classes\debug\com中)拖到包文件的根目录中。
  3. 拖动修改MANIFEST.MF(包括OSGI配置信息)到包文件的META-INFO / MANIFEST.MF。

  4. 最后签署包apk。

我想知道如何编写一些gradle脚本来自动执行这些操作?



我使用gradle 2.2。 1和'com.android.tools.build:gradle:1.2.3'。



ps:
我知道有一个gradle插件osgi可以为osgi bunlde生成MANIFEST.MF,但只适用于javagradle插件。
我不能在androidgradle插件中使用它。



我试过这种方法:

  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'

(e in zip。条目()){
if(!e.name.equalsIgnoreCase(toModify)){
zos.putNextEntry(e)
zos<< zip.getInputStream(e).bytes
} else {
zos.putNextEntry(new ZipEntry(toModify))
zos<<新文件(inputFile).bytes
}
zos.closeEntry()
}

zos.close()
zipIn.delete()
zipTemp.renameTo(zipIn)
}
}

但没有成功:


  1. 如果使用gradle clean build,它会报告out-file.apk找不到。

  2. 如果out-file.apk存在,它会报告异常:

    java.util.zip.ZipException:invalid entry compressed size(expected 400但得到了401字节)
    在java.util.zip.ZipOutputStream.closeEntry(未知源代码)


任何帮助将不胜感激!

解决方案

我找到了解决方法。

  buildscript {
存储库{
jcenter()
}
依赖关系{
classpath'com.android.tools.build:gradle:1.2.3'

//注意:不要在这里放置您的应用程序依赖项;它们属于单个模块build.gradle文件中的
//
}
}

allprojects {
存储库{
jcenter()



应用插件:'com.android.application'

import java.util.zip。*

android {
compileSdkVersion 21
buildToolsVersion21.1.2

defaultConfig {
minSdkVersion 19
targetSdkVersion 21
sourceCompatibility ='1.7'
targetCompatibility ='1.7'
versionCode 1
versionName1.0
}

signingConfigs {
release {
storeFile file (debug.keystore)//将debug.keystore移动到build.gradle目录
keyAliasandroiddebugkey
storePasswordandroid
keyPasswordandroid
}
}

buildTypes {
release {
minifyEnable d false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
signingConfig signingConfigs.release
}

android.applicationVariants。全部{变体 - >
variant.outputs [0] .packageApplication.doLast {
println在packageApplication之后添加osgi文件...

def manifestFile ='MANIFEST.MF'
def toModify ='META-INF / MANIFEST.MF'
def classesPath =build / intermediates / classes /+ variant.buildType.name +/ com
def zipFile = variant.outputs [0] .outputFile.getAbsolutePath()

// if(variant.buildType.name.equals(debug))
//在配置发布signingConfig之后,应该更改apk名称,所以删除这个if语句
zipFile = zipFile.replace(。apk,-unaligned.apk)

//发布应该更改3个职位:(如果没有发布signingConfig)
// 1.不需要用-unaligned.apk替换名称;
// 2.复制中间体/类/ release / com;
// 3.应该添加META-INF / MANIFEST.MF,而不是替换。
updateZipEntry(zipFile,toModify,manifestFile,classesPath)

signApkFile(zipFile)
// println project.buildDir
// println System.getenv('JAVA_HOME' )
}
}
}
}

依赖关系{
提供了fileTree(dir:'src / main / libs',包括: ['* .jar'])
提供'org.apache.felix:org.apache.felix.framework:5.0.0'
}

void updateZipEntry(String zipFile ,String zipEntry,String newFile,String classesPath){
def zin = new ZipFile(zipFile)
def tmp = File.createTempFile(temp _ $ {System.nanoTime()},'.zip' )
tmp.withOutputStream {os - >
def hasReplaced = false
def zos = new ZipOutputStream(os)
zin.entries()。each {entry - >
def isReplaced = entry.name == zipEntry
entry.setCompressedSize(-1);
zos.putNextEntry(被替换?new ZipEntry(zipEntry):entry)
zos<< (isReplaced?new File(newFile).bytes:zin.getInputStream(entry).bytes)
zos.closeEntry()
if(isReplaced)hasReplaced = true
}
if (!hasReplaced){//为发布版本添加META-INF / MANIFEST.MF。
zos.putNextEntry(new ZipEntry(zipEntry))
zos<<新文件(newFile).bytes
zos.closeEntry()
}
addDirToArchive(zos,new File(classesPath),com)
zos.close()

zin.close()
断言新文件(zipFile).delete()
tmp.renameTo(zipFile)
}

void addDirToArchive(ZipOutputStream zos,File srcDir,String dstDir){
//System.out.println(\"Adding directory:+ srcDir.getName()+ - >+ dstDir);

文件[] files = srcDir.listFiles();
for(int i = 0; i< files.length; i ++){
//如果文件是目录,则使用递归
if(files [i] .isDirectory()) {
addDirToArchive(zos,files [i],dstDir +/+ files [i] .getName());
继续;
}
try {
//System.out.println(\"Adding file:+ files [i] .getName());

//创建字节缓冲区
byte [] buffer = new byte [1024];
FileInputStream fis = new FileInputStream(files [i]);
zos.putNextEntry(new ZipEntry(dstDir +/+ files [i] .getName()));
int长度; ((length = fis.read(buffer))> 0){
zos.write(buffer,0,length);
while(
}
zos.closeEntry();

//关闭InputStream
fis.close();
} catch(IOException ioe){
System.out.println(IOException:+ ioe);



$ b void signApkFile(String zipFile){
def signApkCmdLine =java -Xmx512m -jar signapk.jar -w testkey。 x509.pem testkey.pk8+ zipFile ++ zipFile
def sout = new StringBuffer(),serr = new StringBuffer()
def proc = signApkCmdLine.execute()
proc.consumeProcessOutput (sout,serr)
proc.waitForOrKill(1000)
printlnsign apk:out> $ sout err> $ serr


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.

I tried this way :

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)
    }
}

But without success:

  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 400 but got 401 bytes) at java.util.zip.ZipOutputStream.closeEntry(Unknown Source)

Any help would be appreciated!

解决方案

I find a way to solve it.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

apply plugin: 'com.android.application'

import java.util.zip.*

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 21
        sourceCompatibility = '1.7'
        targetCompatibility = '1.7'
        versionCode 1
        versionName "1.0"
    }

    signingConfigs {
        release {
            storeFile file("debug.keystore") // should move debug.keystore to build.gradle directory
            keyAlias "androiddebugkey"
            storePassword "android"
            keyPassword "android"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release            
        }

        android.applicationVariants.all { variant ->
            variant.outputs[0].packageApplication.doLast {
                println "Add osgi files after packageApplication ..."

                def manifestFile = 'MANIFEST.MF'  
                def toModify = 'META-INF/MANIFEST.MF'
                def classesPath =  "build/intermediates/classes/"+variant.buildType.name+"/com"
                def zipFile = variant.outputs[0].outputFile.getAbsolutePath()

                //if (variant.buildType.name.equals("debug"))
                // After config release signingConfig, should change apk name, so remove this if statement
                zipFile = zipFile.replace(".apk", "-unaligned.apk")

                //release should change 3 positions: (If without release signingConfig)
                //  1. no need replace name with -unaligned.apk; 
                //  2. copy intermediates/classes/release/com; 
                //  3. should add META-INF/MANIFEST.MF, not replace.
                updateZipEntry(zipFile, toModify, manifestFile, classesPath)

                signApkFile(zipFile)
                //println project.buildDir
                //println System.getenv('JAVA_HOME')
            }
        }
    }
}

dependencies {
    provided fileTree(dir: 'src/main/libs', include: ['*.jar'])
    provided 'org.apache.felix:org.apache.felix.framework:5.0.0'
}

void updateZipEntry(String zipFile, String zipEntry, String newFile, String classesPath){
    def zin = new ZipFile(zipFile)
    def tmp = File.createTempFile("temp_${System.nanoTime()}", '.zip')
    tmp.withOutputStream { os ->
        def hasReplaced = false
        def zos = new ZipOutputStream(os)
        zin.entries().each { entry ->
            def isReplaced = entry.name == zipEntry
            entry.setCompressedSize(-1);
            zos.putNextEntry(isReplaced ? new ZipEntry(zipEntry) : entry)
            zos << (isReplaced ? new File(newFile).bytes : zin.getInputStream(entry).bytes )
            zos.closeEntry()
            if (isReplaced) hasReplaced = true
        }
        if (!hasReplaced) { // Add META-INF/MANIFEST.MF for release build.
            zos.putNextEntry(new ZipEntry(zipEntry))
            zos << new File(newFile).bytes
            zos.closeEntry()
        }
        addDirToArchive(zos, new File(classesPath), "com")
        zos.close()
    }
    zin.close()
    assert new File(zipFile).delete()
    tmp.renameTo(zipFile)
}

void addDirToArchive(ZipOutputStream zos, File srcDir, String dstDir) {
    //System.out.println("Adding directory: " + srcDir.getName() + " -> " + dstDir);

    File[] files = srcDir.listFiles();
    for (int i = 0; i < files.length; i++) {
        // if the file is directory, use recursion
        if (files[i].isDirectory()) {
            addDirToArchive(zos, files[i], dstDir + "/" + files[i].getName());
            continue;
        }
        try {
            //System.out.println("Adding file: " + files[i].getName());

            // create byte buffer
            byte[] buffer = new byte[1024];
            FileInputStream fis = new FileInputStream(files[i]);
            zos.putNextEntry(new ZipEntry(dstDir + "/" + files[i].getName()));
            int length;
            while ((length = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();

            // close the InputStream
            fis.close();
        } catch (IOException ioe) {
            System.out.println("IOException :" + ioe);
        }
    }
}

void signApkFile(String zipFile) {
    def signApkCmdLine = "java -Xmx512m -jar signapk.jar -w testkey.x509.pem testkey.pk8 "+zipFile+" "+zipFile
    def sout = new StringBuffer(), serr = new StringBuffer()
    def proc = signApkCmdLine.execute()
    proc.consumeProcessOutput(sout, serr)
    proc.waitForOrKill(1000)
    println "sign apk : out> $sout err> $serr"
}

这篇关于如何在android gradle插件环境中支持osgi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆