Advantage of 'implementation' in build.gradle - gradle

Say I have a project, ProjectA, containing ClassInProjectA which I build into an .aar.
I also have a project, ProjectB, which uses ClassInProjectA internally.
public class ClassInProjectB {
public ClassInProjectB() {
new ClassInProjectA();
}
}
The build.gradle of ProjectB is as follows:
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
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 'com.android.support:appcompat-v7:28.0.0'
implementation(name:'projA', ext:'aar')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
I use implementation because ClassInProjectA is used only internally by ProjectB.
I then have a main app and wish to use ClassInProjectB.
The main app's build.gradle is as follows:
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.project.projectc"
minSdkVersion 15
targetSdkVersion 28
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(name:'projB', ext:'aar')
implementation(name:'projA', ext:'aar')
implementation 'com.android.support:appcompat-v7:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
projB needs to be imported explicitly or I am unable to construct ClassInProjectB, due to the requirement for ClassInProjectA.
All is well, all compiles and ClassInProjectB can be constructed.
If the build.gradle of ProjectB is changed such that projA is an api requirement:
api(name:'projA', ext:'aar')
nothing changes in the build.gradle of the main app.
What is the advantage of the implementation keyword in this context? Is it purely that it allows me to replace ProjectB in the main app and avoid triggering a recompilation of Project A?
I'm also a little confused by this post, and this one, each of which imply that the main project shouldn't actually need to implicitly import ProjectA at all if ProjectB depends on ProjectA via the api keyword. Have I misunderstood something? edit: this would seem to be the case only if the dependencies are via submodules in the same project rather than exported .aar.

What is the advantage of the implementation keyword in this context? Is it purely that it allows me to replace ProjectB in the main app and avoid triggering a recompilation of Project A?
The only advantage of the implementation keyword, and indeed the reason why it was introduced, is that it can avoid full project rebuilds. This blogpost does a great job at explaining the details. To summarize, if :projectA has an implementation dependency on :projectB, then :projectA only needs to be recompiled if the interface of :projectB changes.

Related

Build failed - Could not find androidx.lifecycle:compiler:2.2.0

