Cannot see androidx core-ktx extensions in Android Studio project - android

It seems that my Android Studio is not recognizing the 'androidx.core:core-ktx:1.2.0' package. Android Studio version: 3.6.1
My gradle.properties:
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
My app module build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
defaultConfig {
applicationId "cz.nn.example.myapplication"
minSdkVersion 25
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
targetCompatibility = JavaVersion.VERSION_1_8.toString()
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
When I try to use extension functions like:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val manager = systemService<NotificationManager>()
SharedPreferences.edit {
}
}
}
functions systemService<NotificationManager>() and edit are not recognized. What am I doing wrong?

TL;DR
You are using them wrongly
First of all SharedPreferences.edit() extension function is not static and needs an instance of SharedPreferences to be used on. Try this:
val prefs = getSharedPreferences("com.example.yourapp", Context.MODE_PRIVATE)
prefs.edit { putString("myString", "myValue") }
Secondly, the method is called getSystemService, not systemService. So, this will work:
val manager = getSystemService<NotificationManager>()

Related

implementation of cometchat sdk into kotlin project

I have been trying to implement comet Chat sdk into my project. I followed the documentation for kotlin but each time i try to initialize comet chat i get several errors. Here are my codes. 1. Main Activity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initCometChat()
}
private fun initCometChat() {
private val appID = "208314db190ca55b"
private val region = "us"
val appSettings = AppSettingsBuilder().subscribePresenceForAllUsers().setRegion(region).build()
CometChat.init(this, appID, appSettings, object : CallbackListener<String>() {
override fun onSuccess(successMessage: String) {
}
override fun onError(e: CometChatException) {
}
})
}
project level build gradle
plugins {
id 'com.android.application' version '7.1.1' apply false
id 'com.android.library' version '7.1.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
The CometChat.init() function returns an error marked with red. I am new to kotlin and so i do not know how sdks work. Please help me. Thank you
app level build gradle
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.chat.comet"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-`rules.pro'`
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.cometchat:pro-android-chat-sdk:3.0.4'
}

Strange static method error for normal method invocation in minimal Android app

