How to extract composeBom dependencies code to external files in Gradle? - android

I have an external file.
def composeBom = platform('androidx.compose:compose-bom:2022.12.00')
But this caused an error, it seems that in the external file there is no platform()
Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method platform() for arguments [androidx.compose:compose-bom:2022.12.00] on object of type org.gradle.internal.extensibility.DefaultExtraPropertiesExtension.
How can I solve this problem?

You can use something different:
Define:
buildscript {
ext {
composeBom = 'androidx.compose:compose-bom:2022.12.00'
}
}
Then in the dependencies block
dependencies {
implementation (platform(composeBom))
//..
}

Related

Error while setting up Detekt custom rules

I have setup Detekt in a multi module project. Below is my root level build.gradle
tasks.register("detektAll", Detekt) {
description = "Custom Detekt build for all modules"
parallel = true
setSource(file(projectDir))
config.setFrom(files("$rootDir/detekt.yml"))
jvmTarget = "1.8"
classpath.setFrom(project.configurations.getByName("detekt"))
include("**/*.kt")
exclude("**/build/**")
reports {
html {
enabled = true
destination = file("$rootDir/build/reports/detekt-results.html")
}
xml.enabled = false
txt.enabled = false
}
}
Now I am looking at setting up custom rule for which I followed this
This is what my build.gradle of customdetekt module looks like
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compileOnly "io.gitlab.arturbosch.detekt:detekt-api:1.17.1"
testImplementation "junit:junit:4.13.2"
testImplementation "org.assertj:assertj-core:3.20.2"
testImplementation "io.gitlab.arturbosch.detekt:detekt-api:1.17.1"
testImplementation "io.gitlab.arturbosch.detekt:detekt-test:1.17.1"
}
Now when I hook up custom rule module in my main module using this:
detektPlugins project(path: ':customdetekt', configuration: 'default')
I get an error:
Caused by: org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could not find method detektPlugins() for arguments [DefaultProjectDependency{dependencyProject='project ':customdetekt''
Please help

Could not resolve com.squareup.sqldelight:runtime:1.1.3

I tried to integrate sqldelight in my Multiplatform library project for Android/iOS but I'm having several unresolved dependency errors when syncing gradle.
ERROR: Unable to resolve dependency for ':SharedCode#debug/compileClasspath': Could not resolve com.squareup.sqldelight:runtime:1.1.3.
ERROR: Unable to resolve dependency for ':SharedCode#debug/compileClasspath': Could not resolve com.squareup.sqldelight:runtime-jvm:1.1.3.
ERROR: Unable to resolve dependency for ':SharedCode#release/compileClasspath': Could not resolve com.squareup.sqldelight:runtime:1.1.3.
ERROR: Unable to resolve dependency for ':SharedCode#release/compileClasspath': Could not resolve com.squareup.sqldelight:runtime-jvm:1.1.3.**
Gradle version 5.1.1
Gradle Plugin 3.4.0
sqldelight 1.1.3
enableFeaturePreview('GRADLE_METADATA') is present in my settings.gradle
My project gradle file looks like this:
buildscript {
ext {
kotlin_version = '1.3.31'
ktor_version = '1.2.1'
ktor_json_version = '1.2.1'
kotlinx_coroutines_version = '1.2.1'
serialization_version = '0.11.0'
sqldelight_version = '1.1.3'
dokka_version = '0.9.16'
}
repositories {
google()
jcenter()
mavenCentral()
gradlePluginPortal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-android-gradle-plugin:$dokka_version"
classpath "com.squareup.sqldelight:gradle-plugin:$sqldelight_version"
}
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://kotlin.bintray.com/kotlinx" }
}
}
task clean(type: Delete) {
setDelete rootProject.buildDir
}
my SharedCode lib gradle file:
apply plugin: 'kotlinx-serialization'
apply plugin: 'com.android.library'
apply plugin: 'org.jetbrains.kotlin.multiplatform'
apply plugin: 'com.squareup.sqldelight'
group = 'com.example.multiplatform'
version = '1.0'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 15
}
buildTypes {
release {
minifyEnabled false
}
}
}
dependencies {
// Specify Kotlin/JVM stdlib dependency.
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7'
implementation "com.squareup.sqldelight:runtime:1.1.3"
testImplementation 'junit:junit:4.12'
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.jetbrains.kotlin:kotlin-test'
androidTestImplementation 'org.jetbrains.kotlin:kotlin-test-junit'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
kotlin {
targets {
final def iOSTarget = System.getenv('SDK_NAME')?.startsWith("iphoneos") \
? presets.iosArm64 : presets.iosX64
fromPreset(iOSTarget, 'ios') {
binaries {
framework('SharedCode')
}
}
fromPreset(presets.android, 'androidLib')
}
sourceSets {
commonMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-json:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlinx_coroutines_version"
//Kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serialization_version"
//sqldelight
implementation "com.squareup.sqldelight:runtime:$sqldelight_version"
}
}
androidLibMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-json-jvm:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization-jvm:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinx_coroutines_version"
//Kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$serialization_version"
//sqldelight
implementation "com.squareup.sqldelight:android-driver:$sqldelight_version"
}
}
iosMain {
dependencies {
//HTTP
implementation "io.ktor:ktor-client-ios:$ktor_version"
implementation "io.ktor:ktor-client-json-native:$ktor_json_version"
implementation "io.ktor:ktor-client-serialization-native:$ktor_version"
//Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-native:$kotlinx_coroutines_version"
//kotlinx serialization
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-native:$serialization_version"
//sqldelight
implementation "com.squareup.sqldelight:ios-driver:$sqldelight_version"
}
}
}
}
sqldelight {
MyApp {
packageName = 'com.example.multiplatform'
}
}
configurations {
compileClasspath
}
task packForXCode(type: Sync) {
final File frameworkDir = new File(buildDir, "xcode-frameworks")
final String mode = project.findProperty("XCODE_CONFIGURATION")?.toUpperCase() ?: 'DEBUG'
final def framework = kotlin.targets.ios.binaries.getFramework("SharedCode", mode)
inputs.property "mode", mode
dependsOn framework.linkTask
from { framework.outputFile.parentFile }
into frameworkDir
doLast {
new File(frameworkDir, 'gradlew').with {
text = "#!/bin/bash\nexport 'JAVA_HOME=${System.getProperty("java.home")}'\ncd '${rootProject.rootDir}'\n./gradlew \$#\n"
setExecutable(true)
}
}
}
tasks.build.dependsOn packForXCode
I was solved this problem.
First: Apply sqldelight plugin right to all project:
apply plugin: 'com.squareup.sqldelight'
sqldelight {
MyDatabase {
packageName = "ru.trendagent.database"
sourceFolders = ["sqldelight"]
}
}
In android section add:
packagingOptions {
...
exclude 'META-INF/*.kotlin_module'
}
remove all implementations of sqldelight from CommonMain section
add implementation to AndroidMain section:
implementation "com.squareup.sqldelight:android-driver:1.1.3" //DataBase
add implementation to iosMain section if need:
implementation "com.squareup.sqldelight:ios-driver:1.1.3"//DataBase
add Metadata settings to gradle to settings.gradle file:
enableFeaturePreview("GRADLE_METADATA") // IMPORTANT!
Fully clean gradle files, reset gradle wrapper and other.
Don't use kotlin 1.4.0. It unsupport sqldelight 1.1.3 version
Change gradle to latest version. You can download latest gradle manually and set it on IDE. You need gradle 5.3.1 or latest (5.1.1 unsupported)
If error will remain - change IDE to intellij idea comunity edition
This is seems to me as an error occurred due to offline gradle or invalid cache as this error is not specific to com.squareup.sqldelight. To resolve this issue follow the following steps:
File
Settings
Build, Execution, Deployment
Gradle
Uncheck offline work
and then try to invalidate the cache and restart from file menu. Hope it helps!
Change this
classpath "com.squareup.sqldelight:gradle-plugin:$sqldelight_version"
Following
classpath "com.squareup.sqldelight:runtime:$sqldelight_version"
you are passing wrong metadata to sqldelight change with my solution it will work
The Gradle part of the readme of the library indicates, that gradle plugin is needed. There's no such sqldelight library as runtime. They only have android-driver, ios-driver and sqlite-driver.
Using the DroidConKotlin app project as an example, I upgraded gradle from 5.1.1 to 5.3.1 and it resolved all the dependencies issues.
You likely are trying to use a dependency on dependant libraries or even applications and not only in commonMain module. Just change:
commonMain {
dependencies {
...
implementation "com.squareup.sqldelight:runtime:$sqldelight_version"
}
}
to
commonMain {
dependencies {
...
api "com.squareup.sqldelight:runtime:$sqldelight_version"
}
}
Also change it on dependencies section
And do the same with any other dependency uncorrecly resolved
This can happen for example if you define a class that implements an interface provided by the dependency and you use or extend that class on dependant libraries or applications (Just an example of millions that could be).
If this is your case, for further understanding, just take a look to this answer:

