Android Scala and Gradle - android

I am new to Gradle. Is there some example how to configure properly gradle-android-plugin for scala classes.
this is what I have now.
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.1' }
}
apply plugin: 'android'
apply plugin: 'eclipse'
apply plugin: 'scala'
sourceCompatibility = 1.6
version = "1.0.0"
repositories { mavenCentral() }
dependencies {
compile files('/home/pcu/workspace/workspace-android/emoo/libs/android-support-v4.jar')
compile 'org.scala-lang:scala-library:2.9.1'
scalaTools 'org.scala-lang:scala-compiler:2.9.1'
scalaTools 'org.scala-lang:scala-library:2.9.1'
}
task configureDebug << { jar.classifier = "debug" }
task configureRelease << { proguard.enabled = true }
but compilation fails. Scala class is not compiled.

It is actually much simpler with the right plugin:
https://github.com/saturday06/gradle-android-scala-plugin
This worked perfectly fine for me.
You need only about 3 more lines in your gradle configuration and potentially proguard to reduce the apk size.
The setup is well documented on the github page. Everything beyond steps 1-3 is optional.

Related

Can I define kotlinVer either plugins or dependencies when I use Android Studio?

I find there are two ways to define kotlin version in build.gradle (Project) based some sample projects, such as Code A and Code B.
Are they the same way? which one is better?
Code A
buildscript {
ext {
kotlinVer = '1.7.10'
...
}
}
plugins {
...
id 'org.jetbrains.kotlin.android' version "$kotlinVer" apply false
}
Code B
buildscript {
ext {
...
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10"
...
}
}
Both serve the same purpose, and the reason they are different is because of newer Gradle upgrades from 7.1 upward.
You can check out how it's done in the android Doc. here

How to test local gradle plugin that depends on android plugin?

