get App name in gradle.build - android

Hi I have a common module for two apps,and I want to add a dependency in that common module if I am running on a certain app,I tried the code below,but that didn't work.
if (project.getName() == 'MyApp')
{
compile 'com.facebook.react:react-native:0.49.5'
}
Any help is appreciated.

This post Might be helpful for you. It sounds like creating different product flavors is an option for creating dependency conditions.
Update:
Adding examples of productFlavors from android studio documentation
android {
...
defaultConfig {
minSdkVersion 8
versionCode 10
}
productFlavors {
flavor1 {
applicationId "com.example.flavor1"
versionCode 20
}
flavor2 {
applicationId "com.example.flavor2"
minSdkVersion 14
}
}
}
dependencies {
flavor1Compile "..."
...
flavor2Compile “...”
}
If this isn’t sufficient for your needs you could always have your dependencies rely on external project dependencies, an example from the above post I mentioned is here:
dependencies {
compile "com.squareup.leakcanary:leakcanary-android"+(project.ext.has("leakCanary")?"":"-no-op")+":1.3.1"
}

Related

How to use different dependencies in build.gradle?

(related to my question here)
I want to publish a one time update for my app for minSdk < 14 (compared to the current minSdk = 15. I am considering creating two flavors of my app - one that supports API levels lower than 14 with android support library 25.4.0, and for API level 14 and newer that uses the latest support library 27.1.1, which only support API 14 and above.
Can the dependencies section of the build.gradle support different packages imports for different flavors?
My first try looks like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
flavorDimensions "standard", "oldApi"
defaultConfig {
versionCode 37
versionName "2.1.3"
applicationId "org.app"
}
productFlavors {
standard {
dimension "standard"
targetSdkVersion 27
minSdkVersion 15
}
oldApi {
dimension "oldApi"
versionNameSuffix "X" //for old devices
targetSdkVersion 14
minSdkVersion 8
maxSdkVersion 14
}
}
signingConfigs {...}
}
buildTypes {
release {...}
debug {...}
}
}
dependencies {
standardImplementation project(path: ':MyappLibrary', configuration: 'standard')
oldApiImplementation project(path: ':MyappLibrary', configuration: 'oldApi')
}
and:
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
flavorDimensions "standard", "oldApi"
productFlavors {
standard {
dimension "standard"
}
oldApi {
dimension "oldApi"
}
}
}
dependencies {
api 'com.dropbox.core:dropbox-core-sdk:3.0.6'
standardApi "com.android.support:appcompat-v7:$package_version"
standardApi "com.android.support:support-v4:$package_version"
oldApiApi 'com.android.support:appcompat-v7:25.4.0'
}
Errors (note the bolded parts):
Unable to resolve dependency for
':app#standardOldApiDebug/compileClasspath': Could not resolve
project :MyappLibrary.
Unable to resolve dependency for
':app#standardOldApiDebugAndroidTest/compileClasspath': Could
not resolve project :MyappLibrary. Open File Show Details
Can the dependencies section of the build.gradle support different packages imports for different flavors?
You can specify:
productFlavors {
f1 {
//..
}
f2 {
//..
}
}
dependencies{
f1Implementation 'com.android.support:appcompat-v7:XX.2.1'
f2Implementation 'com.android.support:appcompat-v7:27..1'
}
More info here.

attr/colorError not found error when using com.android.support:recyclerview-v7:26.0.0-beta2