java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.experimental.Deferred"

I am facing this issue when using coroutine with retrofit, can you please let me know why i am getting this error
java.lang.ClassNotFoundException: Didn't find class "kotlinx.coroutines.experimental.Deferred" on path: DexPathList[[zip file "/data/app/com.coroutines.retrofit.kotlin-1/base.apk"],nativeLibraryDirectories=[/vendor/lib64, /system/lib64]]
while i am using the below dependencies,
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
and the kotlin version is :
ext.kotlin_version = '1.3.10'
Add this dependency in your build.gradle : (remove experimental dependency for coroutine)
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
}
And add CoroutineCallAdapterFactory() for retrofit
addCallAdapterFactory(CoroutineCallAdapterFactory())
I am facing the same problem, it is the Jake Warton's library problem. It is using experimental references internally.
Using implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:1.0.0'
java.lang.NoClassDefFoundError: Failed resolution of: Lkotlinx/coroutines/experimental/Deferred;
at com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory.get
This library is deprecated . Here is the solution from the library's page on git
This library is deprecated. Please migrate to Retrofit 2.6.0 or newer and its built-in suspend support
it works: (pay attention to jar task)
plugins {
application
kotlin("jvm") version "1.6.10"
}
group = "org.example"
version = "1.0-SNAPSHOT"
application {
mainClass.set("org.example.MainKt")
}
repositories {
mavenCentral()
}
dependencies {
implementation("io.ktor:ktor-client-core-jvm:1.6.7")
implementation("io.ktor:ktor-client-cio-jvm:1.6.7")
}
tasks {
jar {
manifest {
attributes["Main-Class"] = application.mainClass
}
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations.compileClasspath.get().forEach {
from(if (it.isDirectory) it else zipTree(it))
}
}
compileKotlin {
kotlinOptions.freeCompilerArgs += "-opt-in=kotlin.ExperimentalStdlibApi"
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}
}