I am totally new to Android and struggling past 2days with my build.
I get the following message
Build failed
Could not find androidx.lifecycle:compiler:2.2.0.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/androidx/lifecycle/compiler/2.2.0/compiler-2.2.0.pom
- https://jcenter.bintray.com/androidx/lifecycle/compiler/2.2.0/compiler-2.2.0.pom
Required by: project :app
I updated my studio and gradle plugins and versions of dependencies, but none seem to work. I am using Gradle Plugin: 4.0.1 and Gradle: 6.1.1
My project gradle is as follows
buildscript {
repositories {
google()
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And my app gradle is as follows
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.xxxx.xxxxxx"
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
def room_version = "2.2.0"
def lifecycle_version = "2.2.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// Lifecycles only (without ViewModel or LiveData)
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
// Saved state module for ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version"
// Annotation processor
annotationProcessor "androidx.lifecycle:compiler:$lifecycle_version"
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}
In the idea.log i find the line
testKnownPluginVersionProvider - 'gradle' plugin missing from the offline Maven repo, will use default 4.0.1
repeating regularly and the gradle-wrapper properties file is as follows
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
Remove the following line if you are using Java 8:
annotationProcessor "androidx.lifecycle:compiler:$lifecycle_version"

NoClassDefFoundError: error when generating apk

migrate my application to android x and gradle 5
the moment that I send to generate the apk or to install gives me this error
Caused by: java.lang.NoClassDefFoundError: com/android/tools/r8/dex/ApplicationReader
Build.graddle
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven { url 'https://maven.fabric.io/public' }
google()
}
android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
vectorDrawables.useSupportLibrary = true
applicationId "gortiz.com.goapp"
minSdkVersion 18
targetSdkVersion 28
versionCode 37
versionName '2.0.37'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation('com.crashlytics.sdk.android:crashlytics:2.7.1#aar') {
transitive = true;
}
implementation 'androidx.appcompat:appcompat:1.+'
implementation 'androidx.vectordrawable:vectordrawable-animated:1.+'
implementation 'androidx.mediarouter:mediarouter:1.+'
implementation 'androidx.core:core:1.+'
implementation 'androidx.multidex:multidex:2.+'
implementation 'com.google.android.material:material:1.+'
implementation 'commons-io:commons-io:2.5'
implementation 'androidx.constraintlayout:constraintlayout:1.+'
implementation 'androidx.legacy:legacy-support-v4:1.+'
implementation 'com.google.code.gson:gson:2.8.1'
implementation 'com.github.satyan:sugar:1.5'
implementation 'com.squareup.picasso:picasso:2.5.0'
implementation 'me.panavtec:wizard:1.2'
implementation 'com.github.johnkil.print:print:1.2.+'
implementation 'com.github.bmelnychuk:atv:1.2.+'
implementation 'androidx.legacy:legacy-support-v13:1.0.0'
testImplementation 'junit:junit:4.12'
implementation 'androidx.recyclerview:recyclerview:1.+'
implementation ('com.google.firebase:firebase-messaging:17.3.4'){
exclude group: 'com.google.firebase', module: 'firebase-iid'
}
implementation 'com.google.android.gms:play-services-maps:17.+'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.google.android.gms:play-services-location:16.0.0'
implementation 'com.google.android.gms:play-services-analytics:16.0.7'
implementation 'com.google.android.gms:play-services-places:16.0.0'
implementation 'com.google.android.gms:play-services-gcm:16.0.0'
implementation 'com.google.android.gms:play-services-base:16.1.0'
implementation 'com.google.firebase:firebase-core:16.0.7'
implementation 'com.google.android.gms:play-services-vision:18.+'
implementation 'com.google.firebase:firebase-config:18.+'
implementation 'com.google.firebase:firebase-iid:17.0.2'
implementation 'androidx.annotation:annotation:1.1.0'
}
apply plugin: 'com.google.gms.google-services'
graddle.properties
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.useAndroidX=true
android.enableJetifier=true
Caused by: java.lang.NoClassDefFoundError: com/android/tools/r8/dex/ApplicationReader
The top level build.gradle seems to be missing a dependency on Android Gradle Plugin. Please add something like this (depending on the version you would like to use):
buildscript {
...
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
...
}
...
}
If you create an empty project in Android Studio that should all be in place.

Unable to resolve dependency for ':app#debugUnitTest/compileClasspath', :app#debugAndroidTest/compileClasspath

Situation:
Create the simplest project
Add to the project module as File -> New -> New module "Phone & Tablet Module"
Add the dependency on the module
And get errors:
Unable to resolve dependency for ':app#debug/compileClasspath': Could not resolve project :testmodule.
Unable to resolve dependency for ':app#debugAndroidTest/compileClasspath': Could not resolve project :testmodule.
Unable to resolve dependency for ':app#debugUnitTest/compileClasspath': Could not resolve project :testmodule.
Unable to resolve dependency for ':app#release/compileClasspath': Could not resolve project :testmodule.
Unable to resolve dependency for ':app#releaseUnitTest/compileClasspath': Could not resolve project :testmodule.
Below are the project files:
Project:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module: app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.pawga.test00"
minSdkVersion 14
targetSdkVersion 28
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(path: ':testmodule')
}
Module: testmodule
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.pawga.testmodule"
minSdkVersion 14
targetSdkVersion 28
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
gradle-wrapper.properties
#Mon Jun 25 22:51:52 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
settings.gradle
include ':app', ':testmodule'
Note:
If the module type is "Android Library" (Point "two" above), then there are no such errors.
For such a simple project, where everything is by default, there should not be such errors. What's wrong?
Try this, replace:
implementation project(':testmodule')
To:
implementation project(path:':testmodule', configuration: 'default')
Go to
File -> Settings -> Build, Execution, Deployment -> Gradle menu.
You will see "Offline work" options. Uncheck it. And it's ok.
It looks like your plugin needs to be changed to apply plugin: 'com.android.library' instead of 'com.android.application'
See this post for more
information. Hope that helps.
Try online mode for gradle. Hope it helps(I had the same problem, so more chances).
##REMOVE THE FOLLOWING FROM THE BUILD GRADLE##
>>implementation 'com.android.support:appcompat-v7:28.+'
>>implementation 'com.android.support.constraint:constraint-layout:1.0.2'
>>androidTestImplementation 'com.android.support.test:runner:1.0.1'
>>androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
>>SEE ATTACHED PICTURE
>>[STUDIO ERRORS][1]
>>[1]: https://i.stack.imgur.com/DY6sz.png

