how to get Dagger2 Compiler Options working? - android

I'm trying to use 3 of the dagger2 compiler options in my android project.
but it seems none of them actually work.
I have pasted the code from here to my gradle.properties and even compiler options of AS settings.
the 3 that I'm interested in are:
-Adagger.fastInit=enabled
-Adagger.formatGeneratedSource=disabled
-Adagger.gradle.incremental
the fastinit and codeformatting just don't work (judging by the code that is generated) but the incremental cause a compile error saying:
no compiler option found.
the versions that I'm using are:
dagger : 2.18
gradle : 5.2.1
kotlin : 1.3.21
androidPlugin : 3.3.1

For projects with multiple modules, the top build.gradle can be updated with this
allprojects {
repositories {
...
}
afterEvaluate {
extensions.findByName('kapt')?.arguments {
arg( "dagger.formatGeneratedSource", "disabled" )
}
}
}

Perhaps you should try without "A"
dagger.fastInit=enabled
dagger.formatGeneratedSource=disabled
dagger.gradle.incremental=enabled
Also can try directly in build.gradle, but this should be done for each project.
kapt {
arguments {
arg('dagger.fastInit', 'enabled')
arg('dagger.formatGeneratedSource', 'disabled')
arg('dagger.gradle.incremental', 'enabled')
}
}

Related

Convert groovy to kotlin dsl

Hi everyone I use appDynamics library and the documentation only explains with groovy Gradle, I have a problem with converting the groovy Gradle script to kotlin Gradle DSL and I have tried several ways and several syntaxes and I even used converting tools from groovy to Kotlin Gradle also didn't solve the problem following script with groovy Gradle
adeum {
account {
name 'xxx'
licenseKey 'yyyy'
}
proguardMappingFileUpload {
failBuildOnUploadFailure true //should build fail if upload fails? Defaults to false.
enabled true //enables automatic uploads. Defaults to true.
}
}
[Error][1]
[1]: https://i.stack.imgur.com/tet7q.png
and also i have to mention that the groovy is working fine
The problem is in some plugins in kts you have to use closure to determine the plugin packages
adeum {
account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
this.name ="xxx"
this.licenseKey ="yyy"
})
proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
this.failBuildOnUploadFailure = true
this.enabled = true
})
}
It's possible to change adeum to configure<ADPluginExtension>, like this:
configure<ADPluginExtension> {
account(closureOf<com.appdynamics.android.gradle.ADPluginExtension.Account> {
this.name ="xxx"
this.licenseKey ="yyy"
})
proguardMappingFileUpload(closureOf<com.appdynamics.android.gradle.ADPluginExtension.ProguardConfig> {
this.failBuildOnUploadFailure = true
this.enabled = true
})
}
I had some issues, when used adeum plugin with kotlin-dsl, where gradle sync not generated extensions accessors to android, dependencies, implementation, etc... provided from kotlin-dsl plugin.

Kotlin Native compile jar and framework

I'm building a multiplatform library for Android and iOS. My gradle file looks like this:
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.4.0'
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: 'maven-publish'
kotlin {
jvm()
// This is for iPhone simulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
ios {
binaries {
framework()
}
}
sourceSets {
commonMain {
dependencies {
implementation kotlin('stdlib-common')
implementation("com.ionspin.kotlin:bignum:0.2.2")
}
}
commonTest {
dependencies {
implementation kotlin('test-common')
implementation kotlin('test-annotations-common')
}
}
jvmMain {
dependencies {
implementation("com.ionspin.kotlin:bignum:0.2.2")
}
}
jvmTest {
dependencies {
implementation kotlin('test')
implementation kotlin('test-junit')
}
}
iosMain {
}
iosTest {
}
}
}
configurations {
compileClasspath
}
Im using a third party library and I'm using it like this:
fun test(value: String): Int {
return BigDecimal.parseString(value).toBigInteger().intValue()
}
The problem is when I build the .jar the bignum library isn't included, and when I use the lib in an Android project I get an exception ClassNotFoundException: Didn't find class "com.ionspin.kotlin.bignum.decimal.BigDecimal".
Is there a way to include third party libs in the .jar for Android and .framework for iOS?
JVM
So, the only way I've found to generate a Fat JAR that works like you expect is by adding two custom gradle tasks in project:build.gradle.kts of your KMP library after appling the java plugin.
plugins {
[...]
id("java")
}
[...]
kotlin {
jvm {
[...]
compilations {
val main = getByName("main")
tasks {
register<Copy>("unzip") {
group = "library"
val targetDir = File(buildDir, "3rd-libs")
project.delete(files(targetDir))
main.compileDependencyFiles.forEach {
println(it)
if (it.path.contains("com.")) {
from(zipTree(it))
into(targetDir)
}
}
}
register<Jar>("fatJar") {
group = "library"
manifest {
attributes["Implementation-Title"] = "Fat Jar"
attributes["Implementation-Version"] = archiveVersion
}
archiveBaseName.set("${project.name}-fat")
val thirdLibsDir = File(buildDir, "3rd-libs")
from(main.output.classesDirs, thirdLibsDir)
with(jar.get() as CopySpec)
}
}
tasks.getByName("fatJar").dependsOn("unzip")
}
}
[...]
}
You then must launch the fatJar gradle task that generate a .jar file with the 3rd libraries classes extracted from they corresponding jar archives.
You can customize the two custom gradle scripts even more in order to better fit your needs (here I only included com. package name starting deps).
Then in your Android app app:build.gradle file you can use it as you did or simply
implementation files('libs/KMLibraryTest001-fat-1.0-SNAPSHOT.jar')
iOS
As you ask also for the iOS part in your title (even if it's a second citizen in the main topic of your question) you need only to use api instead of implementation for your 3rd party library along with the export option of the framework.
ios() {
binaries {
framework() {
transitiveExport = true // all libraries
//export(project(":LibA")) // this library project in a trainsitive way
//export("your 3rd party lib") // this 3rd party lib in a transitive way
}
}
}
And you can find a full reference here.
If you see the Krypto library, it has
androidMain
jsMain
jvmMain
mingwX64Main
nativPosixMain
Which means 5 kind of binaries are generated to support 5 platforms
Convincingly, this explains that each platform expects its own binary
for example,
windows -- DLL file
linux -- so file
java -- JAR file
mac -- dylib file
A JAR gets loaded into JVM, but IOS does not use JVM
Separate your Utility functions which has a common logic and write gradle to target multiple platforms
If you want to start with pure multiplatform, you can try this Official Example
Or create a sub gradle module and create a library project which is common to IOS as well as Android
The possible targets are properly documented here
I have created a application which publishes the binary to local repository and re-uses in the MainActivity -- you can get the code here
modify the local.properties for android SDK location and use
gradlew assemble
to build the APK and test it yourself
open the mylib\build.gradle.kts folder and you can see the targets jvm and iosX64 , jvm is used for android
If I'm correct using api instead of implementation should fix your problem, though I didn't try it out yet on the Native part
See Api and implementation separation

How to exclude explicitApi warnings for test classes in Kotlin?

I have Android library module with enabled explicitApi kotlin feature in gradle
android {
kotlinOptions {
freeCompilerArgs += '-Xexplicit-api=warning'
}
}
Everything is fine, but the problem is that warnings are also reported for test classes in packages src/test and src/androidTest.
How to exclude test classes from explicit-api control?
Thanks
As far as I know, you can't! I was thinking of opening a bug report just this week but never got to it. In the meantime, I suggest you add something like this to your build script, which will at least fix it for the Kotlin compiler (but you'll still see the IDE warnings):
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
if (!it.name.contains("Test")) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
If you're using Gradle Kotlin DSL:
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
if ("UnitTest" !in name) {
kotlinOptions.freeCompilerArgs += "-Xexplicit-api=strict"
}
}
And don't set the compiler argument in android.kotlinOptions, only in that block, so that it is applied only to the non-test source sets.
EDIT: I just checked and the warnings bug was fixed in IntelliJ, so it should be fixed in Android Studio in a few months to a year.