Independent android library with Google analytics

TL;DR
Any chance to compile Google Analytics inside library module without any help from application module?
I'm struggling for more than a week on how to compile an use GoogleAnalytics in my library.
The goals are:
1. Compile and run GoogleAnalytics in android library
2. Maintain an application context (Singletone? )
Here is how i'm trying to compile GoogleAnalytics in my library build.gradle file
buildscript {
ext {
support = '27.0.2' // https://developer.android.com/topic/libraries/support-library/revisions.html
kotlin = '1.2.20' // https://kotlinlang.org/docs/reference/using-gradle.html
playServices = '11.8.0'
}
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin"
classpath 'com.google.gms:google-services:3.1.0'
}
}
And the code that handles all analytics:
class GenericSettingsAnalytics private constructor(context : Context) {
private val tracker = GoogleAnalytics.getInstance(context).newTracker(R.xml.global_tracker)
companion object {
private var instance : GenericSettingsAnalytics? = null
fun getInstance(context: Context) : GenericSettingsAnalytics {
if (instance == null) {
instance = GenericSettingsAnalytics(context)
instance!!.sendInitAnalytics(context)
}
return instance!!
}
}
fun sendEvent(category: String, action: String) {
tracker.send(HitBuilders.EventBuilder().setCategory(category).setAction(action).build())
}
fun sendInitAnalytics(context: Context) {
tracker.send(HitBuilders.EventBuilder()
.setCategory(ANALYTICS_CATEGORIES.CATEGORY_APP_INFO.categoryName)
.setAction(ANALYTICS_ACTIONS.ACTION_APP_INIT.actionName)
.setLabel(context.packageName)
.build())
}
}
Very simple one.
Everything works great in debug, but when uploading library to bintray (To use it's official compile) i'm getting the following exception:
FATAL EXCEPTION: main
Process: com.udioshi85.genericsettings, PID: 12398
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/analytics/GoogleAnalytics;
at com.oshi.libgenericsettings.GenericSettingsAnalytics.<init>(GenericSettingsAnalytics.kt:14)
at com.oshi.libgenericsettings.GenericSettingsAnalytics.<init>(GenericSettingsAnalytics.kt:12)
at com.oshi.libgenericsettings.GenericSettingsAnalytics$Companion.getInstance(GenericSettingsAnalytics.kt:22)
at com.oshi.libgenericsettings.GenericSettingsLib$Companion.initAnalytics(GenericSettingsLib.kt:20)
at com.oshi.libgenericsettings.GenericSettingsLib$Companion.init(GenericSettingsLib.kt:16)
at com.oshi.genericsettings.GenericSettingsApp.onCreate(GenericSettingsApp.kt:10)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5827)
at android.app.ActivityThread.-wrap1(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1673)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:172)
at android.app.ActivityThread.main(ActivityThread.java:6637)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.analytics.GoogleAnalytics"
Some thougts:
In my library build.gradle i'm adding the following classpath:
buildscript {
....
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
}
}
Is it correct?
Most common libraries counts on their users to init the library from an application class. Something like
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
MyLibrary.init(this)
}
}
I've tried it already. but still getting the same exception. Maybe i'm doing something wrong?
Last thing, I've seen & read lots of examples on how to integrate GoogleAnalytics. The important thing is that my users should not add anything in their build.gradle except my compile/implementation library.
Would appreciate if someone can help me with that.
Thanks very much!
buildscript dependencies are the dependencies of your build script itself, not dependencies of your library. That is why it is not being included into your final build.
You can solve this issue by adding your classpath dependency to the application as well.
buildscript {
dependencies {
classpath 'com.google.gms:google-services:3.1.0'
}
}

