Skip to content

Commit

Permalink
Merge "[fused lib] Publish sample in 8.9 & fix formatting" into studi…
Browse files Browse the repository at this point in the history
…o-main
  • Loading branch information
Treehugger Robot authored and Android (Google) Code Review committed Jan 17, 2025
2 parents 6a48436 + 904dcd0 commit d70bf88
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 73 deletions.
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ kotlin_library(
"//tools/base/bazel:gradle",
"//tools/base/common:tools.common",
"//tools/base/testutils:tools.testutils",
"@maven//:com.google.code.gson.gson",
"@maven//:junit.junit",
],
)
Expand Down
46 changes: 25 additions & 21 deletions recipes/applyFusedLibraryPlugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ by other libraries.
│ :androidLib3 com.google.code.gson:gson* │
└─────────────────────────────────────────┘
</pre>
This diagram shows an overview of the relevant project dependency structure.
* include dependency of :fusedLibrary
This diagram shows an overview of the relevant project dependency structure.

`*` indicates an `include` dependency of the `:fusedLibrary` module

Example usages of classes, resources and other artifacts are demonstrated in the :app module unit
and instrumentation tests.
Expand All @@ -48,29 +49,29 @@ and instrumentation tests.

1. Apply the plugin
gradle/libs.versions.toml append
```kts
```toml
[plugins]
...
android-fusedlibrary = { id = "com.android.fusedlibrary", version.ref = "agp" }
```
2. Create a new module. `File` > `New Module...` . Then, click `Android Library` and fill out the
required module metadata. Click `Finish`.
3. In the new module (let's call it `:fusedLibrary`) open the `build.gradle.kts` file,
then replace the `plugins` block with
```
```kts
plugins {
alias(libs.plugins.android.fusedlibrary)
}
```
to apply the Fused Library Plugin
4. Fused library modules cannot not contain sources such as code or resources, nor does it use
the typical `implementation` or `api` configurations you may expect to declare as dependencies.

Done.

Fused library introduces a new configuration `include`, that declares what dependencies will be
fused in the built/published .aar file.

For example, the :fusedLibrary could define the following in the `dependencies` block:
For example, the `:fusedLibrary` could define the following in the `dependencies` block:

```kts
dependencies {
Expand All @@ -91,25 +92,27 @@ dependencies {
2. Once you are satisfied, you can proceed to build the library using
`./gradlew :fusedLibrary:assemble`. Assuming dependencies are valid,
this task produces the .aar fused library at `fusedLibrary/build/bundle/bundle.aar`.
3. Resync project ctrl+shift+O
3. Resync project `ctrl+shift+O`

Done.

At this point you can add the fused library as a dependency from other modules.
At this point, you can add the fused library as a dependency from other modules.

### Running the consumption tests

In the :app module there are tests that make use of the classes and resource distributed via
:fusedLibrary.
`:fusedLibrary`.

Run unit tests: `./gradlew :app:testDebugUnitTest --tests "com.example.fusedlibrarysample.FusedLibraryConsumptionUnitTest"`

Run instrumentation tests: `./gradlew :app:connectedDebugAndroidTest`

### Publishing the fused library

Fused Library Plugin artifacts can be easily configured for publication with Maven publishing
plugins. The plugin generates it's own POM for distribution that preserves the artifact dependencies.
We'll provide some typical configurations for publishing that may be useful for most use cases,
however if your needs are more complex, consult the Maven documentation.
We'll provide some typical configurations for publishing that may be useful for most use cases
however, if your needs are more complex, consult the Maven documentation.

Generating the fused lib POM
1. Follow the steps of `Building a fused library`
Expand Down Expand Up @@ -143,12 +146,13 @@ Generating a maven repository with the fused library
}
```
2. Execute the task for creating the repository `./gradlew fusedLibrary:publishReleasePublicationToMyrepoRepository`
2. Execute the task for creating the repository `./gradlew :fusedLibrary:publishReleasePublicationToMyrepoRepository`
3. As androidLib3 is a project dependency of the fused library, that also needs to be published to
the repository`./gradlew androidLib3:publishMavenPublicationToMyrepoRepository`
4. Note: :app has already configured dependency substitution that prefers the published local repo
artifacts over the :fusedLibrary project itself, so :app now automatically depends on the correct
the repository`./gradlew :androidLib3:publishMavenPublicationToMyrepoRepository`
4. Note: `:app` has already configured dependency substitution that prefers the published local repo
artifacts over the `:fusedLibrary` project itself, so `:app` now automatically depends on the correct
artifacts.