I'm using Android Studio 3.0 Canary 4. I imported the recycler view library. Then it comes out the attr/colorError not found message.
This is app build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.robyn.myapplication"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:recyclerview-v7:26.0.0-beta2'
implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
}
Whenever I add the two libraries implementation 'com.android.support:recyclerview-v7:26.0.0-beta2' and
implementation 'com.android.support:appcompat-v7:26.0.0-beta2', it comes out this error message:
I tried clean and rebuild, the error message is still there. I checked res/values/colors, the color values are there. Why I get this color error? If I want to use recycler view, what version of library should I import?
Change the following details it will work fine,
compileSdkVersion 26
buildToolsVersion "26.0.0-beta2"
Also upgrading compileSDKVersion and buildToolsVersion to 26 (it was 25) fixed the issue for me:
compileSdkVersion 26
buildToolsVersion '26.0.2'
...
dependencies {
...
compile 'com.android.support:appcompat-v7:26.0.2'
}
In general, make sure to keep all the versions consistent (compile, build, appcompat libraries).
This is to ensure compilation and stability on runtime (one can also see lint warning about the latter if lint finds differnet support library versions)
Revision 26.0.0 Beta 2
Please note that 26.0.0-beta2 is a pre-release version. Its API
surface is subject to change, and it does not necessarily include
features or bug fixes from the latest stable versions of Support
Library.
For your problem you can use "26.0.0-beta2" . It will be better if you use Stable Version .
pasting following code at Android/build.gradle bottom helped me:
subprojects {
afterEvaluate {project ->
if (project.hasProperty("android")) {
android {
compileSdkVersion 28
buildToolsVersion "28.0.3"
}
}
}
}
I've seen the same error when my app is on appcompat-26 and tries to include an Android library which in turn uses appcompat-25.
My solution has so far been to keep the app on 25 as well.
I have no idea if it's supposed to be like this. Surely you must be able to publish a library that uses the support lib version X and have it run in apps using support lib version X+1.
I am using AS 3.0-beta7 so maybe it's resolved on stable 3.0 which was released a few days ago.
Just change the minSdk:
e.g.:
android {
compileSdkVersion 26
buildToolsVersion "26.0.0-beta2"
defaultConfig {
applicationId "com.parse.starter"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
}
Hope this helps
I found this "attr/colorError" error occurred when I had created product flavours and had put the "legacy" after the "current" in my build.gradle (in "app" folder). When I put the "legacy" before the "current" (as shown below) then the error went away. Maybe the lower "versionCode" or "Sdk" versions need to appear first?
flavorDimensions "legacycurrent"
productFlavors {
legacy {
dimension "legacycurrent"
versionCode 98
minSdkVersion 9
targetSdkVersion 25
compileSdkVersion 25
}
current {
dimension "legacycurrent"
versionCode 99
minSdkVersion 14
targetSdkVersion 26
compileSdkVersion 26
}
}
FWW - For future searchers, I've added the code below to my root build.gradle to search down through dependencies and fix them to match my root project. There are probably caveats and reasons why this is a bad idea, but it consistently works for me.
subprojects {
afterEvaluate {subproject ->
if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
}
}
}
this code in android/build.gradle fixed my error.
subprojects {
afterEvaluate {project ->
if (project.hasProperty("android")) {
android {
compileSdkVersion 31
buildToolsVersion "30.0.2"
}
}
}
}

Publishing same app but two versions

Hi I wish to publish on play 2 similar apps but one with commercials and other one for money. How do I approach to this?
Do changing applicationId in gradle will be enough? Due to that play store not allowing same package names...
Please give me any tips how to arrange this in Studio and how to publish it.
You can define the flavors in your build.gradle file.
apply plugin: 'com.android.application'
android {
defaultConfig { ... }
signingConfigs { ... }
buildTypes { ... }
productFlavors {
demo {
applicationId "com.buildsystemexample.app.demo"
versionName "1.0-demo"
}
full {
applicationId "com.buildsystemexample.app.full"
versionName "1.0-full"
}
}
}
More info here.
flavors solves this problem. I think the link will help.

Android flavors, Gradle sourceSets merging

I try to make an application for multiple partners and for each partner a test and prod version. For each flavors I create a specific folder with res/values in it like the documentation said.
My gradle file look like this :
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
compileSdkVersion 14
buildToolsVersion "21.1.2"
defaultConfig {
versionCode 1
versionName 1
minSdkVersion 14
targetSdkVersion 19
}
productFlavors {
prodPARTNER1 {
applicationId "com.PARTNER1"
}
testPARTNER1 {
applicationId "com.PARTNER1.test"
}
prodPARTNER2{
applicationId "com.PARTNER2"
}
testPARTNER2{
applicationId "com.PARTNER2.test"
}
}
sourceSets {
testPARTNER2 {
res.srcDirs = ["testPARTNER2/res", "prodPARTNER2/res"]
}
}
}
dependencies {
compile project(':ViewPagerIndicatorLibrary')
compile 'com.android.support:support-v4:19.0.0'
compile 'com.google.android.gms:play-services:+'
compile files('libs/achartengine-1.1.0.jar')
compile files('libs/android-async-http-1.4.4.jar')
compile files('libs/jackson-annotations-2.2.3.jar')
compile files('libs/jackson-core-2.2.3.jar')
compile files('libs/jackson-databind-2.2.3.jar')
compile files('libs/urlimageviewhelper-1.0.4.jar')
}
I want for a test version to take res folder of the prod version (like this I not duplicate resources for both version) and merge it with the test version.
The problem is :
Error:Error: Duplicate resources:
C:\...prodPARTNER2\res\values\strings.xml:string/app_name, C:\...testPARTNER2\res\values\strings.xml:string/app_name
Any idea ? It's not possible to merge too res folder for the same flavors ?
EDIT : I use gradle v 1.1.0 and android studio v 1.1.0 too
Warning: this approach has changed a little bit on Android Plugin for Gradle 3.0.0
Use productFlavors for each partner app and define a build type test for test builds.
productFlavors {
partner1 {
applicationId "com.partner1"
}
partnerN {
applicationId "com.partnerN"
}
}
buildTypes {
debug {
// use defaults
}
release {
// the 'prod' version, use defaults
}
test {
// config as you want!
applicationIdSuffix ".test"
versionNameSuffix " Test"
debuggable true
minifyEnabled false
signingConfig signingConfigs.debug
}
}
You can use mix and match source folders as the Android Plug-in for Gradle shows:
To build each version of your app, the build system combines source code and resources from:
src/main/ - the main source directory (the default configuration common to all variants)
src/<buildType>/ - the source directory
src/<productFlavor>/ - the source directory
Edit: This user guide is another source of help: http://tools.android.com/tech-docs/new-build-system/user-guide
Edit 2: according to the link above:
Additional sourcesets are also created for each variants:
android.sourceSets.flavor1Debug
Location src/flavor1Debug/
android.sourceSets.flavor1Release
Location src/flavor1Release/
android.sourceSets.flavor2Debug
Location src/flavor2Debug/
android.sourceSets.flavor2Release
Location src/flavor2Release/
Then, you can use /src/partner1Test/ to define resources specifics to partner1 flavor AND Test build, that is a partner1Test build variant.

