How to pass an enum in AIDL? - android

Im trying to pass enums using AIDL and it's wrapping a Parcelable but it's still failing to compile with the following error:
> Task :app:compileDebugAidl FAILED
ERROR: uk.ac.liverpool.altphone.service.Mode: couldn't find import for class uk.ac.liverpool.altphone.service.Mode
My files are as follows:
Project Level build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.20"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
def nav_version = "2.3.3"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module Level build.gradle:
plugins {
id 'com.android.application'
id "androidx.navigation.safeargs"
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-parcelize'
id "com.google.protobuf" version "0.8.12"
}
preBuild {
doFirst {
new ProcessBuilder("java", "$rootDir/scripts/Zipper.java")
.directory(new File("$rootDir"))
.start()
.waitFor()
}
}
android {
compileSdkVersion 29
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "uk.ac.liverpool.altphone"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
javaCompileOptions {
annotationProcessorOptions {
arguments += ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
viewBinding true
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
tasks.withType(JavaCompile) {
configure(options) {
options.debug = true
options.warnings = true
options.compilerArgs << '-Xlint:all' << '-parameters' << '-g:source,lines,vars'
}
}
}
dependencies {
implementation 'androidx.activity:activity-ktx:1.2.0-beta02'
implementation 'androidx.appcompat:appcompat:1.3.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0-alpha1'
implementation 'androidx.core:core-ktx:1.5.0-alpha05'
implementation 'androidx.datastore:datastore:1.0.0-alpha06'
implementation 'androidx.datastore:datastore-core:1.0.0-alpha06'
implementation 'androidx.fragment:fragment-ktx:1.3.0-beta02'
def nav_version = "2.3.3"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
implementation 'androidx.recyclerview:recyclerview:1.2.0-beta01'
def room_version = "2.3.0-alpha03"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation 'androidx.startup:startup-runtime:1.0.0'
implementation 'com.google.android.material:material:1.3.0-alpha04'
implementation "com.google.protobuf:protobuf-javalite:3.10.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.10.0"
}
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option 'lite'
}
}
}
}
}
AIDL File app/src/main/aidl/uk/ac/liverpool/altphone/service/IAltPhoneServiceInterface.aidl:
// IAltPhoneServiceInterface.aidl
package uk.ac.liverpool.altphone.service;
// Declare any non-default types here with import statements
import uk.ac.liverpool.altphone.service.Mode;
interface IAltPhoneServiceInterface {
oneway void setMode(in Mode mode);
oneway void updateSettings(in Mode mode, in int headphone);
oneway void stop();
}
Kotlin File app/src/main/java/uk/ac/liverpool/altphone/service/AltPhoneService.kt:
package uk.ac.liverpool.altphone.service
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import uk.ac.liverpool.altphone.R
class AltPhoneService : Service() {
override fun onBind(intent: Intent): IBinder {
return null!!
}
}
#Parcelize
public enum class Mode : Parcelable {
SINGLE, ALTERNATE;
companion object {
fun getModeByID(id: Int): Mode? = when (id) {
R.id.single_mode_mi -> SINGLE
R.id.alternate_mode_mi -> ALTERNATE
else -> null
}
}
}

Related

implementing hilt in android with kapt Execution failed for task ':app:kaptDebugKotlin'

I have been using hilt in my several android projects. But since I have updated my android studio to latest version Chipmunk// 2021.2.1, this error comes every time when I just build project
Execution failed for task ':app:kaptDebugKotlin'.
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
java.lang.reflect.InvocationTargetException (no error message)
I am checking it on sample app which is just just one dependency injection, using hilt and kapt.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
allprojects {
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
dependencies {
// Hilt Classpath for Java
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.38.1'
}
}
plugins {
id 'com.android.application' version '7.2.1' apply false
id 'com.android.library' version '7.2.1' apply false
id 'org.jetbrains.kotlin.android' version '1.7.10' apply false
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: 'com.android.application'
apply plugin: 'org.jetbrains.kotlin.android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
android {
compileSdk 32
defaultConfig {
applicationId "com.example.hiltkotlinpractice"
minSdk 21
targetSdk 32
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.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
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.google.dagger:hilt-android:2.38.1")
kapt("com.google.dagger:hilt-android-compiler:2.38.1")
kapt 'androidx.hilt:hilt-compiler:1.0.0'
}
#HiltAndroidApp
class App: Application()
#Module
#InstallIn(ActivityComponent::class)
class AppModule {
#Provides
fun provideInterface(): APIProvider {
return APIProvider()
}
}
class Repo #Inject constructor(
val apiProvider: APIProvider
) {
fun get() = apiProvider.getData()
}
#AndroidEntryPoint
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val repo = Repo(APIProvider())
Log.d(TAG, repo.get())
}
}
Try using "annotationProcessor" instead of the "kapt" in the build.gradle dependencies. Worked for me.

