Android java.lang.NoClassDefFoundError: R$string - android

whenever i run a unit test, i get this error when it tries to retrieve a string value from a custom config.xml file.
Background:
The project itself is a library apk project that uses and references another library APK project.
The error occurs when the project itself tries to initiate a new object that is a subclass of a super class contained on the referenced library apk project.
The Issue explained more below
The specific line of code that fails with the error is a static variable defined below:
protected static final String ANDROID_CONFIG_ID = LibraryApplication.getApplication().getString(R.string.api_checkout_config_id_override);
it fails with the following error:
java.lang.NoClassDefFoundError: com/jon/commonlib/R$string
commonLib is the referenced library apk if you are wondering.
Here is my unit test
#RunWith(RobolectricTestRunner.class)
#Config(constants = BuildConfig.class, manifest=Config.NONE)
public class TestSearchShows {
#Test
public void testSearchJsonFile(){
EventsTestHelper testHelper = new EventsTestHelper(RuntimeEnvironment.application);
try {
ShowsList showList = testHelper.getShowList(new SearchEvent());
if(showList.getShows().size() < 0){
Assert.fail("show list is empty");
}
} catch (IOException e) {
e.printStackTrace();
Assert.fail(e.toString());
}
}
}
The EventsTestHelper is the sub class for a super class called NetworkHelper which looks like this:
public abstract class NetworkHelper<T, P, S> implements NetworkConstants {
protected static final String ANDROID_CONFIG_ID = LibraryApplication.getApplication().getString(R.string.api_checkout_config_id_override);
//other stuff ....
}
I use robolectric version 3.0 the latest for runing ,my unit tests.
If i was to run the code live and call and initiate the sub class, it works perfectly fine, no crashes
edit: Here is snippets of my build gradle file below
apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.library'
apply plugin: 'crashlytics'
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'http://www.testfairy.com/maven' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
classpath 'com.testfairy.plugins.gradle:testfairy:1.+'
}
}
repositories {
mavenCentral()
maven { url 'http://download.crashlytics.com/maven' }
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
flatDir {
dirs 'libs'
}
}
android {
compileSdkVersion 19
buildToolsVersion "22.0.1"
def package_namespace = "com.jonney.moduleApp"
defaultConfig {
minSdkVersion 14
testApplicationId "com.jonney.moduleApp"
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
productFlavors {
//testing {
//}
local {
}
mock {
}
qa {
}
//qa4 {
//}
production {
}
}
sourceSets {
local {
res.srcDir 'build-config/local/res'
}
testing {
res.srcDir 'build-config/testing/res'
}
mock {
res.srcDir 'build-config/mock/res'
}
qa {
res.srcDir 'build-config/qa/res'
}
qa4 {
res.srcDir 'build-config/qa4/res'
}
staging {
res.srcDir 'build-config/staging/res'
test.java.srcDirs += 'src/main/java'
}
production {
res.srcDir 'build-config/production/res'
test.java.srcDirs += 'src/main/java'
test.java.srcDirs += "build/generated/source/r/production"
test.java.srcDirs += "build/generated/source/buildConfig/production"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.1.+'
compile 'com.google.code.gson:gson:2.3'
testCompile('org.robolectric:robolectric:3.0') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
compile 'com.fasterxml.jackson:jackson-parent:2.5'
compile 'com.squareup:otto:1.3.6'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.sothree.slidinguppanel:library:3.0.0'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'
compile "joda-time:joda-time:2.4"
testCompile('junit:junit:4.12') {
exclude module: 'hamcrest'
exclude module: 'hamcrest-core'
}
testCompile 'org.hamcrest:hamcrest-all:1.3'
compile 'com.sothree.slidinguppanel:library:3.0.0'
compile 'com.squareup:otto:1.3.6'
compile 'com.squareup.okhttp:okhttp:2.3.0'
testCompile 'org.apache.maven:maven-ant-tasks:2.1.3'
compile 'com.google.android.gms:play-services:7.0.0'
compile 'com.android.support:multidex:1.0.0'
compile(name: 'commonLib 1.0 1', ext: 'aar')
testCompile(name: 'commonLib-1.0 1', ext: 'aar')
}
edit: i have also tried to manually create a task that copies the r class for each dependency.
afterEvaluate { project ->
android.libraryVariants.each { variant ->
// workaround for missing R class for aar dependencies
def copyTaskName = "copy${variant.name.capitalize()}AppCompat"
def copyTaskNameTwo = "copy${variant.name.capitalize()}commonlib"
task(copyTaskName, type:Copy) {
dependsOn "process${variant.name.capitalize()}Resources"
from { "build/generated/source/r/${variant.dirName}/$package_namespace_path" }
into { "build/generated/source/r/${variant.dirName}/com/jon/commonlib" }
// into { "src/test/java/android/support/v7/appcompat" }
include 'R.java'
filter { line -> line.contains("package ${package_namespace};") ? 'package android.support.v7.appcompat;' : line }
outputs.upToDateWhen { false }
}
task(copyTaskNameTwo, type:Copy) {
dependsOn "process${variant.name.capitalize()}Resources"
from { "build/generated/source/r/${variant.dirName}/$package_namespace_path" }
into { "build/generated/source/r/${variant.dirName}/android/support/v7/appcompat" }
// into { "src/test/java/android/support/v7/appcompat" }
include 'R.java'
filter { line -> line.contains("package ${package_namespace};") ? 'package com.jon.commonlib;' : line }
outputs.upToDateWhen { false }
}
System.out.println("adding ${copyTaskName} build/generated/source/r/${variant.dirName}/$package_namespace_path ")
println("basename = ${variant.baseName}")
println("directory name = ${variant.dirName}")
tasks.getByName("compile${variant.name.capitalize()}UnitTestJava") dependsOn copyTaskName
}
}
Kind regards
jonnney

The project itself is a library apk project that uses and references another library APK project.
For this type of projects exist an already known issue https://github.com/robolectric/robolectric/issues/1796 but you can workaround it.
The base issue is the android/gradle behaviour for library projects which is different compared to application projects. It just ignores to generate R classes from aar dependencies.
To workaround you can provide your own R class which already contains all dependencies R values. Here an example for the appcompat dependency
afterEvaluate { project ->
android.libraryVariants.each { variant ->
// one line for each aar dependency
tasks.getByName("assemble${variant.name.capitalize()}").dependsOn copyAppcompat
}
}
// copy it for each aar dependency and adjust it to your needs
task copyAppcompat(type: Copy) {
// replace the base package with yours (all after /r/debug/) which contains your R.class
from 'build/generated/source/r/debug/com/example/core'.replace("/", File.separator)
// replace the library packages with yours (all after /test/java/) with your aar dependency base package
into 'src/test/java/android/support/v7/appcompat'.replace("/", File.separator)
// also replace the package declaration or you will get compile errors
filter { line -> line.contains('package com.example.core;') ? 'package android.support.v7.appcompat;' : line }
include 'R.java'
}
An example project with your setup can be found at https://github.com/nenick/AndroidStudioAndRobolectric/tree/library-with-aar

Are you including the library in your testing classpath?
dependencies {
androidTestCompile(<reference to commonLib>)
}

I wasn't able to reproduce this in any way, but here are two things that might help.
Try using RobolectricGradleTestRunner instead of RobolectricTestRunner
You shouldn't need to put your other library in testCompile. So I think you can safely remove testCompile(name: 'commonLib-1.0 1', ext: 'aar')
Let me know in the comments if you have any updates after you try the different runner.

Related

Android aar include jar dependency resources folder

Hello
While developing an Android AAR module, I encounter the following problem
My AAR module has a few jar dependencies, some of them have resources folder with error localized error messages. For some reason when I create the module AAR I noticed that Android build process exclude the resources folder from the jar dependency. The dependencies folder is an essential part since otherwise the error details are not found.
If something could shed light on the issue, I tried to create additional resources folder under main/resources//messages.properties but it didn't help.
Any help will be appreciated
here is my gradle file
apply plugin: 'com.android.library'
apply plugin: 'jacoco'
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2'
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
android {
compileSdkVersion 27
buildToolsVersion '26.0.3'
configurations {
javadocDeps
}
defaultConfig {
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled false
}
}
testOptions {
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
unitTests.returnDefaultValues = true
}
lintOptions {
abortOnError false
}
sourceSets {
androidTest {
...
}
test {
}
}
}
//assembleDebug.dependsOn(copyResDirectoryToClasses)
dependencies {
releaseCompile fileTree(include: ['*.jar'], dir: 'libs')
provided 'com.android.support:appcompat-v7:27.1.1'
provided 'org.jetbrains:annotations:16.0.3'
provided 'com.google.code.findbugs:annotations:3.0.1'
provided 'org.codehaus.sonar-plugins:sonar-ant-task:2.2'
provided 'org.jetbrains:annotations-java5:15.0'
provided 'com.google.android:annotations:4.1.1.4'
provided 'com.squareup.okhttp3:okhttp:3.9.1'
provided 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.2'
androidTestCompile files('../sdkSample/lib/qa_utils.jar')
androidTestCompile 'com.google.android:annotations:4.1.1.4'
javadocDeps 'com.android.support:appcompat-v7:27.1.1'
javadocDeps 'com.squareup.okhttp3:okhttp:3.9.1'
javadocDeps 'com.squareup.okhttp:okhttp-urlconnection:3.9.1'
javadocDeps 'com.google.code.findbugs:annotations:3.0.1'
javadocDeps group: 'org.mozilla', name: 'rhino', version: '1.7.7.1'
javadocDeps group: 'org.jetbrains', name: 'annotations', version: '16.0.3'
testCompile files('../sdkSample/lib/qa_utils.jar')
testCompile 'junit:junit:4.12'
testCompile 'org.json:json:20171018'
testCompile 'com.google.android:annotations:4.1.1.4'
testCompile 'org.testng:testng:6.13.1'
testCompile 'com.squareup.okhttp3:okhttp:3.9.1'
testCompile 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1'
testCompile 'org.robolectric:robolectric:3.8'
androidTestCompile 'com.squareup.okhttp3:okhttp:3.9.1'
androidTestCompile 'com.squareup.okhttp3:okhttp-urlconnection:3.9.1'
testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
}
gradle.taskGraph.whenReady {
gradle.taskGraph.allTasks.each {
if(it.project == 'sdkSample') {
it.onlyIf { false }
}
}
}
//ignore all test failures - is done temporary, the final solution is test categorization
project.gradle.taskGraph.whenReady {
-> project.tasks.findAll { it.name =~ /connected.+AndroidTest/ ||
it.name == "testDebugUnitTest" ||
it.name =="testReleaseUnitTest" }.each {
it.ignoreFailures = true
}
}
You need to use api instead of provided. You can find more info from here.
As i know aar cant have transitive dependencies. You may try export fat aar or you can give it a try to
implementation 'your.package:your_artifact:your_version' {transitive = true}
PS: Dont use type #aar
Also known as flatDir, because flatDir dependencies DOES NOT resolve transitives)
implementation 'your.package:your_artifact:your_version#aar' {transitive = true}
On the otherhand, you need to convert your project to maven standards. So that, you need a pom.xml file which points transitive dependencies under dependencies tag.
As you experienced jar files cant have resource files either. If you dont need to resource files, just export jar by executing createFullJarRelease task.
If you have to use .aar, you can add other dependencies that library uses in consumer.

Gradle build error (manifestOutputFile) when upgrading plugin to 3.0.0

After upgrading the Gradle plugin version to 3.0.0 (classpath "com.android.tools.build:gradle:3.0.0") and if I then try to clean or build the project I get this error:
A problem occurred configuring project ':app'.
> Manifest Tasks does not support the manifestOutputFile property any
more, please use the manifestOutputDirectory instead.
For more information, please check
https://developer.android.com/studio/build/gradle-plugin-3-0-0-
migration.html
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or
--debug option to get more log output.
* Get more help at https://help.gradle.org
I am not using manifestOutputFile anywhere in any of my gradle files. using the --debug and --stacktrace flags did nothing for me. I'm guessing this issue is coming up in one of my dependencies but I have no idea. Also, I am using Kotlin in a lot of classes; I'm not sure if that matters or not. Does anyone know if there is a way to see which library is throwing this error? Or does anyone have any suggestions or insight around this issue when the build.gradle file does not even reference manifestOutputFile?
Here's most of the build.gradle file:
buildscript {
repositories {
jcenter()
mavenCentral()
maven { url 'https://maven.fabric.io/public' }
maven { url 'https://plugins.gradle.org/m2/' }
maven { url 'https://maven.google.com'}
}
dependencies {
classpath 'io.fabric.tools:gradle:1.24.4'
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
classpath 'com.dicedmelon.gradle:jacoco-android:0.1.2'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.17.0'
classpath 'gradle.plugin.com.nobleworks_software.icon-overlay:icon-overlay-plugin:1.2.3'
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'io.fabric'
apply plugin: 'checkstyle'
apply plugin: 'findbugs'
apply plugin: 'jacoco'
apply plugin: 'jacoco-android'
apply plugin: 'com.github.ben-manes.versions'
apply plugin: 'com.nobleworks_software.icon-overlay'
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
flatDir {
dirs 'libs'
}
}
ext {
libsSrcDir = new File('${projectDir}/libs')
}
String gpsPropertyName = 'gps'
String gpsPropertyValueAlpha = 'alpha'
String gpsPropertyValueBeta = 'beta'
boolean tangoBuild = false
String tangoPropertyName = 'tango'
String alphaBuildEnding = '20'
String betaBuildEnding = '10'
String productionBuildEnding = '00'
def isBuildForGPS = { ->
return project.hasProperty(gpsPropertyName)
}
def isTangoBuild = { ->
return project.hasProperty(tangoPropertyName)
}
def getBranchName = { ->
try {
def branchOut = new ByteArrayOutputStream()
exec {
commandLine 'git', 'symbolic-ref', '--short', 'HEAD'
standardOutput = branchOut
}
return branchOut.toString().trim()
}
catch (ignored) {
return 'master'
}
}
def getVersionCode = { ->
try {
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', '--count', 'HEAD'
standardOutput = stdout
}
// Make the Tango version use '50' as the least sig offset
int tangoOffset = 0
if (tangoBuild) {
tangoOffset = 50
}
if (isBuildForGPS()) {
String channel = project.findProperty(gpsPropertyName)
if (gpsPropertyValueAlpha == channel) {
return Integer.parseInt(stdout.toString().trim() + alphaBuildEnding) + tangoOffset
} else if (gpsPropertyValueBeta == channel) {
return Integer.parseInt(stdout.toString().trim() + betaBuildEnding) + tangoOffset
}
}
return Integer.parseInt(stdout.toString().trim() + productionBuildEnding) + tangoOffset
}
catch (ignored) {
return -1
}
}
def getVersionName = { ->
try {
if (isBuildForGPS()) {
def tag = getLastTaggedVersion()
String channel = project.findProperty(gpsPropertyName)
if (gpsPropertyValueAlpha == channel) {
tag = tag + '-a'
} else if (gpsPropertyValueBeta == channel) {
tag = tag + '-b'
}
return tag
} else {
return getBranchName()
}
}
catch (ignored) {
return 'X.X.X'
}
}
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dexOptions {
javaMaxHeapSize "4g"
}
dataBinding {
enabled = true
}
defaultConfig {
versionCode getVersionCode()
versionName getVersionName()
applicationId getApplicationId()
minSdkVersion 16
targetSdkVersion 26
multiDexEnabled = true
multiDexKeepFile file('multidex_keep_file.txt')
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "com.demoapp.demoapp.WayTestRunner"
renderscriptTargetApi 16
renderscriptSupportModeEnabled true
String branchName = getBranchName().toUpperCase()
String iconOverlayText = branchName.toLowerCase()
buildConfigField "String", "LAST_TAGGED_VERSION", "\"${getLastTaggedVersion()}\""
if (branchName == 'DEV') {
iconOverlayText = 'ALPHA'
} else if (branchName.startsWith('RELEASE')) {
iconOverlayText = 'BETA'
}
if (!isBuildForGPS()) {
iconOverlay {
enabled = true
fontSize = 8
textColor = [255, 255, 255, 255]
verticalLinePadding = 2
backgroundOverlayColor = [0, 0, 0, 180]
text = { "$iconOverlayText" }
}
}
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
flavorDimensions("store", "3d_enabled")
productFlavors {
demoapp {
dimension = "store"
applicationId getApplicationId()
// Dupe required because SearchRecentSuggestionsProvider has no available context in its constructor
buildConfigField "String", "SEARCH_RECENT_AUTHORITY", "\"${applicationId}.WFSearchRecentSuggestionsProvider\""
// And one for the manifest
resValue "string", "SEARCH_RECENT_AUTHORITY", "${applicationId}.WFSearchRecentSuggestionsProvider"
// These need to be defined as string resources so they can be registered in the manifest.
// However in order to access them in tests it's convenient to have them also defined in BuildConfig
String deepLinkScheme = "demoappapp"
resValue "string", "EXPLICIT_DEEP_LINK_SCHEME1", deepLinkScheme
resValue "string", "EXPLICIT_DEEP_LINK_SCHEME2", deepLinkScheme
buildConfigField "String", "EXPLICIT_DEEP_LINK_SCHEME1", "\"${deepLinkScheme}\""
buildConfigField "String", "EXPLICIT_DEEP_LINK_SCHEME2", "\"${deepLinkScheme}\""
if (getBranchName().toUpperCase() == 'MASTER') {
manifestPlaceholders = [
appIcon : "#mipmap/ic_launcher",
appRoundIcon: "#mipmap/ic_launcher_round"
]
} else {
manifestPlaceholders = [
appIcon : "#mipmap/ic_launcher_dev",
appRoundIcon: "#mipmap/ic_launcher_dev_round"
]
}
}
demoappflavor {
dimension = "store"
applicationId getDemoappflavorApplicationId()
// Dupe required because SearchRecentSuggestionsProvider has no available context in its constructor
buildConfigField "String", "SEARCH_RECENT_AUTHORITY", "\"${applicationId}.WFSearchRecentSuggestionsProvider\""
// And one for the manifest
resValue "string", "SEARCH_RECENT_AUTHORITY", "${applicationId}.WFSearchRecentSuggestionsProvider"
// These need to be defined as string resources so they can be registered in the manifest.
// However in order to access them in tests it's convenient to have them also defined in BuildConfig
String deepLinkScheme1 = "theapp"
String deepLinkScheme2 = "demoappflavorapp"
resValue "string", "EXPLICIT_DEEP_LINK_SCHEME1", deepLinkScheme1
resValue "string", "EXPLICIT_DEEP_LINK_SCHEME2", deepLinkScheme2
buildConfigField "String", "EXPLICIT_DEEP_LINK_SCHEME1", "\"${deepLinkScheme1}\""
buildConfigField "String", "EXPLICIT_DEEP_LINK_SCHEME2", "\"${deepLinkScheme2}\""
manifestPlaceholders = [
appIcon : "#mipmap/ic_launcher",
appRoundIcon: "#mipmap/ic_launcher_round"
]
}
}
android.variantFilter { variant ->
def store = variant.getFlavors().get(0).name
def tango_enabled = variant.getFlavors().get(1).name
// Disable building the tango dimension
if ((store == 'demoappflavor' || !isTangoBuild()) && tango_enabled == "tangoEnabled") {
variant.setIgnore(true)
}
}
buildTypes {
release {
// Commented out to stop a production build crash with OKIO
//minifyEnabled true
// Commented out due to Gradle 2.2.0 issue
//shrinkResources true
ext.enableCrashlytics = true
apply plugin: 'signing'
signingConfig signingConfigs.release
ext.betaDistributionReleaseNotesFilePath = './release-notes.txt'
ext.betaDistributionGroupAliases = 'android-nightly'
buildConfigField 'boolean', 'OVERRIDE_MIN_VERSION', isBuildForGPS() ? 'false' : 'true'
buildConfigField 'boolean', 'ENABLE_APPSEE', isBuildForGPS() ? 'true' : 'false'
}
debug {
// TODO: Uncomment this out to enable code coverage
// testCoverageEnabled true
buildConfigField 'boolean', 'OVERRIDE_MIN_VERSION', isBuildForGPS() ? 'false' : 'true'
buildConfigField 'boolean', 'ENABLE_APPSEE', isBuildForGPS() ? 'true' : 'false'
}
configurations.all {
resolutionStrategy {
force 'com.google.code.findbugs:jsr305:1.3.9'
force 'com.google.guava:guava:21.0'
force "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
force 'com.google.code.gson:gson:2.8.2'
}
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'bin/AndroidManifest.xml'
exclude 'bin/jarlist.cache'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
return 0
}
testOptions {
// Disable animations for UI testing
animationsDisabled = true
unitTests.returnDefaultValues = true
unitTests.all {
jacoco {
includeNoLocationClasses = true
}
}
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
}
configurations {
demoappTangoEnabledCompile
}
allprojects {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
maven { url "https://clojars.org/repo/" }
}
}
ext.daggerVersion = '2.11'
ext.playServicesVersion = '11.6.2'
ext.supportLibsVersion = '27.0.1'
ext.robolectricVersion = '3.3.2'
ext.frescoVersion = '1.5.0'
ext.leakCanaryVersion = '1.5.4'
ext.roomVersion ='1.0.0'
ext.butterKnifeVersion ='8.7.0'
ext.espressoVersion ='3.0.1'
ext.gsonVersion ='2.8.2'
ext.mockitoVersion ='2.9.0'
ext.dexmakerVersion ='1.2'
ext.icepickVersion ='3.2.0'
ext.multidexVersion ='1.0.2'
dependencies {
// Compile Project
compile(project(':models'))
// Compile
compile('com.crashlytics.sdk.android:crashlytics:2.7.1#aar') {
transitive = true
}
// GOOGLE LIBS
compile "com.android.support:design:${supportLibsVersion}"
compile "com.android.support:recyclerview-v7:${supportLibsVersion}"
compile "com.android.support:cardview-v7:${supportLibsVersion}"
compile "com.android.support:support-v4:${supportLibsVersion}"
compile "com.android.support:support-v13:${supportLibsVersion}"
compile "com.android.support:percent:${supportLibsVersion}"
compile "com.android.support:support-annotations:${supportLibsVersion}"
compile "com.android.support:appcompat-v7:${supportLibsVersion}"
compile "com.android.support:multidex:${multidexVersion}"
compile "com.google.code.gson:gson:${gsonVersion}"
// FIREBASE AND PLAY SERVICES
compile "com.google.firebase:firebase-messaging:${playServicesVersion}"
compile "com.google.firebase:firebase-appindexing:${playServicesVersion}"
//noinspection GradleDependency
compile "com.google.android.gms:play-services-analytics:${playServicesVersion}"
//noinspection GradleDependency
compile "com.google.android.gms:play-services-auth:${playServicesVersion}"
//noinspection GradleDependency
compile "com.google.android.gms:play-services-wallet:${playServicesVersion}"
// FLOW LAYOUT FOR CHIPS
compile 'org.apmem.tools:layouts:1.10#aar'
// RxJava and related
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile project(':FloatingSearchView')
// RxJava support for Room
compile 'android.arch.persistence.room:rxjava2:1.0.0'
// Testing support
androidTestCompile 'android.arch.core:core-testing:1.1.0'
// Dagger
compile "com.google.dagger:dagger:${daggerVersion}"
compile "com.google.dagger:dagger-android-support:${daggerVersion}"
kapt "com.google.dagger:dagger-compiler:${daggerVersion}"
kapt "com.google.dagger:dagger-android-processor:${daggerVersion}"
compile ("com.facebook.fresco:imagepipeline-okhttp3:${frescoVersion}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
compile "com.jakewharton:butterknife:${butterKnifeVersion}"
kapt "com.jakewharton:butterknife-compiler:${butterKnifeVersion}"
compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4'
compile 'com.appsee:appsee-android:2.3.4'
compile 'commons-io:commons-io:2.5'
compile "com.android.support.test:runner:1.0.1"
// ESPRESSO
androidTestCompile "com.android.support:support-v4:${supportLibsVersion}"
androidTestCompile "com.android.support:support-annotations:${supportLibsVersion}"
androidTestCompile "com.android.support:recyclerview-v7:${supportLibsVersion}"
androidTestCompile "com.android.support:design:${supportLibsVersion}"
androidTestCompile "com.android.support:recyclerview-v7:${supportLibsVersion}"
androidTestCompile "com.android.support.test.espresso:espresso-core:${espressoVersion}"
androidTestCompile "com.android.support.test.espresso:espresso-intents:${espressoVersion}"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:${espressoVersion}"
androidTestCompile "com.android.support.test.espresso:espresso-web:${espressoVersion}"
androidTestCompile "org.mockito:mockito-core:${mockitoVersion}"
androidTestCompile "com.google.dexmaker:dexmaker:${dexmakerVersion}"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:${dexmakerVersion}"
androidTestCompile "com.android.support:multidex:${multidexVersion}"
androidTestCompile "org.jetbrains.kotlin:kotlin-stdlib:${kotlinVersion}"
androidTestCompile "org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}"
androidTestCompile "com.google.code.gson:gson:${gsonVersion}"
androidTestCompile('com.jakewharton.espresso:okhttp3-idling-resource:1.0.0') {
//Using App Version Instead
exclude group: 'com.squareup.okio', module: 'okio'
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
//WireMock
androidTestCompile( project(":wiremock")) {
//Using Android Version Instead
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
exclude group: 'org.ow2.asm', module: 'asm'
//Using Android Version Instead
exclude group: 'org.json', module: 'json'
exclude group: 'com.google.guava', module: 'guava'
}
androidTestCompile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:${mockitoVersion}"
testCompile 'org.hamcrest:hamcrest-library:1.3'
testCompile "org.robolectric:robolectric:${robolectricVersion}"
testCompile "org.robolectric:shadows-multidex:${robolectricVersion}"
testCompile "org.robolectric:shadows-support-v4:${robolectricVersion}"
testCompile "com.google.code.gson:gson:${gsonVersion}"
testCompile "com.google.dagger:dagger:${daggerVersion}"
testCompile "com.android.support:multidex:${multidexVersion}"
kaptTest "com.google.dagger:dagger-compiler:${daggerVersion}"
// Kotlin mockito
testCompile ("com.nhaarman:mockito-kotlin-kt1.1:1.5.0"){
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib'
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
compile "frankiesardo:icepick:${icepickVersion}"
provided "frankiesardo:icepick-processor:${icepickVersion}"
compile "com.facebook.fresco:fresco:${frescoVersion}"
compile "com.facebook.fresco:animated-gif:${frescoVersion}"
// Canary
debugCompile "com.squareup.leakcanary:leakcanary-android:${leakCanaryVersion}"
releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}"
androidTestCompile "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}"
testCompile "com.squareup.leakcanary:leakcanary-android-no-op:${leakCanaryVersion}"
compile 'com.prolificinteractive:material-calendarview:1.4.3'
//Card IO
compile 'io.card:android-sdk:5.5.1'
compile 'com.facebook.rebound:rebound:0.3.8'
//Chrome Web-View
compile "com.android.support:customtabs:${supportLibsVersion}"
def folder = new File('./brickkit-android/BrickKit/bricks/build.gradle')
if (folder.exists()) {
compile project(':bricks')
} else {
compile 'com.demoapp:brickkit-android:0.9.27'
}
// intellij annotations are included in kotlin, so this creates
// a "java.util.zip.ZipException: duplicate entry" on its modules
//compile 'com.intellij:annotations:12.0#jar'
testCompile "com.google.dagger:dagger:${daggerVersion}"
demoappTangoEnabledCompile project(':demoappView')
compile ('com.perimeterx.sdk:msdk:1.1.0') {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
// Kotlin support
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
compile "org.jetbrains.kotlin:kotlin-reflect:$kotlinVersion"
//Data binding
kapt "com.android.databinding:compiler:$gradleVersion"
compile 'nl.littlerobots.rxlint:rxlint:1.4'
// Room
compile "android.arch.persistence.room:runtime:${roomVersion}"
kapt "android.arch.persistence.room:compiler:${roomVersion}"
androidTestCompile "android.arch.persistence.room:testing:${roomVersion}"
// Stetho
compile 'com.facebook.stetho:stetho:1.5.0'
compile 'com.facebook.stetho:stetho-js-rhino:1.4.2'
compile 'com.siftscience:sift-android:0.9.10'
}
import groovy.json.JsonBuilder
task uploadapithingFiles << {
// Get all the apithing files
String apithingDir = "${project.rootDir}/models/src/main/java/com/demoapp/models/requests/apithing-files"
FileTree files = fileTree(dir: apithingDir)
// TreeMap of File names and File Data
TreeMap<String, String> fileMap = new TreeMap<>()
files.each { file ->
Scanner input = new Scanner(file)
String output = ""
while (input.hasNext()) {
output = output + input.nextLine()
}
input.close()
fileMap.put(file.name.take(file.name.lastIndexOf('.')), output)
}
// Build request JSON
def json = new JsonBuilder()
json{
payload {
build_id getVersionCode()
client_id 2
version lastTaggedVersion
is_production isBuildForGPS() ? 1 : 0
queries fileMap.collect {
[
"name": it.key,
"query": it.value
]
}
}
}
// Create POST Request
def url = 'http://services.demoapp.com:8280/purest/performance/app_apithing'
def post = new URL(url).openConnection()
def message = json.toString()
projects.logger.debug(message)
post.setRequestMethod("POST")
post.setRequestProperty("Content-Type", "application/json; charset=utf-8")
post.setDoOutput(true)
post.getOutputStream().write(message.getBytes("UTF-8"))
def responseCode = post.getResponseCode()
if(responseCode >= 200 && responseCode <= 299) {
projects.logger.lifecycle("apithing upload successful.")
} else {
projects.logger.lifecycle("apithing Upload Failed with response: ${post.getResponseMessage()}")
throw new GradleException("ERROR: Unable to upload apithing files. Server returned a response code: ${responseCode}")
}
}
project.gradle.taskGraph.whenReady {
->
project.tasks.findAll { it.name =~ /connected.+AndroidTest/ }.each {
it.ignoreFailures = true
}
}
def errorOutput(dir) {
println "\n\n================OUTPUT REPORT=================\n\n"
println "The file $dir does not exist\n "
println "Check the current APK Path and Version "
println "\n\n==============================================\n\n"
}
def executeUpload(dir, isUpload, key) {
if (isUpload) {
exec {
commandLine 'curl', 'https://tok_gh6e7yrydkhgqyaz2fvx702y8m#api.appetize.io/v1/apps', '-F', "file=#$dir", '-F', 'platform=android'
}
} else {
exec {
commandLine 'curl', "https://tok_gh6e7yrydkhgqyaz2fvx702y8m#api.appetize.io/v1/apps/$key", '-F', "file=#$dir", '-F', 'platform=android'
}
}
}
apply plugin: 'com.google.gms.google-services'
jacocoAndroidUnitTestReport {
excludes += ['**/R.class','**/R$*.class','**/*$ViewInjector*.*','**/*$ViewBinder*.*','**/BuildConfig.*','**/Manifest*.*','**/*$Lambda$*.*','**/*Module.*','**/*Dagger*.*','**/*MembersInjector*.*','**/*_Provide*Factory*.*','**/*_Factory*.*','**/*$*$*.*','**/test*/**','**/androidTest/**','**/databinding/**']
}
tasks.whenTaskAdded { task ->
if (task.name == 'connecteddemoappDebugAndroidTest' || task.name == 'connecteddemoappflavorDebugAndroidTest') {
task.doFirst() {
exec {
commandLine 'sh', 'get_android_wiremock.sh'
}
}
task.doLast() {
exec {
commandLine 'sh', 'get_device_recordings.sh', 'wiremock_blacklist.txt'
}
}
}
}
android.applicationVariants.all { variant ->
task("checkstyle${variant.name.capitalize()}", type: Checkstyle) {
configFile file("${project.rootDir}/config/quality/checkstyle/checkstyle.xml")
configProperties.checkstyleSuppressionsPath = file("${project.rootDir}/config/quality/checkstyle/suppressions.xml").absolutePath
source 'src'
include '**/*.java'
exclude '**/gen/**'
exclude '**/models/generated/**'
classpath = files() as FileCollection
group = "verification"
}
check.dependsOn("checkstyle${variant.name.capitalize()}")
}
android.applicationVariants.all { variant ->
task("findbugs${variant.name.capitalize()}", type: FindBugs) {
ignoreFailures = false
effort = "default"
reportLevel = "medium"
excludeFilter = new File("${project.rootDir}/config/quality/findbugs/findbugs-filter.xml")
classes = files("${project.rootDir}/app/build/intermediates/classes")
source = fileTree('src/main/java/')
classpath = files()
group = "verification"
reports {
xml.enabled = false
html.enabled = true
xml {
destination "$project.buildDir/findbugs/findbugs-output.xml"
}
html {
destination "$project.buildDir/findbugs/findbugs-output.html"
}
}
dependsOn "compile${variant.name.capitalize()}JavaWithJavac"
}
check.dependsOn("findbugs${variant.name.capitalize()}")
}
android.applicationVariants.all { variant ->
task("buildCheckTest${variant.name.capitalize()}", type: GradleBuild) {
dependsOn "checkstyle${variant.name.capitalize()}"
dependsOn "assemble${variant.name.capitalize()}"
dependsOn "lint${variant.name.capitalize()}"
dependsOn "findbugs${variant.name.capitalize()}"
dependsOn "test${variant.name.capitalize()}UnitTest"
dependsOn "jacocoTest${variant.name.capitalize()}UnitTestReport"
mustRunAfter "clean"
}
}
task buildReleaseTest(type: GradleBuild)
android.applicationVariants.all { variant ->
if (variant.name.endsWith("Release")) {
buildReleaseTest.dependsOn("buildCheckTest${variant.name.capitalize()}")
}
}
android.applicationVariants.all { v ->
if (v.buildType.name == "release"){
v.assemble.dependsOn("uploadapithingFiles")
}
}
buildReleaseTest.mustRunAfter('clean')
task cleanBuildReleaseTest(type: GradleBuild)
cleanBuildReleaseTest.dependsOn('clean')
cleanBuildReleaseTest.dependsOn('buildReleaseTest')
Whoa, that's a lot of plugins. The error message comes because one of them depends on an API that was changed in 3.x (hence migration link). You'll need to update the corresponding plugin.
Based on gradle dependencies run at step 3 below, my guess is gradle.plugin.com.nobleworks_software.icon-overlay:icon-overlay-plugin. It shows com.android.tools.build:gradle:2.2.3 -> 3.0.1 nested under.
(gradlew buildEnvironment on your project may also give you a similar result if you add 3.0.1 Android Gradle Plugin to your build.gradle)
--stacktrace should have also given you the exact location where com.android.build.gradle.tasks.ManifestProcessorTask#getManifestOutputFile was called from. If it doesn't you can still do below:
This is a way I would answer this question if I had an mvce. You have too many plugins to set up everything manually from scratch. This method is also applicable to any Gradle buildscript weirdness in plugins.
Close your project in AS/IDEA
Note: if you have a buildSrc in your project that could also work, so there's no need to close and create another.
Create another project (let's call it debugger) with build.gradle file as follows:
buildscript {}'s contents from your project (but no buildscript block!)
repositories {
jcenter()
...
}
dependencies {
implementation ... // for each classpath ...
}
add plugins { id 'java' } on root level
add implementation 'com.android.tools.build:gradle:3.0.1' inside dependencies
Open this project in IDEA/Android Studio
(this will download all the sources for those plugins)
Run gradlew --no-daemon -Dorg.gradle.debug=true --debug --stacktrace from terminal
(this will stop executing and wait,
note: Listening for transport dt_socket at address: 5005)
Go to "Run/Debug configurations" dialog in debugger project
Create a "Remote" configuration (green +) with parameters from Gradle's log:
Close dialog with OK / Apply
Jump to class/file com.android.build.gradle.tasks.ManifestProcessorTask inside gradle-core-3.0.1-sources.jar
Put a breakpoint at line 128 in ManifestProcessorTask.getManifestOutputFile
To be safe repeat above two steps for gradle-core-3.0.1.jar's decompiled code as well. (line number may differ)
Run menu > Debug "Remote"
Wait and celebrate when breakpoint hits and you see who calls the method in the Debug view's Stack frame listing.
It looks overwhelming, but it's quite simple and fast after you did this once.

how to add Paho-MQTT to android studio

I am trying to use Paho-MQTT in android studio. I referred to this link
and I should add the following to gradle files
the link demands adding the following:
repositories {
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
dependencies {
compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
exclude module: 'support-v4'
}
}
the text did not specify which gradle file I use use "gradle-proj or gradle-app", so I tried both and in either cases i received errors such as
Error:(14, 0) Could not find method compile() for arguments [org.eclipse.paho:org.eclipse.paho.android.service:1.0.2, build_9fu4g5nmegp97bvhjazm7s8o8$_run_closure1$_closure3$_closure5#6dff2815] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
please let me know which gradle file i should use "proj or app"? and how to add the previous code correctly to gradle?
build.gradle app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.alten.test_pahomqtt_1"
minSdkVersion 15
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'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
//compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
//compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
//provided 'com.google.android.things:androidthings:0.2-devpreview'
//provided 'com.google.android.things:androidthings:0.1-devpreview'
//compile('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') { exclude module: 'support-v4' }
compile files('libs/org.eclipse.paho.android.service-1.0.2.jar')
compile files('libs/org.eclipse.paho.client.mqttv3-1.0.2.jar')
}
build.gradle project:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" }
maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://repo.eclipse.org/content/repositories/paho-snapshots/" }
maven { url "https://repo.eclipse.org/content/repositories/paho-releases/" }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
current error
In your app you should add:
dependencies {
. . .
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.1.0'
compile 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
}
In your proj:
buildscript {
repositories {
. . .
maven {
url "https://repo.eclipse.org/content/repositories/paho-releases/"
}
}
}
dont forget about adding a service to your manifest under application tag:
<service
android:name="org.eclipse.paho.android.service.MqttService"
android:exported="false" />
Those two lines
compile files('libs/org.eclipse.paho.android.service-1.0.2.jar')
compile files('libs/org.eclipse.paho.client.mqttv3-1.0.2.jar')
will not work until libs folder dont contains this jars. If you want to stick to this approach (copying jars) you can find them here:
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.android.service/
https://repo.eclipse.org/content/repositories/paho-releases/org/eclipse/paho/org.eclipse.paho.client.mqttv3/

Apache commons dependency not excluded in gradle | Android

I have a library project (Janrain:Jump) that uses the org.apache.http.legacy library. When I try to build my project I get the duplicate error like following:
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/commons/codec/StringEncoderComparator.class
So I figured that the org.apache.commons are duplicate entry because Janrain uses it and it's also included in the Android 24 (as an external library). So I tried to remove the commons from the :Jump gradle like:
configurations {
all*.exclude group: 'org.apache.commons'
}
Now, I'd expect that this removes org.apache.commons but I still get the duplicate entry gradle error.
Here is the :Jump gradle file
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
//If building with strict Android 6.0 the following will need to be uncommented
//See: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html
//And: http://stackoverflow.com/questions/31653002/how-to-use-the-legacy-apache-http-client-on-android-m
useLibrary "org.apache.http.legacy"
defaultConfig {
minSdkVersion 17
targetSdkVersion 24
// replace the below string with your own Google client ID. Make sure this is consistent
// with the values used in openid_appauth_idp_configs.xml
manifestPlaceholders = [
<my values>]
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
configurations {
all*.exclude group: 'org.apache.commons'
}
dependencies {
compile 'com.android.support:support-v4:24.2.0'
compile files('libs/org.apache.http.legacy.jar')
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'com.squareup.okhttp:okhttp-apache:2.5.0'
compile 'com.squareup.okio:okio:1.6.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit:retrofit:1.8.0'
compile 'net.openid:appauth:0.4.1'
}
allprojects {
repositories {
jcenter()
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
Why is the org.apache.commons not excluded even when I include it in configurations?
Just add this task to your build.gradle then run gradle findDuplicates to find the jars
task findDuplicates {
doLast {
def findMe = 'org/apache/commons/codec/StringEncoderComparator.class'
configurations.runtime.asFileTree.matching {
include '**/*.jar'
}.files.each { File jarFile ->
zipTree(jarFile).visit { FileVisitDetails fvd ->
if (fvd.path == findMe) {
println "Found $findMe in $jarFile.name"
}
}
}
}
}

How to define different dependencies for different product flavors

I am converting one of my apps to Gradle and would like to use the new build flavor features to have a paid and a free ad based flavor.
I want only the ad based version to depend on the admob SDK.
My build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
productFlavors {
Pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
Free {
dependencies {
}
}
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0#aar'
compile fileTree(dir: 'libs', include: '*.jar')
}
Is there a way to configure the dependency in the free product flavor to have its own libs folder that is merged with the main libs folder that contains general libraries for both flavors?
If this is possible how would I define this folder?
To define a flavor specific dependency you can use proCompile instead of compile in your dependency section. When you run gradle properties you get an overview of automatic created configurations.
The correct build file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 22
}
productFlavors {
pro {
packageName "de.janusz.journeyman.zinsrechner.pro"
}
free { }
}
}
dependencies {
compile 'com.android.support:support-v4:22.2.0'
freeCompile 'com.google.android.gms:play-services-ads:7.5.0'
}
Fast forward to mid-2018. You will need to add flavorDimensions.
android {
...
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "dimensionName"
productFlavors {
pro {
dimension "dimensionName"
}
free {
dimension "dimensionName"
}
}
}
dependencies {
implementation 'com.android.support:support-v4:22.2.0'
freeImplementation 'com.google.android.gms:play-services-ads:15.0.1'
}
Also, take note:
Configuration 'compile' is obsolete and has been replaced with
'implementation' and 'api'. It will be removed at the end of 2018. For
more information see:
http://d.android.com/r/tools/update-dependency-configurations.html
Simple:
dependencies {
....
....
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("free")) {
implementation 'com.google.android.gms:play-services-ads:17.2.0'
}
}
....
....
}
or just:
FreeImplementation 'com.google.android.gms:play-services-ads:17.2.0'
You need to manually add configuration for each flavor. Example
configurations {
proCompile
freeCompile
}
dependencies {
compile 'com.parse.bolts:bolts-tasks:1.3.0'
proCompile 'com.android.support:design:23.1.1'
freeCompile 'com.parse:parse-android:1.12.0'
}
Edit: I recommend using one of the other techniques!
An alternative to the accepted answer is this:
ext {
flavorType = ""
}
gradle.startParameter.getTaskNames().each { task ->
if(task.contains("flavor1")){
flavorType = "flavor1"
} else if (task.contains("flavor2")){
flavorType = "flavor2"
} else {
flavorType = "flavor3"
}
}
if(flavorType == 'flavor1' || flavorType == 'flavor2') {
compile 'com.android.support:support-v4:18.0.+'
}
If you use Gradle with Kotlin, dependencies for custom flavors can be added as follows:
dependencies {
"freeImplementation"("com.google.android.gms:play-services-ads:7.5.0")
}

Categories

Resources