问题
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
回答1:
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 errorNo 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
来源:https://stackoverflow.com/questions/21691678/android-with-gradle-and-proguard