Gradle Sync failed:Could not find method compile() for arguments

i am refering this https://github.com/mopub/mopub-android-sdk/wiki/Getting-Started to integrate mopub in my app. I got an error "could not find method compile for arguments" while adding mopub-sdk dependency in my project's build.gradle file.Please help me out.
Build.gradle file:
buildscript {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}}
allprojects {
repositories {
jcenter()
}
dependencies {
compile('com.mopub:mopub-sdk:4.14.0#aar') {
transitive = true
}
// For banners
compile('com.mopub:mopub-sdk-banner:4.14.0#aar') {
transitive = true
}
// For interstitials
compile('com.mopub:mopub-sdk-interstitial:4.14.0#aar') {
transitive = true
}
// For rewarded videos. This will automatically also include
interstitials
compile('com.mopub:mopub-sdk-rewardedvideo:4.14.0#aar') {
transitive = true
}
// For native static (images).
compile('com.mopub:mopub-sdk-native-static:4.14.0#aar') {
transitive = true
}
// For native video. This will automatically also include native static
compile('com.mopub:mopub-sdk-native-video:4.14.0#aar')
{transitive = true}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Actual Error:-
Error:(31, 0) Could not find method compile() for arguments [com.mopub:mopub-sdk:4.14.0#aar, build_7edkg2m28hxg79jomt8w3j8ic$_run_closure1$_closure4$_closure5#1c36532] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
You need to put your dependencies in the other build.gradle. They belong to the individual module build.gradle files.
Also, you are importing com.mopub:mopub-sdk:4.14.0#aar, which contains all of the ad format dependencies you already have below in your Gradle file. You only need com.mopub:mopub-sdk:4.14.0#aar or the modularized artifacts.

Categories

Resources