I get a strange runtime exception when running some very simple code (8-line method; only 2 lines relevant) on my Android 10 device, after compiling with Android Studio Canary 7 (or 6) using Jetpack Compose alpha12.
Error:
java.lang.NoSuchMethodError: No static method copy-0d7_KjU$default(JFFFFILjava/lang/Object;)J in class Landroidx/compose/ui/graphics/Color; or its super classes (declaration of 'androidx.compose.ui.graphics.Color' appears in /data/app/com.example.myapplication-GbQdisqKhWdQawA6_DsPkQ==/base.apk)
at com.example.myapplication.MainActivity.onCreate(MainActivity.kt:15)
at android.app.Activity.performCreate(Activity.java:7955)
Code:
package com.example.myapplication
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.activity.compose.setContent
import androidx.compose.ui.graphics.Color
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val x = Color(0xFFBB86FC)
val y = x.copy() // ******* line 15 *******
setContent {
Surface(color = MaterialTheme.colors.background) {
Text(text = "hello world")
}
}
}
}
Top-level build.gradle:
buildscript {
ext {
compose_version = '1.0.0-alpha12'
}
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0-alpha07"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module level build.gradle:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
useIR = true
freeCompilerArgs += [
"-P",
"plugin:androidx.compose.compiler.plugins.kotlin:suppressKotlinVersionCompatibilityCheck=true"
]
}
buildFeatures {
compose true
}
composeOptions {
kotlinCompilerExtensionVersion compose_version
}
}
dependencies {
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.activity:activity-compose:1.3.0-alpha02'
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.ui:ui:$compose_version"
implementation "androidx.compose.material:material:$compose_version"
implementation "androidx.compose.ui:ui-tooling:$compose_version"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
The culprit is the line classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30-RC" which should instead be 1.4.30 without the -RC. The reason I had the initial -RC version was because the latest Android Studio Kotlin plugin version is indeed the -RC (frustrating that IntelliJ doesn't release the correct version even though it seems to be available in gradle), and using 1.4.30 gave a warning that it was different from the IDE version. Ignoring that warning and using the release version fixed that problem.
Sigh!

DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'

Gets following warning when building the project
DSL element 'android.dataBinding.enabled' is obsolete and has been replaced with 'android.buildFeatures.dataBinding'.
I am using Android Studio Canary 6
Starting from Android Gradle Plugin 4.0.0-alpha05 there is a new block called buildFeatures to enable build features.
So in order to enable databinding with new AGP plugin you have do like following in module (ex: app) level gradle file
build.gradle ( Groovy DSL )
// shorter version
// android.buildFeatures.dataBinding true
// longer version
android {
buildFeatures {
dataBinding true
// for view binding:
// viewBinding true
}
}
build.gradle.kts ( Kotlin DSL )
// shorter version
// android.buildFeatures.dataBinding = true
// longer version
android {
buildFeatures {
dataBinding = true
// for view binding:
// viewBinding = true
}
}
Reference: https://developer.android.com/studio/releases/gradle-plugin#buildFeatures
Put it in build.gradle(app level).It will work with android studio version greater than or equal to 4.0.0.
android {
buildFeatures{
dataBinding true // for data binding
viewBinding true // for view binding
}
}
This warning occurs because
dataBinding {
enabled=true
}
viewBinding {
enabled=true
}
This code style is deprecated and it will remove from the gradle version 5
now if you still want to use this then you can use androidx legacy support dependencies
in app lavel build.gradle
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
otherwise you can use new code style to enable data binding and view binding
like this
android {
buildFeatures {
dataBinding = true
// for view binding:
// viewBinding = true
}
}
Put this code in Gradle Scripts >> build.gradle(Module: appName.app)
after the buildTypes, include the data biding code
buildTypes {
release {
.......
........
}
}
//here is the code...
buildFeatures {
dataBinding = true
}
That's all :)
If you're looking for the new feature viewBinding, try this for Groovy
android {
...
buildFeatures {
viewBinding true
}
}
and this for Kotlin
android {
...
buildFeatures {
viewBinding true
}
}
But, to use the default android data binding
android {
...
buildFeatures {
dataBinding true
}
}
also, be aware to use
kapt "com.android.databinding:compiler:4.0.0"
1- add dataBinding under buildFeatures like this:
android {
...
buildFeatures {
dataBinding true
}
...
}
2- Change dagger version to 2.31.2:
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
3- Change also butterKnife version to 10.2.3:
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
buildFeatures {
//just for dataBinding ,It has nothing to do with viewBinding
dataBinding = true
//just for viewBinding ,It has nothing to do with dataBinding
viewBinding = true
}
Look at the notes above,so it should be very clear
The following works:
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.poet.navviewmodeljave"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
//dataBinding.enabled true
buildFeatures.dataBinding
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
You have to add buildFeatures in your gradle module
android {
buildFeatures{
dataBinding true
}
}
example:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
defaultConfig {
applicationId "com.demo"
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildFeatures{
dataBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'org.jetbrains:annotations:15.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

Jetpack Compose can't import Text or setContent anymore

I've been messing around with Jetpack Compose recently and I had gone through the basic tutorial here. Then I started looking at the Jetnewssample project they have. Now I'm ready to start working on my own project but now when I create a new project in the same parent directory as the Jetnewssample project, (which is working fine) Android Studio can no longer import androidx.ui.core.Text or androidx.ui.core.setContent. I can import other classes from the same location but now I just get an unresolved reference error. This is a new project set to start with an Empty Compose Activity. Here is the code:
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.Composable
import androidx.ui.core.Text
import androidx.ui.core.setContent
import androidx.ui.material.MaterialTheme
import androidx.ui.tooling.preview.Preview
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Greeting("Android")
}
}
}
}
#Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
#Preview
#Composable
fun DefaultPreview() {
MaterialTheme {
Greeting("Android")
}
}
Here's the module's gradle build file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.franklin.sanctified"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
compose true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.ui:ui-layout:0.1.0-dev03'
implementation 'androidx.ui:ui-material:0.1.0-dev03'
implementation 'androidx.ui:ui-tooling:0.1.0-dev03'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Update on :May-2021
Add below dependencies in app level Gradle or kts file for set content{ .. }
implementation("androidx.activity:activity-compose:1.3.0-alpha07")
Those seem to be in ui-framework for dev03. Try adding that dependency to your lineup:
implementation "androidx.ui:ui-framework:0.1.0-dev03"
In my case the problem was that I was missing activities integration for compose. setContent is an extension function on ComponentActivity class, defined in package androidx.activity.compose
Just import:
implementation 'androidx.activity:activity-compose:1.4.0'

I added it when I made the project. AndroidX Artifact has not been mqt communication since then. What is the problem?

I added it when I made the project. AndroidX Artifact(checkBox) has not been mqt communication since then. What is the problem?
Androidx is used to use BiometricPrompt.
Below is my mqtt code.↓
private lateinit var mqttAndroidClient: MqttAndroidClient
val CLINET_ID: String = MqttClient.generateClientId()
fun connect(applicationContext : Context) {
val context: Context = applicationContext
mqttAndroidClient = MqttAndroidClient ( context.applicationContext,"tcp://13.124.231.98:1883",
CLINET_ID )
try {
val token = mqttAndroidClient.connect()
token.actionCallback = object : IMqttActionListener {
override fun onSuccess(asyncActionToken: IMqttToken) {
Log.i("Connection", "success ")
//connectionStatus = true
// Give your callback on connection established here
// publish("test", "open")
}
override fun onFailure(asyncActionToken: IMqttToken, exception: Throwable) {
//connectionStatus = false
Log.i("Connection", "failure")
// Give your callback on connection failure here
exception.printStackTrace()
}
}
} catch (e: MqttException) {
// Give your callback on connection failure here
e.printStackTrace()
}
}
Errors in the code above -> val token = mqttAndroidClient.connect()
My Build
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 28
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-
rules.pro'
}
}
buildToolsVersion '28.0.3'
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-rc01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//fingerPrint
implementation 'androidx.biometric:biometric:1.0.0-beta01'
//mqtt
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.2.2'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
Is it a version problem?
I tried BiometricPrompt. in the version other than androidx to solve this problem, but an error occurred in the code that generated the Fragment and failed to resolve it.
Finally, I thought it would be good to run mqtt on Androidx, so I tried several times but failed.
If you've had a similar experience with me and you've solved it, help.
You should add the following dependencies to with androidx
androidx.legacy:legacy-support-v4:1.0.0
androidx.localbroadcastmanager:localbroadcastmanager:1.0.0
Find the conversion of the issue in GitHub here

Categories

Resources