【问题标题】:Android with Gradle and Proguard带有 Gradle 和 Proguard 的 Android
【发布时间】:2014-03-08 15:19:26
【问题描述】:

我刚刚使用 Gradle 创建了一个新的 Android 库项目,并希望通过 Proguard 优化和混淆代码。

这是 build.gradle 文件的 android 部分:

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

当我从终端运行 gradle build 命令时,它在 :library:proguardRelease 处失败,并显示以下消息:

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

有人知道这是为什么吗?

Gradle 1.10 JVM 1.6.0_65 Progruard 4.10

【问题讨论】:

    标签: android gradle proguard android-library


    【解决方案1】:

    执行gradlew assembleRelease 时,以下build.gradle 文件适用于Proguard。

    请注意,它被设置为从配置文件中读取发布密钥库信息(并且我在项目中包含了调试密钥证书,因为在调试模式下 Maps API v2 需要它),以及来自命令行的密码:

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

    以下是设置配置文件以读取密钥库信息的说明:

    要构建发布版本,您需要创建一个指向“secure.properties”文件的“gradle.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 路径分隔符 \,您将收到错误 No value has been specified for property '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

    【讨论】:

      猜你喜欢
      • 2014-09-24
      • 2013-09-20
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多