Gradle: passing parameters to `apply from: <file>` - android

I'd like to put some common boilerplate gradle script code into shared .gradle file. Then, I can reuse it using apply from: statement.
The question is whether it's possible to pass parameters to applied script?
For example, I'd like to reuse the following boiler plate:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.robolectric'
configurations {
apt
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
The problem is here: apply plugin: 'com.android.application'. I'd like to reuse this code either for application projects or android library projects. So I need some parameter in order to decide which plugin to apply:
// assuming <isApplicationProject> - is a script parameter
if (isApplicationProject) {
apply plugin: 'com.android.application'
} else {
apply plugin: 'com.android.library'
}
Of course, I can just define some project-level property in this particular case, but I'd like to know whether it's possible to pass parameters upon script invocation

apply from: is line include<> you cannot pass parameters.
however it does run on the same context and therefor on the same project, so you can do something of:ext.isUsingApp=true; apply from: 'xxx'
if that's the only difference i would advise you to do something of the following:
myinclude.gradle:
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.robolectric'
configurations {
apt
}
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
then in your build.gradle app:
apply plugin: 'com.android.application'
apply from: '../includes/myinclude.gradle'
then in your libs build.gradle:
apply plugin: 'com.android.library'
apply from: '../includes/myinclude.gradle'
for a simple Boolean no need to create parameter.

Related

Android Resource Linking Failed although i have all the resource files | Kotlin

Error Screenshot
Whenever I run the app in emulator it shows the above error. But I have all the resource files.
App Directory
dependencies showed in screenshot.
Other part of the gradle is:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.modshabuisness"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
Did you clean and rebuild project? Would you please check xml selector <?xml version="1.0" encoding="utf-8"?> more than one in your xml file , if had keep it single by removing others.

Fabric "crashlyticsUploadDistributionDebug" is not there

I have just updated a project to have debug and release buildTypes, all working well till now expect the crashlyticsUploadDistributionDebug task is not in the project and all other crashlytics tasks are there
using fabricPlugin : '1.28.1'
Task 'crashlyticsUploadDistributionDebug' not found in root project
crashlyticsStoreDeobsDebug
crashlyticsStoreDeobsRelease
crashlyticsUploadDeobsDebug
crashlyticsUploadDeobsRelease
crashlyticsUploadDistributionRelease
android project build.gradle file
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.github.ben-manes.versions'
apply from: '../coverage.gradle'
android {
compileSdkVersion versions.compileSdk
buildToolsVersion versions.buildTools
defaultConfig {
applicationId 'xxxxxxxxxxxxxx'
versionCode buildInfo.number
versionName buildInfo.name
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
//Fabric
ext.betaDistributionReleaseNotes = buildInfo.releaseNotes
ext.betaDistributionGroupAliases = "xxxxxxxxxxxxxx"
}
buildTypes {
debug {
ext.enableCrashlytics = false
signingConfig signingConfigs.debug
applicationIdSuffix '.debug'
versionNameSuffix '-debug'
}
release {
signingConfig signingConfigs.release
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
xxxxxx
implementation(libraries.crashlytics) {
transitive true
}
xxxxx
}
apply plugin: 'com.google.gms.google-services'
I found there is property in the debug build type set to false
debug {
...
ext.enableCrashlytics = false
...
}
when removed or switched to true the debug task will be available :|
sorry my bad

OpenCV3 error in Camera2Renderer.java [duplicate]

I imported the opencv android library to my android studio and the Camera2Renderer class has a lot of compiler errors because the android.hardware.camera2 classes can't be imported.
I solved the problem. Jim was right, I did not have the correct target API. For the next person who has this problem and finds this thread, the solution is that you have to make sure that the build.gradle files for your project and your OpenCV match.
I am working with openCVLibrary3.2.0 and trying to run its sample and faced same issue but I changed gradle files for both mysampleApp and openCVLibrary320 module as below
This is my app build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "org.opencv.face"
minSdkVersion 9
targetSdkVersion 21
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_5
targetCompatibility JavaVersion.VERSION_1_5
}
ndk {
moduleName "native_sample"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
externalNativeBuild {
ndkBuild {
path 'src/main/jni/Android.mk'
}
}
}
dependencies {
compile project(path: ':openCVLibrary320')
}
and this my OpenCV library module build.gradle file
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
Note:
things to notice are that compileSdkVersion, buildToolsVersion, minSdkVersion, and targetSdkVersion these must be same for all gradle files
I changed the compileSdkVersion from 14 to 23
and buildToolsVersion to "23.0.2"
this solved the camera2 import related issue
I am working on openCVLibrary330 trying to run its sample and faced same issue but i changed gradle files for openCVLibrary330 module as below
This is my app build.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "26.0.2"
defaultConfig {
minSdkVersion 8
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
I had the same problem. But, as many people have suggested I didn't had to change the gradle files to match exactly the same. I changed my compileSdkVersion and buildToolsVersion to the same on both gradle files, the rest are different. Its working fine now.

Android Studio 3.0 Proguard using kotlin & greendao returns ClassLookupException

The full exception is:
Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.build.gradle.shrinker.ClassLookupException: Invalid class reference: java/rmi/server/RemoteStub
Error:java.lang.RuntimeException: com.android.build.gradle.shrinker.ClassLookupException: Invalid class reference: java/rmi/server/RemoteStub
Error:com.android.build.gradle.shrinker.ClassLookupException: Invalid class reference: java/rmi/server/RemoteStub
I am using Kotlin and GreenDao.
My gradle:
apply plugin: 'org.greenrobot.greendao'
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.xxx.xxx"
minSdkVersion 19
targetSdkVersion 25
versionCode 4
versionName "0.0.0.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
buildTypes.each {
it.buildConfigField 'String', 'GreenDAODatabaseName', '"App_Database"'
}
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
The second gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
All I did so far in the application is add a few UI classes (fragment and activity) written in kotlin.
Two entities exist for greenDAO. Before I added kotlin proguard worked flawlessly.
GreenDAO doesn't support Kotlin yet (see this).
Have you tried this order?
apply plugin: 'com.android.application'
apply plugin: 'org.greenrobot.greendao'
apply plugin: 'kotlin-android'
Like deviant-studio said here

cannot resolve dependencies in Android Studio

Im really stuck whit this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "com.example.testing"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile project(':External-Module')
}
I dont know what to do... I cant build project and use dependencies from External module.
the error is:
Error:(7, 47) error: package com.example.android.transfer does not exist
Error:(19, 9) error: cannot find symbol class ClientData
Error:(19, 25) error: cannot find symbol variable ClientData
Error:Execution failed for task ':Testing:compileDebugJava'.
but inside the IDE all is well and I can see the module
Here is also the External Module
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "com.example.externalmodule"
minSdkVersion 14
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
}
Your "External-Module" is an Android library, so according to the Gradle+Android documentation instead of
apply plugin: 'com.android.application'
it should be
apply plugin: 'com.android.library'
// Old version of the plugin
// apply plugin: 'android-library'
Also, make sure the imports in your app matches the package you are overriding in your library build script. Looks like the app is not founding com.example.android.transfer but the library package really is com.example.externalmodule

Categories

Resources