I’m trying to integrate a Kotlin Multiplatform library with an existing Android app. I’d like to drop both git repositories into a root directory and then include the library in the app using these small changes.
This is a patch file from two test projects I created with the Android Studio wizard. One was a KMM project and the other was an Android app that just has the built in login screen activity.
---
.idea/gradle.xml | 2 +-
app/build.gradle | 1 +
.../java/com/example/myapp/data/LoginDataSource.kt | 4 +++-
settings.gradle | 2 ++
4 files changed, 7 insertions(+), 2 deletions(-)
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index 4e3844e..10c8697 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -10,9 +10,9 @@
<set>
<option value="$PROJECT_DIR$" />
<option value="$PROJECT_DIR$/app" />
+ <option value="$PROJECT_DIR$/../mylib/shared" />
</set>
</option>
- <option name="resolveModulePerSourceSet" value="false" />
</GradleProjectSettings>
</option>
</component>
diff --git a/app/build.gradle b/app/build.gradle
index 4b52039..a383303 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -43,6 +43,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.5.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.5.1'
+ implementation project(path: ':mylib')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
diff --git a/app/src/main/java/com/example/myapp/data/LoginDataSource.kt b/app/src/main/java/com/example/myapp/data/LoginDataSource.kt
index a5990c5..2010210 100644
--- a/app/src/main/java/com/example/myapp/data/LoginDataSource.kt
+++ b/app/src/main/java/com/example/myapp/data/LoginDataSource.kt
@@ -1,5 +1,6 @@
package com.example.myapp.data
+import com.example.mylib.Greeting
import com.example.myapp.data.model.LoggedInUser
import java.io.IOException
@@ -11,7 +12,8 @@ class LoginDataSource {
fun login(username: String, password: String): Result<LoggedInUser> {
try {
// TODO: handle loggedInUser authentication
- val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), "Jane Doe")
+// val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), "Jane Doe")
+ val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), Greeting().greeting())
return Result.Success(fakeUser)
} catch (e: Throwable) {
return Result.Error(IOException("Error logging in", e))
diff --git a/settings.gradle b/settings.gradle
index 3e6c77c..5a702f5 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -14,3 +14,5 @@ dependencyResolutionManagement {
}
rootProject.name = "myapp"
include ':app'
+include ':mylib'
+project(':mylib').projectDir = new File('../mylib/shared')
\ No newline at end of file
--
2.36.1
This solution works well with the sample projects. However when I try it with the actual existing projects I get an error, “Task ‘wrapper’ not found in project ‘:mylib’”. I see this error when I try to sync the project with the gradle files. Building the KMM project works fine too.
I’ve read a lot of documentation, but I don’t see an issue since that task should be created automatically. I only get this error when I try build the existing app that includes the shared library of the KMM project.
Any ideas on what I should try to fix this issue?