How to use different dependencies in build.gradle? - android

(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.

Related

get App name in gradle.build

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"
}

minSdkVersion 9 cannot be smaller than version 14 declared in library

Error:Execution failed for task ':app:processDebugManifest'.
Manifest merger failed : uses-sdk:minSdkVersion 9 cannot be smaller than version 14 declared in library [com.google.android.gms:play-services:11.0.4] C:\Users\hp elitebook\Desktop\voiceTranslator\app\build\intermediates\exploded-aar\com.google.android.gms\play-services\11.0.4\AndroidManifest.xml
Suggestion: use tools:overrideLibrary="com.google.android.gms.play_services" to force usage
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "26.0.2"
defaultConfig {
applicationId "com.shaker.voicetranslator"
minSdkVersion 9
targetSdkVersion 17
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.0'
compile 'com.google.android.gms:play-services:+'
You are using
compile 'com.google.android.gms:play-services:+'
This library in your SDK Manager is resolved with com.google.android.gms:play-services:11.0.4 (pay attention if you want to use the latest version you have to add the google maven repo).
This dependency has a minSdk =14 and you can't use a minSdk=9.
You have to change
minSdkVersion 9
with
minSdkVersion 14
Also, since you will have a dependency with the latest support libraries you have to change the API used to compile. Use:
compileSdkVersion 26

Android: Standard suggested dependencies give Gradle sync error. Failed to resolve: com.android.support:wear:24.0.0 [duplicate]

This question already has an answer here:
When using compile 'com.google.android.support:wearable:2.0.4' I get the error below, but I am not using 26.0.0
(1 answer)
Closed 5 years ago.
I'm developing a music application for both mobile & wear. I've completed building the base of the application on mobile, and now I want to start the wearable (2.0) part. I tried following the tutorial on the Android Developer site, but even following the base directions gives me a Gradle sync error. I tried adjusting the compileSdkVersion and targetSdkVersion both to 24, which I use for my mobile gradle scrip. It didn't make a difference however.
This is the instruction mentioned:
In the build.gradle file for the Wear module:
>> In the android section, confirm that the compileSdkVersion is set to 25.
>> In the android section, confirm that the targetSdkVersion is set to 25.
Update the dependencies section as follows (requires the latest version of the Google Repository):
compile 'com.android.support:wear:26.0.0'
compile 'com.google.android.support:wearable:2.0.3'
provided 'com.google.android.wearable:wearable:2.0.3'
.
Clicking on "install repository and sync project" has no effect.
This is the build.gradle file for the Wear module.
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "be.ehb.dt.finalwork_lievenluyckx_v001"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:wear:24.0.0'
compile 'com.google.android.support:wearable:2.0.3'
provided 'com.google.android.wearable:wearable:2.0.3'
}
I'm really new to Android, and I don't really understand what is going wrong here. Any help is appreciated, even if it isn't a direct solution to the issue!
Edited: After trial-error,solution finally was found:
In build.gradle file -> allprojects, within repositories,code below should be added...
1) For the Android Gradle plugin revision 2.3.3 with Gradle 3.3 (Android Studio 2.3.3)
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
2) For the Android Gradle plugin revision 3.0.0 with Gradle 4.1(Android Studio 3.0.0)
allprojects {
repositories {
jcenter()
google()
}
}
For more here: When using compile 'com.google.android.support:wearable:2.0.4' I get the error below, but I am not using 26.0.0
Change your gradle to:
android {
compileSdkVersion 25
buildToolsVersion "25.0.1"
defaultConfig {
applicationId "be.ehb.dt.finalwork_lievenluyckx_v001"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:wear:26.0.0'
compile 'com.google.android.support:wearable:2.0.3'
provided 'com.google.android.wearable:wearable:2.0.3'
}

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"
}
}
}
}

Android studio importing in gradle issue

I am new to Android Studio and was just trying to import my eclipse project. i am trying to resolve this issue for quite a while now but unable to.
I have a mainactivity project which uses mmany other libraries staggeredgridview, devsmart, google maps and appcompactv7. I am getting following 2 errors.
Note: mainactivity project is the one which i am working on and uses other external libraries.
Error:(7, 5) uses-sdk:minSdkVersion 8 cannot be smaller than version 9 declared in library B:\Android Studio Projects\mainActivity\build\intermediates\exploded-aar\com.google.android.gms\play-services\6.5.87\AndroidManifest.xml
Suggestion: use tools:overrideLibrary="com.google.android.gms" to force usage
:mainActivity:processDebugManifest FAILED
Error:Execution failed for task ':mainActivity:processDebugManifest'.
Manifest merger failed with multiple errors, see logs
build.gradle of mainactivity
apply plugin: 'com.android.application'
android {
compileSdkVersion 17
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.staggeredgridviewdemo"
minSdkVersion 8
targetSdkVersion 16
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':devsmartAndroid')
compile project(':staggeredGridViewmaster')
compile 'com.google.guava:guava:16.0.1'
compile 'com.google.android.gms:play-services:+'
compile 'com.android.support:appcompat-v7:19.1.0'
compile files('libs/commons-codec-1.6.jar')
}
build.grade for devsmart
apply plugin: 'com.android.library'
android {
compileSdkVersion 14
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 4
targetSdkVersion 4
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile files('libs/CWAC-SackOfViewsAdapter.jar')
compile('com.android.support:appcompat-v7:19.1.0') {
// really use 19.1.0 even if something else resolves higher
force = true
}
}
build.gradle for sttageredgridview
apply plugin: 'com.android.library'
android {
compileSdkVersion 17
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile('com.android.support:appcompat-v7:19.1.0') {
// really use 19.1.0 even if something else resolves higher
force = true
}
}
Just edit minsdk of your build.gradle of mainactivity to 9 :
defaultConfig {
applicationId "com.example.staggeredgridviewdemo"
minSdkVersion 9 // changed line
targetSdkVersion 16
}
Note : Also did the same for other libs if there minsdk is small then 9
Please open your manifest file and write the bellow code it will work
<uses-sdk tools:overrideLibrary="com.google.android.gms"/>
and add the list of libraries for uses-skd separated by comma
Go to all the manifest files or build.gradle files of all the projects and make the minSDKversions the same.8 for instance.

Categories

Resources