1

I've set up a Kotlin multiplatform project. I have a shared project and an Android project. I've compiled the shared project and it creates .AAR and .Jar files with 1 method.

For easy syncing of changes, I've added the maven-publish plugin:

plugins {
    id("maven-publish")
}

I generate my jars and aars and use publishToMavenLocal:

./gradlew build
./gradlew publishToMavenLocal

When I go into the local directory I indeed see my library:

/Users/{MY_USERNAME}/.m2/repository/mylibrary/nameOfMyLibrary/1.0/nameOfMyLibrary-1.0-kotlin-tooling-metadata.json
/Users/{MY_USERNAME}/.m2/repository/mylibrary/nameOfMyLibrary/1.0/nameOfMyLibrary-1.0-sources.jar
/Users/{MY_USERNAME}/.m2/repository/mylibrary/nameOfMyLibrary/1.0/nameOfMyLibrary-1.0.jar
/Users/{MY_USERNAME}/.m2/repository/mylibrary/nameOfMyLibrary/1.0/nameOfMyLibrary-1.0.module
/Users/{MY_USERNAME}/.m2/repository/mylibrary/nameOfMyLibrary/1.0/nameOfMyLibrary-1.0.pom

Then I open the Android project and add to project level build gradle:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral artifactUrls:["file:/home/{MY_USERNAME}/.m2/repository/"]
    }
}

In settings.gradle I also add the maven Local:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenLocal()
        google()
        mavenCentral()
    }
}

Then I include it in the build.gradle app level:

dependencies {
    implementation "mylibrary:nameOfMyLibrary:1.0"
}

Then I sync Gradle and in my MainActivity I try to import a file from the library:

import com.mycompanyname.SomeClass

This doesn't work, the library is not found. Also when I try to import other repositories from that m2 directory, they are not found. Any ideas?

I've turned of my internet and then Android Studio asked:

"Do you want to enable Gradle offline mode". 

But this also didn't help.

Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94
  • Does it work when building from the command line? – nhaarman Jul 13 '23 at 16:01
  • hi @nhaarman, it doesn't recognize the import so it's not building. – Jim Clermonts Jul 14 '23 at 10:10
  • Hey @JimClermonts, looks like `shared` and `androidApp` are within single project? If so, any reason not to use `implementation(project(":shared"))` way to add dependencies to `androidApp` rather than publishing to Maven local? – Alex Dmitriev Jul 17 '23 at 13:40
  • Hi with your implementation, I have to make an .aar file and copy-paste it into my androidApp project. I want a simpler solution. – Jim Clermonts Jul 18 '23 at 08:36

1 Answers1

0

Set local maven repository like this and put url in it:

buildscript {
    repositories {
        mavenLocal {
            url 'c:\\AndroidMaven\\repository'
        }
        mavenCentral()
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        mavenLocal {
            url 'c:\\AndroidMaven\\repository'
        }
        google()
        mavenCentral()
    }
}

Add implementation line to gradle using package name, module name, and version:

implementation 'com.test.package:ModuleName:1.0.0'

Also you can customize local maven path by adding this config in library's gradle.properties file:

systemProp.maven.repo.local=[Path like C:\\AndroidMaven\\repository]
Homayoon Ahmadi
  • 1,181
  • 1
  • 12
  • 24