Gradle: one build type based on another - android-studio

I have 4 build types in Android Studio:
release
debug
kindle
kindle_debug
How is it possible to set the both kindle tasks to use the same data from release/debug and just changing some properties?

You can use initWith to inherit from another BuildType like in this example:
android {
[...]
buildTypes {
debug {
debuggable true
buildConfigField "boolean", "IS_V2", "false"
}
debugV2 {
initWith debug
buildConfigField "boolean", "IS_V2", "true"
applicationIdSuffix ".v2"
}
}
}
}
Then select debugV2 from Build Variants in Android Studio.
See also the docs here: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Types

Related

Android Studio build variant problems

I am having difficulty getting Android Studio to build the right build variant — or even to let me select a build variant at all, sometimes.
Basically I have two different versions of my project a free and a "full" version. Package ids are "com.mycompany.myproj" and "com.mycompany.myprojfree".
Once I've specified "myproj" and "myprojfree" flavors and "release" and "debug" buildtypes, Android Studio produces four variants in the list: myprojDebug, myprojfreeDebug, myprojfreeRelease, and myprojRelease.
Problem is, selecting one of these doesn't reliably select the variant for building, debugging, etc. For instance, I'll select myprojDebug, hit Debug, and myprojfreeDebug will build (as can be seen in the console), and the free version will open on the attached device.
Moreover, sometimes I can't even select one or more of the build variants in the build variant pane. I can click on it, but it doesn't change. But sometimes if I change it to something else first it'll let me go back and change the non-changing one.
I've seen posts referring to similar-sounding problems and have followed all the suggestions — cleaning, rebuilding, deleting .idea, deleting the build folder, Invalidate Caches/Restart, deleting app.iml, etc. — all to no avail.
It may be worth noting that all of this worked fine until yesterday, when I updated from Android Studio 3.1 to 3.4.1.
Here's a simplified version of my app build.gradle:
apply plugin: 'com.android.application'
android {
defaultConfig {
versionCode ...
multiDexEnabled true
vectorDrawables {
useSupportLibrary true
}
minSdkVersion 15
targetSdkVersion 28
}
compileSdkVersion 28
signingConfigs {
myproj {
keyAlias ...
keyPassword ...
storeFile file('...')
storePassword ...
}
myprojfree {
keyAlias ...
keyPassword ...
storeFile file('...')
storePassword ...
}
}
flavorDimensions "tier"
productFlavors {
myproj {
signingConfig signingConfigs.myproj
applicationId 'com.mycompany.myproj'
}
myprojfree {
signingConfig signingConfigs.myprojfree
applicationId 'com.mycompany.myprojfree'
}
}
buildTypes {
release {
debuggable false
buildConfigField "Boolean", "MY_DEBUG_MODE", "false"
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
debug {
debuggable true
buildConfigField "Boolean", "MY_DEBUG_MODE", "true"
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
}
configurations {
implementation.exclude group: "org.apache.httpcomponents", module: "httpclient"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
...
}
I'm pretty sure the problem came from a non-synchrony between files and Gradle sync.
So do `File/Sync Project with Gradle Files' after a change of Build Variant.
Then clean project, rebuild and run.

Running debug on older Android 4.4.2 using Android Studio

I am trying to debug on an older Android device (Samsung Tab3 4.4.2). The strange thing is I am getting an error that I do not see when running on my new devices (Android 6+).
When I hit the debug button, a Gradle build is shot off and I get the following error:
06/21 15:55:35: Launching splashActivity
The APK file C:\Users\me\Documents\src\myapp\build\outputs\apk\MyApp\debug\10-MyApp.apk does not exist on disk.
Error while Installing APK
The file is not there, but is not there when I run against the newer versions, which successfully debug. Is Android Studio doing something different due to the older version? Do I need to make some sort of Gradle adjustment?
My Gradle build appends a version to the front of the APK, could that be the problem? Here is the Gradle file (I have removed the flavors, have a ton of them):
buildscript {
ext.kotlin_version = '1.1.1'
}
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.myapp.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 10
versionName "1.1.10"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7 }
}
aaptOptions {
cruncherEnabled = false
}
dexOptions {
javaMaxHeapSize "4g"
jumboMode true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors.whenObjectAdded { flavor ->
flavor.ext.set('directoryPath', '')
flavor.ext.set('apkName', '')
}
flavorDimensions "default"
productFlavors {
MyApp {
signingConfig signingConfigs.config
directoryPath = 'myapp'
}
Flavor1 {
applicationId 'com.flavor1.flavor'
signingConfig signingConfigs.config
directoryPath = 'flavor1'
}
}
applicationVariants.all { variant ->
variant.outputs.all {
def apkName = variant.productFlavors[0].apkName
def flavor = variant.productFlavors[0].name
if (apkName != '')
flavor = apkName;
//add here your logic to customize the name of the apk
outputFileName = "${variant.versionCode}-${flavor}.apk"
}
variant.assemble.doLast { assemble ->
//copy the apk in another directory, add here your
//logic to customize the destination folder
copy {
from variant.outputs*.outputFile
into "C:/AndroidBuilds/MyApp.Build/${variant.productFlavors[0].directoryPath}"
}
//if you don't want to delete the file after copying it comment the line below
delete variant.outputs*.outputFile
}
}
}
I have removed dependencies and such to make it smaller, and changed names to protect the innocent.
Doh, it was the following line of code in Gradle, that was deleting the variant. Apparently it is needed to run on older versions of Android:
//if you don't want to delete the file after copying it comment the line below
// delete variant.outputs*.outputFile

Android studio 3: Could not find the AndroidManifest.xml file

after migrating to Android studio 3 I'm unable to compile as I' have folowing errors:
Error:Could not find the AndroidManifest.xml file, using generation
folder
[/home/salacr/git/Evotech/app/build/generated/source/apt/debug])
Error:Parceler: Code generation did not complete successfully. For
more details add the compiler argument -AparcelerStacktrace
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
It might be connected with usage of android anotation my app/build.gradle looks like this:
apply plugin: 'com.android.application'
apply plugin: 'realm-android'
def AAVersion = '4.3.1'
def parcelerVersion = '1.1.9'
android {
compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
applicationId "com.my.app"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
multiDexEnabled true
}
signingConfigs {
release {
storeFile file("******")
storePassword "******"
keyAlias "******"
keyPassword "******"
}
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
splits {
abi {
enable true // enable ABI split feature to create one APK per ABI
universalApk true //generate an additional APK that targets all the ABIs
}
}
/*
// map for the version code
project.ext.versionCodes = ['armeabi':1, 'armeabi-v7a':2, 'arm64-v8a':3, 'mips':5, 'mips64':6, 'x86':8, 'x86_64':9]
android.applicationVariants.all { variant ->
// assign different version code for each output
variant.outputs.each { output ->
output.versionCodeOverride =
project.ext.versionCodes.get(output.getFilter(com.android.build.OutputFile.ABI), 0) * 1000000 + android.defaultConfig.versionCode
}
}
*/
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation "org.parceler:parceler-api:$parcelerVersion"
annotationProcessor "org.parceler:parceler:$parcelerVersion"
annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
implementation "org.androidannotations:androidannotations-api:$AAVersion"
implementation 'com.android.support:appcompat-v7:26.0.1'
implementation 'com.android.support:multidex:1.0.2'
}
I'm unable to find the root cause for this, any suggestions?
I tried diferent graddle versions as well as using different buildToolsVersion tools, but without effect.
Any suggestions?
Thanks!
EDIT 1: I have found out that problem is here:
splits {
abi {
enable true // enable ABI split feature to create one APK per ABI
universalApk true //generate an additional APK that targets all the ABIs
}
}
Without this everything works as expected. IT seams that this config isn't compatible with androidanotations in new Android studio
EDIT 2: There is already issue in androidanotation: https://github.com/androidannotations/androidannotations/issues/2034
I had similar issue and solved my problem when I updated android annotation version from 4.4.0 to 4.5.0.
Add the following code in build.gradle file under defaultConfig
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"androidManifestFile":"$projectDir/src/main/AndroidManifest.xml".toString()
]
}
}
I solved this problem changed graddle version:
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
I had similar issue and solved my problem when I updated android annotation version from 4.4.0 to 4.6.0.

Gradle sync error with android.productFlavors setting

I use com.android.tools.build:cradle-experimental:0.7.0.
And want to build only for some abi.
So I set android.productFlavors as below:
productFlavors {
// for detailed abiFilter descriptions, refer to "Supported ABIs" #
// https://developer.android.com/ndk/guides/abis.html#sa
create("arm") {
ndk.abiFilters.add("armeabi")
}
create("arm7") {
ndk.abiFilters.add("armeabi-v7a")
}
create("x86") {
ndk.abiFilters.add("x86")
}
}
I got sync error:Error:Unable to find Android binary with buildType 'debug' and productFlavor '' in project ':xduilib'
I had google for this error message, but no result.
It is ok to only set one platform. why? What's wrong with my setting or product.
Thank you.
Finally, I use this to set target platform.
android.ndk {
moduleName = 'xxx'
abiFilters.addAll(['armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64', 'mips', 'mips64']) //this is default
ldLibs.addAll(['android', 'log'])
}

Slow debug build when using DexGuard in Android Studio with Gradle

The debug build is very slow when using DexGuard in Android Studio.
Make DexGuard to work only during "Release" build, change the "build.gradle" file:
Move "apply plugin: 'dexguard'" from the global scope to the "Release" build type scope
Remove the "getDefaultDexGuardFile" call from the "Debug" build
For example:
android {
...
...
buildTypes {
debug {
minifyEnabled false
...
}
release {
apply plugin: 'dexguard'
minifyEnabled true
proguardFile getDefaultDexGuardFile('dexguard-release.pro')
proguardFiles 'proguard-project.txt'
...
...
}
}
}
**#Retrofit**
-keep class retrofit.** { ; }
-keepattributes Signature
-keepattributes Exceptions
-keepclasseswithmembers class * {
#retrofit2.http. ;
}
-keepclasseswithmembers interface * {
#retrofit2.http.* ;
}
-dontwarn javax.annotation.Nullable
-dontwarn javax.annotation.ParametersAreNonnullByDefault
-dontobfuscate - only in debug mode production mode dont use this
-dontoptimize
-dontshrink
Please use in this lines Dexguard-project.txt file. that's all. Enjoy your coding...

Resources