I am using this project to start a gradle plugin development : https://github.com/int128/gradle-plugin-starter
In this project, there are two kinds of tests :
Unit test using spock
Acceptance tests using gradle test kit
My plugin aims to configure android plugin. It is for internal use. We have a lot of projects that are configured in a same way so I want to use the same code base to do that.
Let's say that my plugin code is as follow :
class CopperPlugin implements Plugin<Project> {
#Override
void apply(Project project) {
if(!project.plugins.hasPlugin("com.android.application") && !project.plugins.hasPlugin("com.android.library")){
throw new GradleScriptException("Android plugin needs to be applied first", new ClassNotFoundException())
}
}
}
I don't know how to make acceptance test working !!! Error is :
* What went wrong:
A problem occurred evaluating root project 'fixture-42'.
Plugin with id 'fr.coppernic.android' not found.
Acceptance test code is
#Unroll
def 'acceptance test should pass on Gradle #gradleVersion'() {
given:
def runner = GradleRunner.create()
.withProjectDir(new File("fixture-${gradleVersion.replace(".","")}"))
.withArguments('test')
.withPluginClasspath()
.withGradleVersion(gradleVersion)
when:
runner.build()
then:
noExceptionThrown()
where:
gradleVersion << ['4.2', '3.5']
}
acceptance test build.gradle
plugins {
id 'groovy'
id 'java-gradle-plugin'
}
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
jcenter()
}
dependencies {
testCompile('org.spockframework:spock-core:1.1-groovy-2.4') {
exclude module: 'groovy-all'
}
}
gradlePlugin {
pluginSourceSet parent.sourceSets.main
}
build.gradle from fixture-35 folder
buildscript {
repositories {
jcenter()
maven { url 'http://arti-01:8081/artifactory/plugins-release' }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin:'com.android.application'
apply plugin:'fr.coppernic.android'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
}
task test << {
assert project.plugins.hasPlugin('fr.coppernic.android')
}
build.gradle from fixture-42 folder
buildscript {
repositories {
google()
jcenter()
maven { url 'http://arti-01:8081/artifactory/plugins-release' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
apply plugin: 'com.android.application'
apply plugin: 'fr.coppernic.android'
task test << {
assert project.plugins.hasPlugin('fr.coppernic.android')
}
GradleTestKit only looks in the plugins DSL definition. You are applying your plugin through the apply plugin: syntax which wouldn't work.

Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'

build.gradle
buildscript {
ext.kotlin_version = '1.1.51'
repositories {
jcenter()
mavenCentral()
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'me.tatarka:gradle-retrolambda:3.6.1'
classpath 'com.google.gms:google-services:3.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
app/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.7.0'
}
}
android{
compileSdkVersion = 26
buildToolsVersion = "26.0.2"
defaultConfig {
minSdkVersion = 16
targetSdkVersion = 26
}
...
applicationVariants.all { variant ->
variant.outputs.all { output ->
outputFileName = "newApkName.apk"
}
}
}
How to solve issue:
Unable to find method 'com.android.build.gradle.api.BaseVariant.getOutputs()Ljava/util/List;'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync
project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build
processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of
Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
I had the same error with gradle syncing, specifically tied to butterknife, my solution was solved via
https://github.com/JakeWharton/butterknife/issues/963#issuecomment-339545297
tldr in your project build file...
buildscript{
repositories {
//other repos will likely exist here like jcenter and mavenCentral
//add this closure
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:8.7.0'
classpath 'com.android.tools.build:gradle:3.0.0'
//change your version to 9.0.0-SNAPSHOT
classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
//.. other project level dependencies
}
}
}
Also make sure your allproject includes
allproject{
repositories{
maven {
//this
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
}
And then the none butterknife related issue was upping my buildToolsVersions to "26.0.2" in your app build file
UPDATE 4/19/2018
Have since parted ways with butterknife as it has since caused more issues than any other 3rd party I've used. Besides, with full Kotlin support, butterknife isn't necessary
seems that issue is not in app/build.gradle , but in butterknife-gradle-plugin
Solved by removing butterknife-gradle-plugin
Based on ElliotM's answer:
Kotlin 1.2.10
Gradle 3.0.1
Build tools version 27.0.3
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
apply plugin: 'com.jakewharton.butterknife'
In my library project this comment helped me (R2 imports):
https://github.com/JakeWharton/butterknife/issues/963#issuecomment-342547601
project's build.gradle:
buildscript {
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
google()
jcenter()
}
dependencies {
classpath 'com.jakewharton:butterknife-gradle-plugin:9.0.0-SNAPSHOT'
}
}
repositories {
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
module's build.gradle:
apply plugin: 'com.jakewharton.butterknife'
...
dependencies {
compile 'com.jakewharton:butterknife:9.0.0-SNAPSHOT'
kapt 'com.jakewharton:butterknife-compiler:9.0.0-SNAPSHOT'
}
This way you can use the
apply plugin: 'com.jakewharton.butterknife'
My solution was to upgrade gradle in my project
from : com.android.tools.build:gradle:3.1.4
to : com.android.tools.build:gradle:3.5.3

Firebase unresolved supertype with Kotlin on Android

I'm experiencing a problem in my project that uses Firebase core and messaging v11.4.2.
The gradle sync works perfectly but then I get this error when compiling:
e: Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class com.google.android.gms.internal.zzctr, unresolved supertypes: com.google.android.gms.internal.zzee
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ‘:app:compileDebugKotlin’.
Compilation error. See log for more details
I've tried both with Kotlin version 1.1.51 and 1.2.0-beta-88; Gradle plugins v2.3.3 and 3.0.0
Any help is welcomed, thanks a lot!
This is how I've configured the project:
app build.gradle
// tried adding and removing the -kapt and -android-extensions. Didn't help.
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
debug {
/// added this to be sure the class was not being left out
minifyEnabled false
shrinkResources false
useProguard false
}
...
dependencies {
// Firebase Core
implementation "com.google.firebase:firebase-core:${rootProject.ext.firebaseVersion}"
// Firebase Cloud Messaging
implementation "com.google.firebase:firebase-messaging:${rootProject.ext.firebaseVersion}"
}
...
// Keep this as the last line or the build will fail
apply plugin: 'com.google.gms.google-services'
* project build.gradle *
buildscript {
// App
ext.compileSdkVersion = 26
ext.minSdkVersion = 18
ext.buildToolsVersion = '26.0.2'
ext.targetSdkVersion = 26
// Kotlin beta, stable version doesn't compile either
ext.kotlin_version = '1.2.0-beta-88'
// Android
ext.androidSupportVersion = '26.1.0'
//TODO: implement LifecycleOwner interface from Architecture Components.
ext.lifecycleVersion = '1.0.0-beta2'
// Architecture, RxJava, Injection
ext.daggerVersion = '2.11'
ext.butterKnifeVersion = '8.8.1'
ext.rxJavaVersion = '2.1.0'
ext.rxAndroidVersion = '2.0.1'
// Google/Firebase Cloud Message
ext.firebaseVersion = '11.4.2'
// Libraries shared between modules (TODO)
}
repositories {
maven { url 'https://maven.google.com' } // Google Maven Repository
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2'} // Kotlin beta
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:3.1.1' // google-services plugin
//// tried this too: classpath 'com.google.firebase:firebase-plugins:1.1.1'
}
allprojects {
repositories {
jcenter()
mavenLocal()
maven { url 'http://dl.bintray.com/kotlin/kotlin-eap-1.2'}
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
flatDir {
dirs 'libs'
}
}
Turns out the problem was an incompatible version of the Google Wallet library, being implemented by another dependency of the project and conflicting with the one imported by the com.google.gms.google-services plugin.
For some reason gradle didn't state this problem and I had to go through the code until I found the place where Wallet was trying to access a class that was successfully importing but at the same time it couldn't be found.
Simply by updating the version in the other module everything was fixed :)

Building android project with gradle

Im trying to build a simple android app using gradle build tools. but im getting an error like this
No signature of method: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.compile() is applicable for argument types: (java.lang.String) values: [org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT]
Possible solutions: module(java.lang.Object)
ang here's a simple configuration of build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
compile 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
processResource {
expand (project.properties)
}
task configureDebug << {
jar.classifier = "debug"
}
task configureRelease << {
proguard.enabled = true
}
When applying a plugin you want tell you build script to use it in its classpath. It is not required for compilation so just change the configuration compile to classpath. More more information see 51.5.1. Using your plugin in another project in the Gradle user guide.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.gradle.api.plugins:gradle-android-plugin:1.2.0-SNAPSHOT'
}
}
EDIT: At the moment the plugin does not support r20 of the Android SDK. For more information see this issue.
Make sure you are writing the dependency block on your application build.gradle "YourProjectName->yourprojectname->build.gradle" in android studio hierarchy .
Use android gradle tools
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}

Categories

Resources