Android编写摇篮和Proguard的 [英] Android with Gradle and Proguard

查看:1082
本文介绍了Android编写摇篮和Proguard的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建使用摇篮一个新的Andr​​oid库项目,并希望code进行优化,并通过Proguard的混淆使用。

下面是Android部分在 build.gradle 文件:

 安卓{
    compileSdkVersion 19
    buildToolsVersion19.0.1

    defaultConfig {
        的minSdkVersion 14
        targetSdkVersion 19
        版本code 1
        VERSIONNAME1.0
    }
    推出 {
        runProguard真
        proguardFiles getDefaultProguardFile('ProGuard的-的Andr​​oid optimize.txt'),'ProGuard的-rules.txt
    }
}
 

当我运行的摇篮打造从终端命令失败在:库:proguardRelease 与消息:

  *出了什么问题:
执行失败的任务:图书馆:proguardRelease。
> java.io.IOException异常:输出罐子是空的。你指定适当的-keep选项?
 

有谁知道这是为什么?

摇篮1.10 JVM 1.6.0_65 Progruard 4.10

解决方案

下面的 build.gradle 在执行时,文件对我的作品使用ProGuard gradlew assembleRelease

请注意它被设置读取配置文件释放密钥存储的信息(我已经包含在项目的调试关键证书,因为它需要的地图在调试模式下API第2版),以及密码的命令行

 应用插件:'机器人'

安卓{
    compileSdkVersion 19
    buildToolsVersion19.0.0

    defaultConfig {
        的minSdkVersion 8
        targetSdkVersion 19
    }

    如果(project.hasProperty(secure.properties)
            &功放;&安培;新的文件(project.property(secure.properties))。存在()){

        属性道具=新特性()
        props.load(新的FileInputStream(文件(project.property(secure.properties))))

        signingConfigs {
            调试{
                storeFile文件(gpstest.debug.keystore)
            }

            推出 {
                storeFile文件(道具['key.store'])
                keyAlias​​道具['key.alias']
                storePasswordaskmelater
                keyPasswordaskmelater
            }
        }
    } 其他 {
        signingConfigs {
            调试{
                storeFile文件(gpstest.debug.keystore)
            }

            推出 {
                // 这里没有什么
            }
        }
    }

    buildTypes {
        推出 {
            runProguard真
            proguardFileproguard.cfg
            signingConfig signingConfigs.release
        }
    }
}

任务askForPasswords<< {
    //必须创建字符串,因为System.readPassword()返回的char []
    //(和分配,以下无提示失败)
    高清storePw =新的String(System.console()readPassword(\ nKeystore密码。))
    高清keyPw =新的String(System.console()readPassword(密钥密码。))

    android.signingConfigs.release.storePassword = storePw
    android.signingConfigs.release.keyPassword = keyPw
}

tasks.whenTaskAdded {th​​eTask  - >
    如果(theTask.name.equals(packageRelease)){
        theTask.dependsOnaskForPasswords
    }
}

依赖{
    编制项目(:ShowcaseViewLibrary)
    编译com.google.android.gms:播放服务:65年2月3日
    编译com.actionbarsherlock:actionbarsherlock:4.4.0@aar
    编译org.jraf:Android的开关反向移植:1.2
    编译com.google.maps.android:android-maps-utils:0.2.1
}
 

下面是建立配置文件读取密钥库信息说明:

  

要建立一个发布版本,你需要创建一个gradle.properties文件指向一个secure.properties文件和secure.properties文件指向您的密钥库和别名。该 gradlew assembleRelease 命令会提示输入密钥存储密码。

     

在gradle.properties文件位于GPSTest目录和具有内容:

  secure.properties =< full_path_to_secure_properties_file>
 

  

在secure.properties文件(在gradle.properties指定的位置)的内容:

  key.store =< full_path_to_keystore_file>

key.alias =< key_alias_name>
 

  