Android Studio 1.0 and error "Library projects cannot set applicationId"

After updating Android Studio to 1.0, I see this error:
Error: Library projects cannot set applicationId. applicationId is set
to 'com.super.app' in default config.
I updated the Gradle plugin as suggested but I did not understand how to fix this.
Based on this info:
ApplicationId in Library Projects
You cannot use applicationId to customize the package of a library project. The package name has to be fixed in library projects (and specified as packageName in the manifest). The Gradle plugin did not enforce this restriction earlier.
Removing applicationId variable from the library's build.gradle file should resolve the issue.
Thanks to Joel for his correct answer: I need to remove only 1 line from te .gradle file:
defaultConfig {
applicationId "com.super.app" <---- remove this line
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
becomes
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
and my AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.super.app">
...
This is the right solution if you don't need to rename the package name of your app. To rename it you need to use "flavours":
android {
...
productFlavors {
flavor1 {
applicationId 'com.super.superapp'
}
}
Libraries can't set applicationId and if you are working in a multi-module project and picking up flavors from a separate file , none of the above answers will work. For a modularized app, you need the following steps -
Create a flavors.gradle file in project root directory
ext.flavorConfig = { // 1
flavorDimensions "pricing"
productFlavors {
free {
dimension "pricing"
ext.myApplicationIdSuffix = '.free' // 2
}
paid {
dimension "pricing"
ext.myApplicationIdSuffix = '.paid'
}
}
productFlavors.all { flavor -> // 3
if (flavor.hasProperty('myApplicationIdSuffix') && isApplicationProject()) {
flavor.applicationIdSuffix = flavor.myApplicationIdSuffix
}
}
}
def isApplicationProject() { // 4
return project.android.class.simpleName.startsWith('BaseAppModuleExtension')
}
In 1 we export a closure so that we can use it in our modules’ build.gradle files.
In 2 we define a custom myApplicationIdSuffix property. We cannot simply have applicationIdSuffix as it is not possible to use it in library modules (build would fail if you did).
In 3 we iterate over created flavors and set applicationIdSuffix if we detect that it’s an application module only.
4 is a way to check where this closure is being used.
All that’s left is to use this closure in our modules’ build.gradle files. E.g. in application module this would look like this:
apply plugin: 'com.android.application'
apply from: "${rootProject.projectDir}/flavors.gradle"
android {
// other config...
with flavorConfig
}
If this isn't clear, you can check out this article for better understanding.
Just incase it helps some one :
When i imported an eclipse project into android studio,i got an error ::
"Error:Application and test application id cannot be the same"
Strange though,but i looked into the build.gradle and found the two placeholders,one for the application and other for testapplication.
I removed the testApplicationId from that as is suggested in this post and this helped me resolve the issue.
Note: This explaination is not related to the errors posted in this question,but might help someone who is getting a similar error.
You cannot define applicationId for your lib.
But incase you want to use an identifier in your build file, which will give you, your library package name, you can define a variable for the module and then use the value as required.
eg : Library's build.gradle
apply plugin: 'com.android.library'
def libraryGroupId = 'com.google.example'
def libraryArtifactId = project.getName()
def libraryVersion = '1.1'
Also, you can use the value below as needed in your build file itself in lib.
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "$libraryVersion"
resValue "string", "Library", libraryGroupId"
}
}

Categories

Resources