I change my ProGuard configuration depending upon the gradle build type. I do this so that I can magically disable certain code when creating a release build.
Example builds:
buildTypes {
debug {
minifyEnabled true
proguardFiles 'proguard-debug.cfg'
}
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
Android Studio is content to add the proguard.cfg file to my Project View, but not the proguard-debug.cfg file.
Example Project View from my project (it shows my proguard config and from a library module, but not from my debug config).
Structure of the directory:
project
-- .gradle
-- .idea
-- app (module)
-- -- src
-- -- Other files / folders
-- -- proguard-debug.cfg
-- -- proguard.cfg
-- -- proguard-release.cfg
-- Other files / folders
-- build.gradle
-- settings.gradle
Related
Does source code are included with the APK taken from Android studio?
if yes how can we remove it so that users can't access the source code anytime
Source code is not added into APK, but bytecode of APK can be decoded into java code.
You can use proguard to shrink, obfuscate, and optimize your app and it's hard to read when decoded.
Here are example of using proguard (put it into build.gradle)
android {
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
I use gradle's experimental plugin in order to add some NDK code.
before usage of NDK support my buildTypes section was next
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
as you can see here I use optimized options of ProGuard.
And when I started to use gradle's experimental plugin I changed those lines to
buildTypes {
release {
minifyEnabled true
proguardFiles.add(file('proguard-rules.pro'))
}
}
The question is next: is it ok to use such options when I want to use 'proguard-android-optimize.txt' file?
If not then how I could define that I want to use 'proguard-android-optimize.txt' file with gradle's experimental plugin?
I came across this issue recently, and found that Gradle Experimental doesn't appear to include any version of ProGuard config by default, and there's no "getDefaultProguardFile()" method to call.
I've had to resort to adding the 'proguard-android-optimize.txt' file to my project for now (and referencing it in my build.gradle), as my project won't build without at least the basic rules being specified.
I'm using Gradle Experimental 0.7.2.
I am trying to import standout library which is a github project as library in my project.What i tried is below:
created a folder in my root project directory named 'libs'
copied the complete folder 'library' of the Standout project on git into 'libs'
renamed the folder 'library' that i just copied to e.g. "standout" (just to avoid confusion)
now added this library in my settings.gradle with the following command:
include ':libs:standout'
going to my build.gradle file of my AppProject and added the following line to my 'dependencies':
compile project(':libs:standout')
but i got error and then added like this
compile fileTree(dir: 'libs', include: ['standout'])
gradle sync was successful but there is red mark on the src files of the library project..I couldn't access the library src files from MainActivity of my Project.As the github project wasn't the android studio project do i need to do anything extra for that?
I have tried below way suggested by Wesley:
Download or clone the git repository, copy the library folder into
your project root folder.
Then in your project settings.gradle file, add a ':library' at the
end of the include line, like this: include ':app', ':library', then
gradle sync your whole project.
At the end, in your app module or you own application module, edit
the build.gradle file, add a line in your dependencies:
compile project(':library')
BTW, you can change the folder name ("library" in this case) to any
name you want (like "standout-library" if you want) when copy it into
you project's root folder, then, when you edit the gradle files, just
use the changed name.
But got the below error:
Error:Configuration with name 'default' not found.
My build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.mapandmap.standout"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':standout')
compile 'com.android.support:appcompat-v7:22.1.1'
}
My settings.gradle:
include ':app',':standout'
screenshot of my Project Tree:
You need to resolve R in each file of "standout" library which is showing as red now.
You can simply add by doing this. Go to Android Studio File->Project Structure-> + , You will get four options to add a new module. From there choose "Import Existing Project"->Next-> browse to the directory you have downloaded library and choose "library" directory and then "Finish".Android Studio will take some time to sync library to your project. Finally add dependency in settings. I just tested by adding this library in a project and its working perfectly.
You could move the library to your main "app" project but its better to add as dependency.
I followed the answer of sbr1308 but I have to do some more things.What you have to do:
Go to Android Studio File->Project Structure-> + , You will get four options to add a new module. From there choose "Import Existing Project"->Next-> browse to the directory you have downloaded library and choose "library" directory and then "Finish"
If there is any error in the classes of the library then Go to File->Invalidate Caches/Restart->Invalidate and Restart.It will take time to indexing.
You have to add the dependency in the app's build.gradle
compile project(':standOut') and then sync.
I imported several eclipse projects to Android Studio (v1.1).
In the original Eclipse environment, they use Proguard for release mode.
In the Android Studio environment, this was translated to the following in the build.gradle script (by the import, not by me):
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
I understand that this means that "in release build, enable Proguard's minify, using proguard.cfg".
The problem, however, is that minify seems to be happening in non-release build (i.e. debug) as well!
How is this possible?
What is the default for minifyEnabled for debug build?
UPDATE 1: Thanks to the answer below, I now know that the default is false. Which means something else is building the various modules minified in debug build.
I am posting the entire build.gradle for one of the modules that get minified in debug build:
apply plugin: 'com.android.library'
android {
compileSdkVersion 8
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 8
targetSdkVersion 8
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard.cfg'
}
}
}
The entire build.gradle for the project itself (i.e. top level) is:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
I cannot spot here anything that could explain enforcing minify on a debug build.
UPDATE 2: Suspecting a mismatch between app's build (debug) and the modules on which it depends (release?), I also checked the Build Variant view on the the left panel. The all show debug unequivocally.
UPDATE 3: It appears that I hit a bug/limitation in Android-Gradle?
I truly need all modules built in debug mode when the app is built in debug mode.
Any ideas how I can solve this problem?
The default value for minifyEnabled is false for all build types, as #laalto answered.
However, currently (as of 2015-04-24), this is not true for multi-module projects, in which some modules (app included) are dependent on other modules. This is due to bug #52962 that causes build types to not propagate to libraries -- they're always built as RELEASE.
Suggestions to work around this bug or notifications of its fix are most welcome.
What is the default for minifyEnabled for debug build?
The default value for minifyEnabled is false for all build types. Reference.
The problem, however, is that minify seems to be happening in non-release build (i.e. debug) as well!
How is this possible?
Your debug build gets proguard treatment possibly by some other definition somewhere, or an external build script you're using.
In your updated question, you have a library project and an app project that uses a minified library even for debug builds. That's a "feature". For a solution, consider the following also mentioned in the issue report:
Build all variants of the library project by adding the following to its build.gradle:
android {
publishNonDefault true
}
In the app project, choose the build type specific dependency with
dependencies {
releaseCompile project(path: ':theotherproject', configuration: 'release')
debugCompile project(path: ':theotherproject', configuration: 'debug')
}
I am using Android Studio 1.1.0 to build an APK for internal distribution in my company using AirWatch.
My understanding of how to build an APK is to go to the "Build==>Make Project". However, when I do that AS does not generate an APK. However, if I go to "Run==>Run App" then I do get an APK.
I fear I have something misconfigured in AS, as this is my first project.
Bryan
There are two.
Here is the first.
apply plugin: 'com.android.application'
android {
signingConfigs {
config {
keyAlias 'XXXXXXXXXXXXXXX'
keyPassword 'password'
storeFile file('PATH')
storePassword 'XXXXXX'
}
}
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.ACME.application"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug{
debuggable true
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/commons-lang-2.3.jar')
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
compile files('src/main/assets/library-1.0.10.jar')
}
Here is the second.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Seems like you are getting correct behavior from Android Studio.
You are correct that to build an APK, you can go to the "Build==>Make Project". When you "build" the project, you are just creating the APK, not installing it on any device or emulator. So you won't see your app 'running' anywhere after this step. Selecting this item in the GUI is the same as running the command line gradle build.
The APK is placed in this directory:
<Your root project folder>/<app name>/build/outputs/apk
You can then install this APK on a device using the command line, or by dragging and dropping the file onto an emulator.
When you go to "Run==>Run App" then you also get an APK. But you get even more! This apk is not only built, it is installed onto your connected device / emulator automatically for you. Selecting this item in the GUI is the same as running the two commands gradle build and adb install.