Getting errors while using Google Cloud Translation Client Libraries for Java (Android)

I'm Trying to make a google translation app using Google's Cloud Translation API. When i'm trying to compile compile 'com.google.cloud:google-cloud-translate:1.14.0' errors as Duplicate files copied in APK project.properties
My android code is as below (No problem in java code)
import com.google.cloud.translate.Translate;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.TranslateOptions;
import com.google.cloud.translate.Translation;
public class QuickstartSample {
public static void main(String... args) throws Exception {
Translate translate = TranslateOptions.getDefaultInstance().getService();
String text = "Hello, world!";
Translation translation =
translate.translate(
text,
TranslateOption.sourceLanguage("en"),
TranslateOption.targetLanguage("ru"));
System.out.printf("Text: %s%n", text);
System.out.printf("Translation: %s%n", translation.getTranslatedText());
}
}
My build.gradle is as below (problem arrises due to below given compile statement)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.googletranslation"
minSdkVersion 22
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.google.cloud:google-cloud-translate:1.14.0'**
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
testCompile 'junit:junit:4.12'
}
Im getting erros as Below:
Error:Conflict with dependency 'com.google.code.findbugs:jsr305' in project ':app'. Resolved versions for app (3.0.0) and test app (2.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Warning:WARNING: Dependency org.apache.httpcomponents:httpclient:4.0.1 is ignored for release as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
Warning:WARNING: Dependency org.json:json:20160810 is ignored for release as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
I have tried all methods in below link but there is no effect. still errors are increasing
Error:Conflict with dependency 'com.google.code.findbugs:jsr305'
One have to include these:
android {
packagingOptions {
exclude 'project.properties'
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/notice'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license'
exclude 'META-INF/license.txt'
}
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
includeCompileClasspath false
}
}}
}

error: cannot find symbol class for add kotlin class into java class in android studio 3.0 stable

i use android studio 3.0 and some old java class Convert Java to Kotlin. after that Kotlin class cant import in java class!
in below you can see my gradle and a picture of my error.
module build.grade
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.google.gms:google-services:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
in app level build.gradle in normally i use gradle code and in android 3.0 i think we need any thing
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.idehnavazan.beautifierclient"
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dataBinding {
enabled = true
}
}
repositories {
mavenCentral()
jcenter()
maven { url 'https://maven.google.com' }
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.github.rey5137:material:1.2.2'
compile 'com.android.support:cardview-v7:25.3.1'
}
apply plugin: 'com.google.gms.google-services'
To work with Kotlin files you need to add Kotlin to your project.
project/build.gradle
buildscript {
ext.kotlinVersion = '1.1.51'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
project/module/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
}
What's next
Since you're using support library 25.3.1 and Android plugin 3.0.0 your next question will probably be this: style attribute '#android:attr/windowEnterAnimation' not found.
I have added all kotlin classes in separate folder and it work for me!
Keep your java classes separate from kotlin. I was having
app:compileDebugJavaWithJavac Kotlin class calling from java: error: cannot find symbol
compile time error.
My issue was that I have imported a submodule using
testImplementation instead of implementation inside of module gradle file that is declaring your module as a dependency.
Changing the dependency declaration fixed it for me.
I am also new to kotlin/android and was using ViewBinding. I was renaming an xml-file when this type of error occured. I just added:
dataBinding {
enabled = true
}
to the build.gradle(module)-file and it now works. To be honest, I never understood the difference between view- and data-binding
kotlin/android and was using ViewBinding.
just add this inside build.gradle(module)-file
viewBinding{
enabled = true
}

Resources