I'm developing an app and I want to separate the Android specific logic from the business logic ( "immutable" logic and Android independent), so I created two modules:
app : Android specific code
domain : Business logic ( written in Kotlin )
I'm using this build.gradle file for the kotlin module
apply plugin: 'kotlin'
kapt {
generateStubs = true
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
/* Annotations library */
provided 'org.glassfish:javax.annotation:10.0-b28'
kapt 'com.google.dagger:dagger-compiler:2.0.1'
/* Dagger 2 library */
compile 'com.google.dagger:dagger:2.0.1'
/* EventBus library */
compile 'de.greenrobot:eventbus:2.4.0'
/* JODA TIME - time and date library */
compile 'joda-time:joda-time:2.9.2'
/*Rx Kotlin*/
compile 'io.reactivex:rxkotlin:0.55.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.0.47-beta'
}
buildscript {
ext.kotlin_version = '1.0.1-2'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
maven{ url "http://repository.jetbrains.com/all" }
}
Is this the correct way to configure the kotlin module?
PLUS: I'm getting a Gradle DSL method not found: 'provided()' error. How could I fix it?
Gradle doesn't have provided configuration by default. Instead it has compileOnly. So if you really want a dependency to be available only during compilation do the following:
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
Related
I'm hoping that this is just something I'm doing wrong here. I'm trying to use Dagger 2.0 to inject dependencies for my JUnit tests (not Espresso tests, just pure JUnit). So, I have a 'main' java module and a 'test' java module. In the main module, I've got a Dagger Module and a Component:
#Module
public class MainModule {
#Provides
public Widget provideWidget() {
return new ConcreteWidget();
}
}
...
#Component (modules = MainModule.class)
public interface MainComponent {
void inject(WidgetConsumer consumer);
}
And in my test module, I have the following:
#Module
public class TestModule {
#Provides public Widget provideWidget() {
return new Widget() {
#Override
public void doThing() {
int y = 6;
y ++;
}
};
}
}
...
#Component(modules = TestModule.class)
public interface TestComponent extends MainComponent{
}
My build.gradle has dependencies that look like this:
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.2.0'
testCompile 'junit:junit:4.12'
compile 'com.google.dagger:dagger:2.9'
testCompile 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
testAnnotationProcessor 'com.google.dagger:dagger-compiler:2.9'
}
For whatever reason, Dagger generates DaggerMainComponent, but refuses to generate DaggerTestComponent. There appear to be no errors in the gradle output when I build.
Here's the thing... I think the annotation processor is running, but somehow the android gradle plugin is failing to pull in those generated sources during compile-time. I have inspected the app/build/generated/source/apt/test/ directory and found DaggerTestComponent.java in there, but for some reason, it's not imported as a dependency.
Any thoughts? Here is a link to a test project showing my issue
Add this to build.gradle after android DSL:
android {
...
}
android.applicationVariants.all {
def aptOutputDir = new File(buildDir, "generated/source/apt/${it.unitTestVariant.dirName}")
it.unitTestVariant.addJavaSourceFoldersToModel(aptOutputDir)
}
Thereafter, your test component would be recognized.
For Kotlin replace generated/source/apt/... with generated/source/kapt/...
There is an issue raised in the tracker concerning this.
I found a workaround, just in case anybody gets stuck with this issue in the future. It appears the testAnnotationProcessor command in the android gradle plugin does not work for test modules (possibly a bug in their implementation?). So you can write testAnnotationProcessor and your build.gradle will compile but it seems to not work properly.
The workaround is to fall back to the older third-party annotation processing plugin by Hugo Visser (android-apt).
To do that, add the following to your buildscript dependencies in your main build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-rc1'
// ADD THIS LINE HERE vvv
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
Then, in your individual module's build.gradle, add the following line at the top:
apply plugin: 'com.android.library'
// ADD THIS LINE HERE vvv
apply plugin: 'com.neenbedankt.android-apt'
Finally, instead of using testAnnotationProcessor and annotationProcessor, just use apt and testApt:
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.2.0'
compile 'com.google.dagger:dagger:2.9'
// USE apt INSTEAD OF annotationProcessor HERE vvv
apt 'com.google.dagger:dagger-compiler:2.9'
testCompile 'com.google.dagger:dagger:2.9'
// USE testApt INSTEAD OF testAnnotationProcessor HERE vvv
testApt 'com.google.dagger:dagger-compiler:2.9'
testCompile 'junit:junit:4.12'
}
Note that you must use the 1.8 version of android-apt, as the 1.4 version does not ship with the testApt command/function/whatever.
Depending on your test type:
insert testAnnotationProcessor "com.google.dagger:dagger-compiler:2.x" inside dependencies on your build.gradle file for src/test
or
insert androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.x" inside dependencies on your build.gradle file for src/androidTest
I had this error
Error:(45, 0) Gradle DSL method not found: 'implementation()'
Possible causes:<ul><li>The project 'LaTaxi2' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
Upgrade plugin to version 2.3.3 and sync project</li><li>The project 'LaTaxi2' may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
build.gradle content
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {
url 'https://maven.google.com'
}
// google()
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
/* CHANGE to classpath 'com.android.tools.build:gradle:2.3.3' for STABLE BUILD TOOL VERSION*/
// classpath 'com.android.tools.build:gradle:3.0.0-alpha7'
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.0'
// We recommend changing it to the latest version from our changelog:
// https://docs.fabric.io/android/changelog.html#fabric-gradle-plugin
classpath 'io.fabric.tools:gradle:1+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {
url 'https://maven.google.com'
}
// google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build .gradle module app
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
repositories {
maven {
url 'https://maven.google.com'
}
// google()
maven { url 'https://maven.fabric.io/public' }
}
android {
compileSdkVersion 25
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "in.techware.lataxi"
minSdkVersion 17
targetSdkVersion 25
versionCode 5
versionName "1.0.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
/* Remove This to remove Crashlytics and Fabric */
compile('com.crashlytics.sdk.android:crashlytics:2.6.7#aar') {
transitive = true;
}
/* compile('com.digits.sdk.android:digits:2.0.6#aar') {
transitive = true;
}*/
implementation 'com.android.support:appcompat-v7:25.4.0'
implementation 'com.android.support:design:25.4.0'
implementation 'com.android.support:recyclerview-v7:25.4.0'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.android.support:cardview-v7:25.4.0'
implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation 'com.google.android.gms:play-services-maps:11.0.2'
implementation 'com.google.android.gms:play-services-location:11.0.2'
implementation 'com.google.android.gms:play-services-places:11.0.2'
implementation 'com.google.firebase:firebase-auth:11.0.2'
implementation 'com.google.firebase:firebase-messaging:11.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta1'
implementation 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'
I had such a simple reason for this not working that I must post my answer to prevent anybody else going through this.
Don't put repositories and dependencies in the file called build.gradle (Project: YourProjectName)! There is a comment in that file that says:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Good job to whoever put that note in there. Instead, place your repositories and dependencies in the similarly named module file build.gradle (Module: app).
There should already be a dependencies function. You may need to add repositories function. In my case, it was:
repositories {
maven { url "https://jitpack.io" }
}
This should let you sync and recognize implementation.
To use the DSL implementation() you have to use:
The updated gradle plugin for Android 3.0.0
The gradle version 3.4 or later
Then in your build.gradle you have to use:
buildscript {
repositories {
...
// You need to add the following repository to download the
// new plugin.
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-beta1'
}
}
In your gradle-wrapper.properties
distributionUrl=\
https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
More detailed info here.
or if you don't want to update to the latest Gradle, go back to using DSL method compile() instead of implementation()
As a beginner, I discovered that there's 2 build.gradle files
It's worth checking that you didn't add the line in the wrong file.
I added mine in the project file whereas I should have added it in the module one.
You using Turkish workspace,
below change solves the problem
testImplementation -> testİmplementation,
androidTestImplementation -> androidTestİmplementation
androidTestImplementation -> androidTestİmplementation
implementation() has replaced compile() configuration, which will be DEPRECATED by the end of 2018.
In order to fix your errors and use it, you must update to Android Gradle Plugin 3.0.0 (or higher). As a result of this update, you also need to update Gradle to Gradle 4.1 (or higher). You also need to use Build Tools 26.0.2 (or higher), meaning to update your buildToolsVersion in your build.gradle, or just completely remove it from there (as, since 3.0.0, the minimum required version is used by default). You also need to update to Android Studio 3 (or higher) in order to use those advanced versions.
You can read about the changelog of android gradle plugin 3.0.0, the improved compile times, and more, here:
https://developer.android.com/studio/releases/gradle-plugin#3-0-0
So how to update Android Gradle Plugin and Gradle?
OPTION 1: Manually
In your build.gradle find your gradle classpath and update version to gradle 3.0. Also, google() maven's repository should be added:
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
}
}
In gradle-wrapper.properties find your distributionUrl and update to:
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
OPTION 2: Through project properties
(But notice the manual configuration WILL override it)
Go to File→Project Structure→Project
One last thing, you can also choose whether to replace compile() by implementation() or by api(). By default I would suggest just to use implementation(). You can read more about the differences here:
https://developer.android.com/studio/build/dependencies
In my case it was just a typo. It was an extra sign "/" after one of rows
I ran into similar errors. when I tried to include the recyclerview dependencies from may build.gradle(Module: app) with a code like so.
//other build.gradle(Module: App) code above
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.0-alpha7'
testCompile 'junit:junit:4.12'
implementation 'com.android.support:recyclerview-v7:25.3.1'
}
So the simple way I fixed this is by changing the code to this, where I changed the implement to compile while retaining every other code and their version numbers.
//other build.gradle(Module: App) code above
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.0-alpha7'
testCompile 'junit:junit:4.12'
compile 'com.android.support:recyclerview-v7:25.3.1'
}
I hope this helps some one having similar issues.
PS: The actual error was caused by implementation 'com.android.support:recyclerview-v7:25.3.1' and changing the code to compile 'com.android.support:recyclerview-v7:25.3.1' got it fixed.
in my case i add these line and issue resolved
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
I´m making an android app that uses dagger + butterknife + recycleviews + retrofit.
It all compiles and work wel until I add the butter knife dependencies (the ones related to the apt) in the gradle that after I sync, my Dagger Component is not found even if I clean and rebuild again.
But if I removed my butterknife dependencies (the ones related to the apt), sync and build, my Dagger component is found.
Here is my gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
and my app gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
compile 'com.google.dagger:dagger:2.7'
annotationProcessor 'com.google.dagger:dagger-compiler:2.7'
compile 'com.android.support:design:24.0.0-beta1'
compile 'com.android.support:support-v4:24.0.0-beta1'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'io.reactivex:rxjava:1.1.6'
compile 'io.reactivex:rxandroid:1.2.1'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
compile 'com.squareup.okhttp3:okhttp:3.4.2'
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
My dagger component:
DaggerMyComponent.builder().
myRetroModule(new MyRetroModule("581e710d3e0000da02c08e10")).
build().
inject(this); // instance
And without the apt dependencies, butterknife doesn´t work
What do I need to do in order to be able to use butterknife + dagger together?
Thanks in advance.
I was doing some testing and I changed
compile 'com.google.dagger:dagger-compiler:2.7'
to
apt 'com.google.dagger:dagger-compiler:2.7'
Sync and that worked.
I'm working in an Android App and a few days ago I updated my Kotlin version from 1.0.0-beta-1103 to the release candidate 1.0.0-rc-1036 and now I can't run my app for something related to Kotlin Android Extensions.
I use the apply plugin: 'kotlin-android-extensions' just like the official blog says http://blog.jetbrains.com/kotlin/2016/02/kotlin-1-0-release-candidate-is-out/ but my app doesn't recognize any view from any layout.
This is what appears on the console:
Error:(20, 8) Unresolved reference: kotlinx
Error:Execution failed for task ':app:compileDebugKotlin'.
Compilation error. See log for more details
Please help me, I'm losing my mind!
Update:
This is my buildScript in globalbuild.gradle
buildscript {
ext {
kotlin_version = "1.0.0-rc-1036"
}
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// The Fabric Gradle plugin uses an open ended version to react
// quickly to Android tooling updates
classpath 'io.fabric.tools:gradle:1.+'
}
}
The dependencies listed in the app build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
// Google
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:cardview-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
compile 'com.android.support:recyclerview-v7:23.1.0'
// Kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// Square
compile 'com.squareup.okio:okio:1.5.0'
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
// ReactiveX
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
compile 'com.jakewharton.rxbinding:rxbinding:0.3.0'
compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.2.0'
// DBFlow
kapt 'com.raizlabs.android:DBFlow-Compiler:2.2.1'
compile "com.raizlabs.android:DBFlow-Core:2.2.1"
compile "com.raizlabs.android:DBFlow:2.2.1"
// Otros
compile 'de.hdodenhof:circleimageview:1.3.0'
// SQLite Assets Helper
compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
// Crashlytics Kit
compile('com.crashlytics.sdk.android:crashlytics:2.5.2#aar') {
transitive = true
}
}
This is an import that I'm using:
import kotlinx.android.synthetic.main.activity_all_quizzes.*
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
should be defined in the project-local build.gradle file for now.
Your kotlin stdlib compile dependency should (at least while not stable) be the same as the kotlin android studio plugin version
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
...
buildscript {
ext.kotlin_version = '1.0.0-rc-1036'
...
i am new to android studio and wants to include boofcv library in my project. I am using Android studio for development. I have done the following steps in order to include the library and is stuck with build.gradle configuration.
Step 1: Have downloaded per-compiled jar files from http://boofcv.org/index.php?title=Download:BoofCV
Step 2: Have updated settings.gradle as
include ':app'
include ':libs:boofcv-libs'
Step 3: My build.gradle looks like:
apply plugin: 'com.android.application'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
As the note of your project's build.gradle file will suggest:
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Remove the compile statements in that gradle file:
compile project(":libs:boofcv-libs")
And copy them to other (module's) build.gradle and make dependencies look like this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.+'
compile project(":libs:boofcv-libs")
}
BoofCV is on maven central so you could just do the following too:
['calibration','feature','geo','ip','recognition','sfm','android'].each
{ String a -> compile group: 'org.boofcv', name: a, version: '0.18' }
In the next it will be even easier if you just want everything:
compile group: 'org.boofcv', name: "all", version: '0.19-SNAPSHOT'
Get latest version from this page
https://boofcv.org/index.php?title=Download
Convert maven to to gradle using this website and make sure to
change artifactId to boofcv-android:
http://sagioto.github.io/maven2gradle
so it will be something like this:
compile "org.boofcv:boofcv-android:0.27"
as mentioned in this page, to avoid library conflict add this to app.gradle:
// Remove libraries that conflict with libraries already included with Android
configurations {
all*.exclude group: "xmlpull", module: "xmlpull"
all*.exclude group: "org.apache.commons", module: "commons-compress"
}