How to configure Firebase Performance Monitoring plugin extension in Gradle Kotlin DSL

I have an Android app using Gradle with Kotlin DSL. I'm adding Firebase Performance Monitoring, but I would like for it to be enabled only for a specific build type.
I've been following the instructions provided at Firebase - Disable Firebase Performance Monitoring. Unfortunately the provided snippets are in Groovy.
I've tried to get a reference to the Firebase Performance Monitoring extension in my app level Gradle script by doing the following:
plugins {
...
id("com.google.firebase.firebase-perf")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}
buildTypes {
getByName(BuildTypes.DEBUG) {
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> {
setInstrumentationEnabled(false)
}
}
...
}
...
dependencies {
val firebaseVersion = "17.2.1"
implementation("com.google.firebase:firebase-core:$firebaseVersion")
implementation("com.google.firebase:firebase-analytics:$firebaseVersion")
implementation("com.google.firebase:firebase-perf:19.0.5")
}
Android Studio doesn't see any problem in this and auto-completes FirebasePerfExtension.
Unfortunately upon running a Gradle sync I get the following:
Extension of type 'FirebasePerfExtension' does not exist.
Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, CrashlyticsExtension, KotlinAndroidProjectExtension, KotlinTestsRegistry, AndroidExtensionsExtension, KaptExtension]
There's no plugin extension related to Firebase Performance Monitoring.
This is in my project level build.gradle file dependencies block:
classpath("com.google.firebase:perf-plugin:1.3.1")
Any help is appreciated!
Update 1
As recommended on the Gradle - Migrating build logic from Groovy to Kotlin guide at "Knowing what plugin-provided extensions are available" I've ran the kotlinDslAccessorsReport task. None of the resulting extensions seems to be related to Firebase.
Had the same issue and was going to apply from groovy file, but seems i found the solution in here: https://docs.gradle.org/5.0/userguide/kotlin_dsl.html#sec:interoperability
withGroovyBuilder {
"FirebasePerformance" {
invokeMethod("setInstrumentationEnabled", false)
}
}
We used this answer, util we discovered a better working way in the team
check(this is ExtensionAware)
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> { setInstrumentationEnabled(false) }

Android Build failure : Could not get unknown property 'assembleDebug' for project

I need to create Jar and copy to lib folder, which is done in following task :
task copyJarToLib(type: Copy, dependsOn: 'createJar') {
from "build/libs/lib1.jar"
from "build/libs/lib2.jar"
into "../App/libs/"
}
I have to execute this after apk generation. So, I am calling following instruction at the end of the module-app build.gradle :
assembleDebug.finalizedBy(copyJarToLib)
Issue is observed after upgrading the gradle plugin to 3.1.0 and gradle to 4.4.
Same implementation is working fine with gradle 2.3.
If you want to execute something at the end of build, you can do it as follows:
gradle.buildFinished {
copy {
from "build/libs/lib1.jar"
from "build/libs/lib2.jar"
into "../App/libs/"
}
}
If you want to execute task before apk is built the you can:
afterEvaluate {
project.tasks.findByName('preDebugBuild').dependsOn(':<module>:copyJarToLib')
}
base on Android Studio 2020.3.1, you can use follow codes
afterEvaluate {
project.tasks.findByName('preDebugBuild').dependsOn(copyJarToLib)
}

Categories

Resources