Cannot access 'kotlin.Lazy' which is a supertype of 'androidx.navigation.NavArgsLazy'

I was trying to receive data in a fragment by safeargs. While delegating SendCashFragmentArgs by navArgs() its throwing : "Cannot access 'kotlin.Lazy' which is a supertype of 'androidx.navigation.NavArgsLazy'. Check your module classpath for missing or conflicting dependencies."
gradle(Project)
buildscript {
ext.kotlin_version = "1.4.32"
ext.nav_version = "2.3.5"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
gradle(app)
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.aj19990321.navigationcomponenetu4u"
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
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//val nav_version = "2.3.5"
// Kotlin
implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
implementation("androidx.navigation:navigation-ui-ktx:$nav_version")
}

gRPC code not being generated from proto file

I followed this guide to add gRPC to my Android project, but the proto file does not seem to generate code.
I placed book.proto under app\src\main\java\com\example\android together with my Kotlin code.
That's my project's build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Tar's:
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.15' // gRPC
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
That's my module build.gradle:
plugins {
id 'com.android.application'
id 'com.google.protobuf' // Tar's: gRPC
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.android"
minSdkVersion 28
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'
}
buildFeatures { // Tar: for using ObservableList, which is part of the Data Binding Library - see https://stackoverflow.com/questions/66352403/observablelist-is-missing-in-android-studio and https://developer.android.com/topic/libraries/data-binding/start
dataBinding true
}
}
// Tar's: gRPC {
protobuf {
protoc { artifact = 'com.google.protobuf:protoc:3.10.0' }
plugins {
javalite { artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0" }
grpc { artifact = 'io.grpc:protoc-gen-grpc-java:1.25.0' // CURRENT_GRPC_VERSION
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc { // Options added to --grpc_out
option 'lite' }
}
}
}
}
// }
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.gridlayout:gridlayout:1.0.0'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
// Tar's:
implementation "org.java-websocket:Java-WebSocket:1.5.1" // Webscokets
//implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0" // Tar: for JSON: https://github.com/Kotlin/kotlinx.serialization
// Tar's - gRPC: {
implementation 'com.google.protobuf:protobuf-lite:3.0.1'
implementation 'io.grpc:grpc-okhttp:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-protobuf-lite:1.25.0' // CURRENT_GRPC_VERSION
implementation 'io.grpc:grpc-stub:1.25.0' // CURRENT_GRPC_VERSION
implementation 'javax.annotation:javax.annotation-api:1.3.2'
// }
}
What could be the problem?
Looks like the path of proto file is not correcttly. Try to move proto files to src/main/resouces/proto or set path in protobuf plugin configuration.
This is works well with kotlin DSL.
import com.google.protobuf.gradle.*
plugins {
java
idea
application
id("com.google.protobuf") version "0.8.14"
id("io.freefair.lombok") version "5.3.0"
}
repositories {
mavenCentral()
jcenter()
}
val grpcVersion = "1.34.1"
val protocVersion = "3.12.0"
val slf4jVersion = "1.7.25"
dependencies {
implementation("io.grpc:grpc-netty:${grpcVersion}")
implementation("io.grpc:grpc-protobuf:${grpcVersion}")
implementation("io.grpc:grpc-stub:${grpcVersion}")
implementation("org.slf4j:slf4j-api:${slf4jVersion}")
implementation("org.slf4j:slf4j-simple:${slf4jVersion}")
testImplementation("junit:junit:4.13")
}
// Look here for set path
sourceSets {
main {
proto {
srcDir("src/main/resources/proto")
}
}
}
protobuf {
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
plugins {
id("grpc") {
artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}"
}
}
generateProtoTasks {
ofSourceSet("main").forEach {
it.plugins {
id("grpc")
}
}
}
}