请注意,在这些文件的路径总是使用UNIX路径分隔符 / ,即使是在Windows上。如果你使用Windows路径分隔符 \ ,你会得到错误无值已被指定为财产signingConfig.keyAlias​​。

这里的路径,如果你想克隆并测试它自己Github上的文件/项目: https://github.com/barbeau/gpstest/blob/master/GPSTest /build.gradle

proguard.cfg 也是在GPSTest子目录(同一目录 build.gradle ): https://github.com/barbeau/gpstest/blob/master/GPSTest /proguard.cfg

I'm just created a new Android Library project using Gradle and would like the code to be optimized and obfuscated with via Proguard.

Here is the android portion the build.gradle file:

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
    }
}

When I run the gradle build command from terminal it fails at :library:proguardRelease with the message:

* What went wrong:
Execution failed for task ':library:proguardRelease'.
> java.io.IOException: The output jar is empty. Did you specify the proper '-keep' options?

Does anybody know why this is?

Gradle 1.10 JVM 1.6.0_65 Progruard 4.10

解决方案

The following build.gradle file works for me with Proguard when executing gradlew assembleRelease.

Note it is set up to read the release keystore info from a config file (and I've included the debug key cert in the project, since its needed for Maps API v2 in debug mode), and passwords from the command line:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }

    if (project.hasProperty("secure.properties")
            && new File(project.property("secure.properties")).exists()) {

        Properties props = new Properties()
        props.load(new FileInputStream(file(project.property("secure.properties"))))

        signingConfigs {
            debug {
                storeFile file("gpstest.debug.keystore")
            }

            release {
                storeFile file(props['key.store'])
                keyAlias props['key.alias']
                storePassword "askmelater"
                keyPassword "askmelater"
            }
        }
    } else {
        signingConfigs {
            debug {
                storeFile file("gpstest.debug.keystore")
            }

            release {
                // Nothing here
            }
        }
    }

    buildTypes {
        release {
            runProguard true
            proguardFile 'proguard.cfg'
            signingConfig signingConfigs.release
        }
    } 
}

task askForPasswords << {
    // Must create String because System.readPassword() returns char[]
    // (and assigning that below fails silently)
    def storePw = new String(System.console().readPassword("\nKeystore password: "))
    def keyPw = new String(System.console().readPassword("Key password: "))

    android.signingConfigs.release.storePassword = storePw
    android.signingConfigs.release.keyPassword = keyPw
}

tasks.whenTaskAdded { theTask ->
    if (theTask.name.equals("packageRelease")) {
        theTask.dependsOn "askForPasswords"
    }
}

dependencies {
    compile project(':ShowcaseViewLibrary')
    compile 'com.google.android.gms:play-services:3.2.65'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
    compile 'org.jraf:android-switch-backport:1.2'
    compile 'com.google.maps.android:android-maps-utils:0.2.1'
}

Here are the instructions to set up the config files to read keystore info:

To build a release build, you need to create a "gradle.properties" file that points to a "secure.properties" file, and a "secure.properties" file that points to your keystore and alias. The gradlew assembleRelease command will prompt for your keystore passphrase.

The "gradle.properties" file is located in the GPSTest directory and has the contents:

secure.properties=<full_path_to_secure_properties_file>

The "secure.properties" file (in the location specified in gradle.properties) has the contents:

key.store=<full_path_to_keystore_file>

key.alias=<key_alias_name>

Note that the paths in these files always use the Unix path separator /, even on Windows. If you use the Windows path separator \ you will get the error No value has been specified for property 'signingConfig.keyAlias'.

Here's the path to the file/project on Github if you want to clone and test it yourself: https://github.com/barbeau/gpstest/blob/master/GPSTest/build.gradle

proguard.cfg is also in the GPSTest subdirectory (same directory as build.gradle): https://github.com/barbeau/gpstest/blob/master/GPSTest/proguard.cfg

这篇关于Android编写摇篮和Proguard的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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