I'm building an application which needs a library with 2 variants - simulator & actual.
My objective is to create build tasks say assembleSimulatorDebug & assembleDebug.
assembleSimulatorDebug will include simulator module/library and assembleDebug will include actual module to build the apk.
I'm thinking of having if else blocks in dependencies sections of build.gradle. Can I have something like this?
I'm very new to gradle and trying from yesterday to set up something like this. It will be v helpful if anyone can provide some hints or links where I can get an idea to achieve this.
Update:
Found another solution. It's in another my answer.
It is possible by usage of 'Flavor products' of android gradle plugin. You can split library implementation by product flavor.
Initial requirements 'com.android.tools.build:gradle:1.0.0', Android Studio 1.0.1.
I created project at GitHub, so you'll be able to get it, run and learn.
You need to create two flavors of your app and library, too. For example, normal and simulator
Application's build.gradle
apply plugin: 'com.android.application'
android {
lintOptions {
abortOnError false
}
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.tivogi.gradle"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
normal {
}
simulator {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
normalCompile project(path: ':library', configuration: 'normalRelease')
simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
}
Library's build.gradle
apply plugin: 'com.android.library'
android {
publishNonDefault true
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
normal {
}
simulator {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
}
Library:
split implementation between flavors by productFlavors (see my sample project if will have questions about that);
add publishNonDefault true into its build.gradle
From Gradle Plugin User Guide
It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well).
Publishing of all variants are not enabled by default.
To enable them:
android {
publishNonDefault true
}
Application:
add normal and simulator product flavors (productFlavors);
add next lines to dependencies section
normalCompile project(path: ':library', configuration: 'normalRelease')
simulatorCompile project(path: ':library', configuration: 'simulatorRelease')
From Gradle Plugin User Guide
To create a dependency on another published artifact, you need to specify which one to use:
dependencies {
flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}
After all of that you'll be able to build 2 variants of applicaiton: normal and simulator.
Found solution for 2 libraries, it looks simpler. It's based on next from official guide
Finally, like Build Types, Product Flavors can have their own dependencies. For instance, if the flavors are used to generate a ads-based app and a paid app, one of the flavors could have a dependency on an Ads SDK, while the other does not.
dependencies {
flavor1Compile "..."
}
So you can create 2 individual libraries and create 2 flavors, for example, libraries are library-normal and library-simulator and flavors are normal and simulator.
Application build.gradle
apply plugin: 'com.android.application'
android {
lintOptions {
abortOnError false
}
compileSdkVersion 21
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.tivogi.gradle"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
normal {
}
simulator {
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
normalCompile project(":library-normal")
simulatorCompile project(":library-simulator")
}
Application:
add only those lines into dependencies;
normalCompile project(":library-normal")
simulatorCompile project(":library-simulator")
Library:
does not require any change;
I published sample project of this solution at individual-libraries branch.
Related
Upgraded to:
Android Studio 3.0 Canary 2
com.android.tools.build:gradle:3.0.0-alpha2
I have a multi-module project (main app + sub modules)
Inclusion inside the main app:
dependencies {
implementation project(path: ':testlib', configuration: 'default')
}
The testlib is defined as a simple android library project and works normally when included with gradle 2.3.0 and via compile project(path: ':testlib')
I get the following gradle error message:
Could not resolve all dependencies for configuration ':app:devDebug_signedCompileClasspath'.
Selected configuration 'default' on 'project :testlib'
but it can't be used as a project dependency because
it isn't intended for consumption by other components.
What does "isn't intended for consumption by other components" mean in this context? The module is defined as an android library.
Here is the build.gradle of the testlib:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha2'
}
}
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
minSdkVersion 16
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 {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
What am I missing?
I also got this error. Android Studio 3.0 Canary 4 just came out. I updated to it, which also updates gradle to 4.0rc1.
The problem went away on it's own.
alter your Top-level build file (aka root gradle)
classpath 'com.android.tools.build:gradle:3.0.0-alpha4'
still not working?
update dist-url (inside gradle-wrapper.properties)
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-rc-1-all.zip
I am getting error after update to appcompat-v7:24.0.0-alpha1 on generating the signed apk.
Error:Error: Style Resource definition cycle: TextAppearance.AppCompat.Light.SearchResult.Title => TextAppearance.AppCompat.SearchResult.Title => TextAppearance.AppCompat.SearchResult.Title [ResourceCycle]
Temporary but Working Solution:
I was searching for the solution for about two days but I was unable to create signed apk, finally I found the answer on this thread: https://code.google.com/p/android/issues/detail?id=203407
Just put these 3 lines in your 'app' build.gradle file under android()
lintOptions {
checkReleaseBuilds false
abortOnError false
}
finally your build.gradle file will be like this:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '24.0.0 rc2'
defaultConfig {
applicationId "abc.xyz"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
//Here the magic Begins
lintOptions {
checkReleaseBuilds false
abortOnError false
}
//Here the magic Ends
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('src/main/libs/YouTubeAndroidPlayerApi.jar')
compile 'de.greenrobot:greendao:2.1.0'
}
I hope this answer will help you. This will create your build, later when proper 24 support libraries release available, you have to change it properly.
Faced same issue, here's right fix for it.
Start your SDK Manager, goto Extras section, here you can see update for Support Library. Install it.
Then open build.gradle/app.gradle
change
compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
to
compile 'com.android.support:appcompat-v7:24.0.0-alpha2'
Problem solved! cheers.
https://code.google.com/p/android/issues/detail?id=203407
check it for more details...
paste it in your Gradle of your project
classpath 'com.android.tools.build:gradle:2.1.0-alpha1'
compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
compile 'com.android.support:design:24.0.0-alpha1'
compile 'com.android.support:support-v4:24.0.0-alpha1'
compile 'com.android.support:cardview-v7:24.0.0-alpha1'
Downgrade local maven repository for support libraries.
Reverting back to 26 solves my problems.
See https://code.google.com/p/android/issues/detail?id=203546#c10
Long ago, a Google Code ticket was opened as the Gradle plugin does not propagate whether you are doing a debug build or a release build to a dependent android library:
DESCRIPTION:
Gradle plugin does not propagate whether you are doing a
debug build or a release build to a dependent android library.
VERSION INFORMATION:
gradle plugin: 0.3 gradle: 1.4
This is a problem, especially when the release configuration of the library prevents the edition of the code or the debug process to occur normally (red highlighting of native methods as the IDE does not find any longer while they link normally, broken Java step-by-step debugging...).
This was quite thoroughly discussed on Google Code and here on Stack Overflow, and it happened that Kane O'Riley proposed a really interesting (and clean) workaround:
Put this in your app:
dependencies {
debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
releaseCompile project(path: ':custom_lib', configuration: "libraryRelease")
}
and in your library's build.gradle add:
defaultPublishConfig 'release'
publishNonDefault true
productFlavors {
library {
}
}
I gave a try, but I have the following message at Gradle sync time:
Error:Configuration with name 'libraryDebug' not found.
I tried with just "debug" and "release" (which are the build config names I use in my lib) but the result is the same.
Any idea to make this interesting workaround working? (I'm running Android Studio 2.0 beta 2)
APPENDIX:
my lib's build.gradle file (including the ugly workaround I'm currently using):
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
// THIS IS WHERE I INSERT THE THREE STATEMENTS PERTAINING
// TO THE LIB FROM Kane O'Riley'S WORKAROUND ABOVE
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
/*
release {
// UNCOMMENT BEFORE MAKING AN ACTUAL RELEASE !!! (duh...)
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-fvisibility=hidden -g0 -DSHLUBLU_ACTUAL_RELEASE -O3"
}
buildConfigField "boolean", "actualRelease", "true"
debuggable false
jniDebuggable false
minifyEnabled false
}
*/
release {
// COMMENT THIS WHOLE BLOCK BEFORE MAKING AN ACTUAL RELEASE !!!
// (this is a copy of the "debug" config below... did I say 'duh' ?)
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-g"
}
buildConfigField "boolean", "actualRelease", "false"
debuggable true
jniDebuggable true
minifyEnabled false
}
debug {
// IF ONLY THIS ONE WAS USED WHEN DEBUGGING!
// (Only the IDE uses that, but the release build is actually linked,
// see https://code.google.com/p/android/issues/detail?id=52962 )
ndk {
moduleName "mylib"
ldLibs "log"
cFlags "-g"
}
buildConfigField "boolean", "actualRelease", "false"
debuggable true
jniDebuggable true
minifyEnabled false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
my project's build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
versionCode 27
versionName "1.4"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
debuggable false
jniDebuggable false
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-shlublu.txt'), 'proguard-rules.pro'
}
debug {
debuggable true
jniDebuggable true
minifyEnabled false
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':mylib')
// THE LINE ABOVE IS WHAT I REPLACE BY Kane O'Riley's WORKAROUND
// DESCRIBED AT THE BEGINNING OF THIS POST
}
my Gradle config:
For this to work, the twobuild.gradle files must not be modified at the same time before syncing once for all. I had to follow the following steps:
Step 1: modify the lib's build.gradle, exactly as Kane said:
// "android" section:
defaultPublishConfig 'release'
publishNonDefault true
productFlavors {
library {
/* This strange empty flavour is actually needed
for the step 3 to be successful */
}
}
Step 2: clean/rebuild
Step 3: modify the app's build.gradle, also as Kane said:
dependencies {
debugCompile project(path: ':custom_lib', configuration: "libraryDebug")
releaseCompile project(path: ':custom_lib', configuration: "libraryRelease")
}
Step 4: Gradle sync.
So it was just a matter of changing the library first, and then cleaning before modifying the app.
I checked the APK produced by the debug and release build modes, and each of them contains the proper variant of the lib, unlike before applying this workaround, so it does work (thanks Kane !).
Shlublu's solution didn't work for me, no matter what steps I used.
After much digging, found out that one can define available configurations (at least using experimental gradle), and this in turn avoided having to clean/rebuild in hope of fixing all errors.
Using latest gradle with AS2.1, I was able to solve this issue with a very simple dependency content:
Lib's build.gradle (uses experimental gradle 0.7.0):
model {
android {
...
productFlavors {
create('all') {
...
}
}
publishNonDefault true
}
}
configurations {
allDebug
allRelease
}
App's build.gradle (uses standard gradle 2.1.0):
debugCompile project(path: ':lib', configuration: 'allDebug')
releaseCompile project(path: ':lib', configuration: 'allRelease')
I'm pretty sure the productFlavors in the lib's gradle is not needed if using debug/release configurations only, like this:
Lib's build.gradle (uses experimental gradle 0.7.0):
model {
...
}
configurations {
debug
release
}
App's build.gradle (uses standard gradle 2.1.0):
debugCompile project(path: ':lib', configuration: 'debug')
releaseCompile project(path: ':lib', configuration: 'release')
I want to use this library in a project in Android Studio, so as mentioned in read me, I created new project in the Android Studio and added dependencies in build.gradle file inside the app folder as:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.mycompany.swipe_instagram"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.lorentzos.swipecards:library:1.0.8#aar'
}
Then when I sync I started getting this error as:
I have Searched on the google and stack overflow but can't find any appropriate solution.
I used your "build.gradle" file and it is working fine for me (both with and without "aar"). Could be something wrong with the cache. Try "File -> Invalidate Caches / Restart"). Another way to generate more error message detail is to try building the module from the command line and getting verbose output
gradle -info compileDebugSources
You'll get lots of output which should give you a hint as to what's going wrong
I'm trying to share 2 external dependencies between 2 modules in Android Studio.
The 2 dependencies are Twitter Core and Twitter4j (a Twitter library extension I'm experimenting with).
Here is the project structure:
Root project 'cineios-test'
+--- Project ':app'
\--- Project ':cineio-broadcast-android-sdk'
I set up the dependencies in my app build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.lgorse.cineios_test"
minSdkVersion 18
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions{
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':cineio-broadcast-android-sdk')
//compile project(':cineio-broadcast-android')
//compile 'io.cine:cineio-broadcast-android-sdk:0.0.9'
compile ('org.twitter4j:twitter4j-stream:4.0.2'){
transitive = true;
}
compile('com.twitter.sdk.android:twitter:1.1.1#aar') {
transitive = true;
}
}
Here is the build.gradle file for the module, which is cineios-android-sdk:
apply plugin: 'android-library'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
// applicationId 'io.cine.android'
minSdkVersion 18
targetSdkVersion 19
versionCode 11
versionName '0.0.11'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile 'com.google.guava:guava:16.0'
compile 'com.loopj.android:android-async-http:1.4.5'
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:support-v4:20.0.0'
}
Finally here is settings.gradle:
include ':app', ':cineio-broadcast-android-sdk'
project(':cineio-broadcast-android-sdk').projectDir = new File('cineio-broadcast-android/cineio-broadcast-android-sdk')
I know there are answers on SO but they refer adding local libraries as modules - but since these dependencies are remote I'm not sure how to adapt the hints to this situation.
I did try adding the dependencies to the other module (cineios-android) but a) it seems ridiculous to double them up like that and b)that would imply registering a new app in the Twitter API, which will probably lead to errors.
The correct approach really is to specify the dependencies in both the main app and the module.
I did try adding the dependencies to the other module (cineios-android) but a) it seems ridiculous to double them up like that and
There's really nothing ridiculous about it. Don't think of it as trying to "share" the dependency between the main app and the module. Look at it this way: your module depends on Twitter4j and Twitter Core. If you were to reuse that module in a different application, the module should be self-contained and should be able to specify its own dependencies without the parent project needing to set them up. Making all its dependencies explicit does this.
If the parent app also depends on Twitter4j and Twitter Core, and if you use the Maven Coordinate-style remote dependency specs as you have, the build system will ensure that only one copy actually gets linked into the app, so everything will be okay.
b)that would imply registering a new app in the Twitter API, which will probably lead to errors.
I'm not familiar with how that works, so I can't comment on it other than to say that if their API is well designed, hopefully just including the library shouldn't imply something like that. If you're having problems, clarify your question or ask a new one.
In your build.gradle add dependency like below:
dependencies {
compile 'org.openimaj:twitter:1.3.1'
}
You can also use belowed link for more reference :
Gradle Please