Done.

### Configurations
Expand All @@ -162,16 +166,16 @@ Done.
This plugin remains in early stages, and there may be corner cases that have not been fully tested
or developed.

See open public issues at this link [open issues](https://issuetracker.google.com/issues?q=hotlistid:4053459)
See open public issues at this link [open issues](https://issuetracker.google.com/components/1692458)

Follow the below steps and use this [link to **file new bugs**](https://issuetracker.google.com/createIssue?title=%5Bfused+lib+-+public%5D+%3CIssue+Name+Here%3E&cc=lukeedgar%40google.com%2C+android-gradle%40google.com&description=Please+include+all+of+the+following%3A%0A1.+Steps+to+reproduce%0A2.+A+paste+of+the+exception%0A3.+run+%60.%2Fgradlew+%3A%3Cfused+library+module%5C%3E%3Areport%60+and+paste+the+contents+of+%0A%60%3Cmy+library+module%5C%3Ebuild%2Freports%2Ffused_library_report%2Fsingle%2Freport.json%60%0A4.Also+consider+running+%60gradle+%3A%3Cfused+library+module%3E%3Adependencies%60+if+dependency+information+is+relevant%0A5.%5Boptional%5D+if+the+build+was+successful%2C+provide+a+copy+of+the+.aar&format=MARKDOWN&component=192709&type=BUG&priority=P2&severity=S2&hotlistIds=4053459&assignee=lukeedgar%40google.com)
Follow the below steps and use this [link to **file new bugs**](https://issuetracker.google.com/issues/new?title=%5Bfused+lib+-+public%5D+%3CIssue+Name+Here%3E&cc=lukeedgar%40google.com%2C+android-gradle%40google.com&description=1.+Steps+to+reproduce%0A2.+A+paste+of+the+exception%0A3.+run+%60.%2Fgradlew+%3A%3Cfused+library+module%3E%3Areport%60+and+paste+the+contents+of+%0A%60%3Cmy+library+module%3Ebuild%2Freports%2Ffused_library_report%2Fsingle%2Freport.json%60%0A4.+Also+consider+running+%60.%2Fgradlew+%3A%3Cfused+library+module%3E%3Adependencies%60+if+dependency+information+is+relevant%0A5.+%5C%5Boptional%5C%5D+if+the+build+was+successful%2C+provide+a+copy+of+the+.aar&format=MARKDOWN&component=192708&type=BUG&priority=P2&severity=S2&hotlistIds=4053459&assignee=lukeedgar%40google.com)
**or provide suggestions** for the Fused Library Plugin.

When filing an issue, please include the following information:
1. Steps to reproduce
2. A paste of the exception
3. run `\gradle :\<fused library module\>:report\` and paste the contents of
`\<my library module\>build/reports/fused_library_report/single/report.json`
4.Also consider running \`gradle :\<fused library module\>:dependencies\` if dependency information is relevant
5.\[optional\] if the build was successful, provide a copy of the .aar
3. run `./gradlew :<fused library module>:report` and paste the contents of
`<my library module>build/reports/fused_library_report/single/report.json`
4. Also consider running `./gradlew :<fused library module>:dependencies` if dependency information is relevant
5. \[optional\] if the build was successful, provide a copy of the .aar
Done.
12 changes: 3 additions & 9 deletions recipes/applyFusedLibraryPlugin/androidLib1/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ plugins {

android {
namespace = "com.example.androidlib1"
compileSdk = 34
compileSdk = $COMPILE_SDK

defaultConfig {
minSdk = 34
minSdk = $MINIMUM_SDK

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
Expand Down Expand Up @@ -54,11 +54,5 @@ android {
}

dependencies {
implementation("com.google.code.gson:gson:2.11.0")
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
implementation("com.google.code.gson:gson:2.10.1")
}
11 changes: 2 additions & 9 deletions recipes/applyFusedLibraryPlugin/androidLib2/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ plugins {

android {
namespace = "com.example.androidlib2"
compileSdk = 34
compileSdk = $COMPILE_SDK

defaultConfig {
minSdk = 34
minSdk = $MINIMUM_SDK

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -41,11 +41,4 @@ android {

dependencies {
implementation(project(":androidLib3"))
implementation("com.google.errorprone:error_prone_annotations:2.27.0")
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package com.example.androidlib2

import android.content.Context
import android.provider.Settings.Global.getString

class ClassFromAndroidLib2 {

fun foo(): String {
Expand Down
10 changes: 2 additions & 8 deletions recipes/applyFusedLibraryPlugin/androidLib3/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ plugins {

android {
namespace = "com.example.androidlib3"
compileSdk = 34
compileSdk = $COMPILE_SDK

defaultConfig {
minSdk = 24
minSdk = $MINIMUM_SDK

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
Expand Down Expand Up @@ -66,10 +66,4 @@ publishing {
}

dependencies {
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
11 changes: 3 additions & 8 deletions recipes/applyFusedLibraryPlugin/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ plugins {

android {
namespace = "com.example.fusedlibrarysample"
compileSdk = 35
compileSdk = $COMPILE_SDK

defaultConfig {
applicationId = "com.example.fusedlibrarysample"
minSdk = 34
targetSdk = 34
minSdk = $MINIMUM_SDK
targetSdk = $COMPILE_SDK
versionCode = 1
versionName = "1.0"

Expand All @@ -53,11 +53,6 @@ android {

dependencies {
implementation(project(":fusedLibrary"))
implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import org.junit.Assert.*
*/
@RunWith(AndroidJUnit4::class)
class FusedLibraryInstrumentationTest {

@Test
fun `access resources from fused library`() {
fun accessResourcesFromFusedLibrary() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val stringOverridden =
appContext.getString(com.example.fusedlibrary.R.string.string_overridden)
Expand All @@ -43,7 +43,7 @@ class FusedLibraryInstrumentationTest {
}

@Test
fun `access external libraries from include dependencies`() {
fun accessExternalLibrariesFromIncludeDependencies() {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
val androidLib1AssetInputStream = appContext.assets.open("androidLib1-asset.json")
val jsonString = androidLib1AssetInputStream.bufferedReader().use {it.readText() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import org.junit.Assert.*
class FusedLibraryConsumptionUnitTest {

@Test
fun `test classes from included dependencies`() {
fun testClassesFromIncludedDependencies() {
// Access symbols from local library dependencies
val androidLib1Class = ClassFromAndroidLib1()
val androidLib2Class = ClassFromAndroidLib2()
Expand All @@ -45,7 +45,7 @@ class FusedLibraryConsumptionUnitTest {
}

@Test
fun `test local lib jar dependencies are accessible`() {
fun testLocalLibJarDependenciesAreAccessible() {
// Access class in local jar from fused library file dependencies
// A_DoIExist is class from a libs jar file dependency
// (fusedLibrary/libs/simple-jar-with-A_DoIExist-class.jar)
Expand Down
6 changes: 3 additions & 3 deletions recipes/applyFusedLibraryPlugin/fusedLibrary/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ plugins {

androidFusedLibrary {
namespace = "com.example.fusedlibrary"
minSdk = 34
minSdk = $MINIMUM_SDK

// If aarMetadata is not explicitly specified,
// aar metadata will be generated based on dependencies.
aarMetadata {
minCompileSdk = 19
minCompileSdk = $MINIMUM_SDK
minCompileSdkExtension = 1
}
}
Expand All @@ -51,6 +51,6 @@ publishing {
dependencies {
include(project(":androidLib1"))
include(project(":androidLib2"))
include("com.google.code.gson:gson:2.11.0")
include("com.google.code.gson:gson:2.10.1")
include(files("libs/simple-jar-with-A_DoIExist-class.jar"))
}
8 changes: 4 additions & 4 deletions recipes/applyFusedLibraryPlugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[versions]
agp = "8.9.0-alpha03"
kotlin = "1.9.0"
agp = $AGP_VERSION
kotlin = $KOTLIN_VERSION
coreKtx = "1.15.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
appcompat = "1.7.0"
material = "1.12.0"
activity = "1.8.0"
Expand Down
7 changes: 4 additions & 3 deletions recipes/applyFusedLibraryPlugin/recipe_metadata.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description ="""
"""

[agpVersion]
min = "8.9.0"
min = "8.9.0-alpha08"

# Relevant Gradle tasks to run per recipe
[gradleTasks]
Expand All @@ -19,10 +19,11 @@ tasks = [
":fusedLibrary:publishReleasePublicationToMyrepoRepository",
":fusedLibrary:publishReleasePublicationToMyrepoRepository",
":app:testDebugUnitTest",
":app:connectedDebugAndroidTest"
]

# All the relevant metadata fields to create an index based on language/API/etc'
[indexMetadata]
index = []
index = [
"Plugin Features/Fused Library Plugin"
]

0 comments on commit d70bf88

Please sign in to comment.