I was given a task to convert an existing android app so it works on iOS using kotlin multiplatform. I will mention that I am very green in this area and not experienced at all, so I would highly appreciate guidance. After creating the shared module and fidgeting with the build.gradle files I finally got the project to build, however once I moved a java file from androidApp module to the shared module and converted it to a kotlin file, every import and further code throws an unresolved reference error. For example I have this file that was originaly in java:
<code>import android.content.Intent;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FbInstanceIdService extends FirebaseInstanceIdService{
public static final String TAG = "FirebaseID";
public static final String TOKEN_BROADCAST = "tokenbroadcast";
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
storeToken(refreshedToken);
private void storeToken(String token){
SharedPreferencesManager.getInstance((getApplicationContext())).storeFirebaseToken(token);
<code>import android.content.Intent;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FbInstanceIdService extends FirebaseInstanceIdService{
public static final String TAG = "FirebaseID";
public static final String TOKEN_BROADCAST = "tokenbroadcast";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
storeToken(refreshedToken);
}
private void storeToken(String token){
SharedPreferencesManager.getInstance((getApplicationContext())).storeFirebaseToken(token);
}
}
</code>
import android.content.Intent;
import android.util.Log;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
public class FbInstanceIdService extends FirebaseInstanceIdService{
public static final String TAG = "FirebaseID";
public static final String TOKEN_BROADCAST = "tokenbroadcast";
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(new Intent(TOKEN_BROADCAST));
storeToken(refreshedToken);
}
private void storeToken(String token){
SharedPreferencesManager.getInstance((getApplicationContext())).storeFirebaseToken(token);
}
}
This is the same code converted to kotlin:
<code>package eu.tinysolutions.tinyprocas
import android.content.Intent
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.FirebaseInstanceIdService
class FbInstanceIdService : FirebaseInstanceIdService() {
// Get updated InstanceID token.
val refreshedToken: String = FirebaseInstanceId.getInstance().getToken()
"Refreshed token: $refreshedToken"
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(Intent(TOKEN_BROADCAST))
storeToken(refreshedToken)
private fun storeToken(token: String) {
SharedPreferencesManager.getInstance(getApplicationContext()).storeFirebaseToken(token)
const val TAG = "FirebaseID"
const val TOKEN_BROADCAST = "tokenbroadcast"
<code>package eu.tinysolutions.tinyprocas
import android.content.Intent
import android.util.Log
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.FirebaseInstanceIdService
class FbInstanceIdService : FirebaseInstanceIdService() {
fun onTokenRefresh() {
// Get updated InstanceID token.
val refreshedToken: String = FirebaseInstanceId.getInstance().getToken()
android.util.Log.d(
TAG,
"Refreshed token: $refreshedToken"
)
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(Intent(TOKEN_BROADCAST))
storeToken(refreshedToken)
}
private fun storeToken(token: String) {
SharedPreferencesManager.getInstance(getApplicationContext()).storeFirebaseToken(token)
}
companion object {
const val TAG = "FirebaseID"
const val TOKEN_BROADCAST = "tokenbroadcast"
}
}
</code>
package eu.tinysolutions.tinyprocas
import android.content.Intent
import android.util.Log
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.FirebaseInstanceIdService
class FbInstanceIdService : FirebaseInstanceIdService() {
fun onTokenRefresh() {
// Get updated InstanceID token.
val refreshedToken: String = FirebaseInstanceId.getInstance().getToken()
android.util.Log.d(
TAG,
"Refreshed token: $refreshedToken"
)
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
getApplicationContext().sendBroadcast(Intent(TOKEN_BROADCAST))
storeToken(refreshedToken)
}
private fun storeToken(token: String) {
SharedPreferencesManager.getInstance(getApplicationContext()).storeFirebaseToken(token)
}
companion object {
const val TAG = "FirebaseID"
const val TOKEN_BROADCAST = "tokenbroadcast"
}
}
I got errors like these:
Unresolved reference: content
Unresolved reference: util
Unresolved reference: FirebaseInstanceId
Unresolved reference: FirebaseInstanceIdService
Unresolved reference: getApplicationContext
and so on…
There are a ton more files but everywhere the errors remain similar.
I mostly tried changing the build.gradle files, however no errors were solved. I will post them in case they do help solve the problem.
Project level build.gradle file:
<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
classpath('com.android.tools.build:gradle:8.1.4')
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0"
classpath 'com.google.gms:google-services:4.4.1'
// Declare the Kotlin Multiplatform plugin with a specific version and set apply false
id 'org.jetbrains.kotlin.multiplatform' version '1.9.0' apply false
id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.4' apply false
id("com.google.gms.google-services") version "4.4.1" apply false
task clean(type: Delete) {
delete rootProject.buildDir
<code>// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath('com.android.tools.build:gradle:8.1.4')
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0"
classpath 'com.google.gms:google-services:4.4.1'
}
}
plugins {
// Declare the Kotlin Multiplatform plugin with a specific version and set apply false
id 'org.jetbrains.kotlin.multiplatform' version '1.9.0' apply false
id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.4' apply false
id("com.google.gms.google-services") version "4.4.1" apply false
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
</code>
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath('com.android.tools.build:gradle:8.1.4')
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0"
classpath 'com.google.gms:google-services:4.4.1'
}
}
plugins {
// Declare the Kotlin Multiplatform plugin with a specific version and set apply false
id 'org.jetbrains.kotlin.multiplatform' version '1.9.0' apply false
id 'com.android.application' version '8.1.4' apply false
id 'com.android.library' version '8.1.4' apply false
id("com.google.gms.google-services") version "4.4.1" apply false
}
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
androidApp build.gradle file:
<code>apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
applicationId "changedID"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
applicationVariants.all { variant ->
variant.resValue "string", "versionName", variant.versionName
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
namespace 'changedNamespace'
maven { url 'https://jitpack.io' }
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.ms-square:expandableTextView:0.1.4'
implementation project(':shared')
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
apply plugin: 'com.google.gms.google-services'
<code>apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdk 27
defaultConfig {
applicationId "changedID"
minSdkVersion 26
targetSdkVersion 27
versionCode 11
versionName "1.11"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
applicationVariants.all { variant ->
variant.resValue "string", "versionName", variant.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
namespace 'changedNamespace'
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.ms-square:expandableTextView:0.1.4'
implementation project(':shared')
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
apply plugin: 'com.google.gms.google-services'
</code>
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdk 27
defaultConfig {
applicationId "changedID"
minSdkVersion 26
targetSdkVersion 27
versionCode 11
versionName "1.11"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
applicationVariants.all { variant ->
variant.resValue "string", "versionName", variant.versionName
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
namespace 'changedNamespace'
}
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.ms-square:expandableTextView:0.1.4'
implementation project(':shared')
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
apply plugin: 'com.google.gms.google-services'
shared module build.gradle.kts file:
//alias(libs.plugins.kotlinMultiplatform)
//alias(libs.plugins.androidLibrary)
//alias(libs.plugins.androidApplication)
id("org.jetbrains.kotlin.multiplatform")
id("com.android.application")
id("com.google.gms.google-services")
maven(url = "https://jitpack.io")
val commonMain by getting {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
val androidMain by getting{
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
val commonTest by getting {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
// Specific platform configurations
val iosX64Main by getting {
val iosArm64Main by getting {
val iosSimulatorArm64Main by getting {
namespace = "changedNamespace"
applicationId = "changedID"
<code>plugins {
//alias(libs.plugins.kotlinMultiplatform)
//alias(libs.plugins.androidLibrary)
//alias(libs.plugins.androidApplication)
id("org.jetbrains.kotlin.multiplatform")
id("com.android.application")
id("com.google.gms.google-services")
}
repositories {
maven(url = "https://jitpack.io")
}
kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "shared"
isStatic = true
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
}
}
val androidMain by getting{
dependencies{
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
// Specific platform configurations
val iosX64Main by getting {
dependsOn(commonMain)
}
val iosArm64Main by getting {
dependsOn(commonMain)
}
val iosSimulatorArm64Main by getting {
dependsOn(commonMain)
}
}
}
android {
namespace = "changedNamespace"
compileSdk = 27
defaultConfig {
minSdk = 26
applicationId = "changedID"
versionCode = 11
versionName = "1.11"
targetSdk = 27
}
}
</code>
plugins {
//alias(libs.plugins.kotlinMultiplatform)
//alias(libs.plugins.androidLibrary)
//alias(libs.plugins.androidApplication)
id("org.jetbrains.kotlin.multiplatform")
id("com.android.application")
id("com.google.gms.google-services")
}
repositories {
maven(url = "https://jitpack.io")
}
kotlin {
androidTarget {
compilations.all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
listOf(
iosX64(),
iosArm64(),
iosSimulatorArm64()
).forEach {
it.binaries.framework {
baseName = "shared"
isStatic = true
}
}
sourceSets {
val commonMain by getting {
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
}
}
val androidMain by getting{
dependencies{
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
implementation(project(":shared"))
implementation ("com.android.support:appcompat-v7:27.1.1")
implementation ("com.android.support.constraint:constraint-layout:1.1.1")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.google.firebase:firebase-messaging:11.8.0")
implementation ("com.android.support:support-v4:27.1.1")
implementation ("com.android.support:design:27.1.1")
implementation ("com.android.volley:volley:1.1.0")
implementation ("com.ms-square:expandableTextView:0.1.4")
implementation ("com.github.PhilJay:MPAndroidChart:v3.0.3")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
// Specific platform configurations
val iosX64Main by getting {
dependsOn(commonMain)
}
val iosArm64Main by getting {
dependsOn(commonMain)
}
val iosSimulatorArm64Main by getting {
dependsOn(commonMain)
}
}
}
android {
namespace = "changedNamespace"
compileSdk = 27
defaultConfig {
minSdk = 26
applicationId = "changedID"
versionCode = 11
versionName = "1.11"
targetSdk = 27
}
}