Cannot implement #Parcelize annotation

I am trying to implement #Parcelize annotation in kotlin to a data class, but it looks as if I forgot to implement something. Even when I try to import kotlinx.parcelize, Android Studio does not know the parcelize option. I implemented kotlin-parcelize and kotlin version is 1.4.21, which should be okay.
Here are my Gradle files:
plugins {
id 'com.android.application'
id 'kotlin-parcelize'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.benzeneapps.ciphers"
minSdkVersion 22
targetSdkVersion 29
versionCode 1
versionName "0.0.1"
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 {
viewBinding true
dataBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "androidx.core:core-ktx:1.3.2"
implementation "androidx.room:room-ktx:2.2.6"
kapt "androidx.room:room-compiler:2.2.6"
androidTestImplementation "androidx.room:room-testing:2.2.6"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.2.0"
}
repositories {
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
mavenCentral()
}
Project Gradle File
buildscript {
repositories {
google()
jcenter()
maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
How I tried to implement it.
import kotlinx.parcelize.Parcelize
import android.os.Parcelable
#Parcelize
data class Cipher(val id: Int, val name: String): Parcelable
Apply kotlin-android before kotlin-parcelize:
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-parcelize'
}
This works for me Add this to your build.gradle file
plugins {
id 'kotlin-android-extensions'
}

The setValue() function has Unresolved reference

fun saveImageInFirebase(){
var currentUser =mAuth!!.currentUser
val email:String=currentUser!!.email.toString()
val storage=FirebaseStorage.getInstance()
val storgaRef=storage.getReferenceFromUrl("gs://gameudemy.appspot.com")
val df=SimpleDateFormat("ddMMyyHHmmss")
val dataobj=Date()
val imagePath= splitString(email) + "."+ df.format(dataobj)+ ".jpg"
val ImageRef=storgaRef.child("images/"+imagePath )
imageSpace.isDrawingCacheEnabled=true
imageSpace.buildDrawingCache()
val drawable=imageSpace.drawable as BitmapDrawable
val bitmap=drawable.bitmap
val baos=ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos)
val data= baos.toByteArray()
val uploadTask=ImageRef.putBytes(data)
uploadTask.addOnFailureListener{
Toast.makeText(applicationContext,"fail to upload",Toast.LENGTH_LONG).show()
}.addOnSuccessListener { taskSnapshot ->
var DownloadURL= taskSnapshot.storage.downloadUrl.toString()!!
myRef.child("Users").child(currentUser.uid).child("email").setValue(currentUser.email)
myRef.child("Users").child(currentUser.uid).child("ProfileImage").setValue(DownloadURL)
loadTweets()
}
}
The setValue method called turns red and says Unresolved reference.
How to resolve it?
Gradle structure->
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.shanu.sevenstar"
minSdkVersion 24
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'
}
}
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'com.google.firebase:firebase-analytics:17.6.0'
implementation 'com.google.firebase:firebase-core:17.5.1'
implementation 'com.google.firebase:firebase-auth:19.4.0'
implementation 'com.google.firebase:firebase-database:19.5.0'
implementation 'com.google.firebase:firebase-storage:19.2.0'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.8"
}
Error returned is
Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public inline operator fun MutableMap<in String, in TypeVariable(V)>.setValue(thisRef: Any?, property: KProperty<>, value: TypeVariable(V)): Unit defined in kotlin.collections
public inline operator fun KMutableProperty0<TypeVariable(V)>.setValue(thisRef: Any?, property: KProperty<>, value: TypeVariable(V)): Unit defined in kotlin
public inline operator fun <T, V> KMutableProperty1<TypeVariable(T), TypeVariable(V)>.setValue(thisRef: TypeVariable(T), property: KProperty<*>, value: TypeVariable(V)): Unit defined in kotlin

Categories

Resources