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

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.

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

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 :)

android method not found in gradle

I have and android project named global flow and I after opening with android studio 2.2.3 on a debian linux I watch this error:
Error:(22, 0) Could not find method android() for arguments [build_5ijv6qsqmd3lnd1fe6nzaworu$_run_closure3#1c8251f] on root project 'GlobalFlow' of type org.gradle.api.Project.
Open File
When I try the solution in this forum could not find method android I don't know where to find all the android modules.
I update the build.gradle file in home/alex/GlobalFlow but I am not sure if is this one or the other one in home/alex/GlobalFlow/app. But I understand that the root project's build gradle is in the first path named.
My build.gradel is:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// 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
}
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
}
dependencies {
compile files('app/libs/junit-4-12-JavaDoc.jar')
}
apply plugin: 'maven'
Where are all android modules?
Thanks.
This is the project/build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
It contains build script instructions. And optionally closures applied to all modules.
The following line allows you to apply com.android.application plugin:
classpath 'com.android.tools.build:gradle:2.2.3'
Also read the NOTE carefully.
This is the project/module/build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
}
dependencies {
// We're in GlobalFlow/app now, adjust the relative path!
compile files('libs/junit-4-12-JavaDoc.jar')
}
apply plugin: 'maven'
When you apply plugin: 'com.android.application' you can use the android { ... } closure to setup Android app build process. Notice they're both in the module build.gradle.
If you were building an Adnroid library you'd apply plugin: 'com.android.library' which also provides you with android { ... } closure.
This is the recommended and default structure as of January 2017.
You can't use the android block in the top level file.
You have to add it in the home/alex/GlobalFlow/app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.1'
}
dependencies {
//.....
}

Gradle build - Could not determine the dependencies of task ':myLibrary:transformClassesAndResourcesWithProguardForRelease'

I have just migrated my project to gradle-experimental:0.4.0 to use JNI. I have followed the instructions here
The project consists of a library and an application. I cannot get round this error (tried the usual clean and invalidate cache/restart):
Could not determine the dependencies of task ':myLibrary:transformClassesAndResourcesWithProguardForRelease'
The library build fine but this error appears when I build the app module. Here are my modified build.gradle scripts:
Project:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle-experimental:0.4.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Library (build OK):
apply plugin: 'com.android.model.library'
model
{
android {
buildToolsVersion="23.0.1"
defaultConfig.with {
minSdkVersion.apiLevel=16
targetSdkVersion.apiLevel=16
testInstrumentationRunner="android.test.InstrumentationTestRunner"
}
}
android.buildTypes {
release {
minifyEnabled=true
proguardFiles.add(file('proguard.cfg'))
}
}
}
App (this build fails):
apply plugin: 'com.android.model.application'
dependencies {
compile files('libs/GoogleAdMobAdsSdk-4.1.1.jar')
compile project(':myLibrary')
}
model
{
android
{
compileSdkVersion='Google Inc.:Google APIs:16'
buildToolsVersion="23.0.1"
defaultConfig.with {
applicationId="my.app.ID"
minSdkVersion.apiLevel=16
targetSdkVersion.apiLevel=16
}
}
android.buildTypes {
release {
minifyEnabled=true
proguardFiles.add(file('proguard.cfg'))
}
}
}
Has anyone seen this error when moving to gradle-experimental?
Ok so removing the proguard line from the library build file fixed this:
--proguardFiles.add(file('proguard.cfg'))

Android Scala and Gradle

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.

Categories

Resources