Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for embedding dependencies substituted by included builds. #364

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified example/app/libs/fat-aar-final.aar
Binary file not shown.
20 changes: 20 additions & 0 deletions example/app/src/main/java/com/fataar/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.kezong.demo.libaar.KotlinTest2;
import com.kezong.demo.libaar.TestActivity;
import com.kezong.demo.libaar2.Aar2LibClass;
import com.kezong.demo.libaarincluded.AarIncludedLibClass;
import com.kezong.demo.libaarincluded.KotlinTestIncluded;
import com.kezong.demo.libaarlocal.AarLocalLibClass;
import com.kezong.demo.libaarlocal2.AarLocal2LibClass;

Expand All @@ -54,11 +56,14 @@ protected void onCreate(Bundle savedInstanceState) {
testMainLibClassMerge();
testClassMerge();
testClassMerge2();
testClassMerge3Included();
testJarMerge();
testResourceMerge();
testResourceMerge2();
testResourceMerge3Included();
testKotlinTopLevel();
testKotlinTopLevel2();
testKotlinTopLevel3Included();
testLocalAar1();
testLocalAar2();
testSoMerge();
Expand Down Expand Up @@ -161,6 +166,11 @@ public void testKotlinTopLevel2() {
addTestView("kotlin2", text, TextUtils.equals(text, "130"));
}

public void testKotlinTopLevel3Included() {
String text = String.valueOf(KotlinTestIncluded.test2());
addTestView("kotlin3included", text, TextUtils.equals(text, "140"));
}

public void testResourceMerge() {
String text = new AarLibClass().getLibName(this);
addTestView("resource", text, TextUtils.equals(text, "lib-aar"));
Expand All @@ -171,11 +181,21 @@ public void testResourceMerge2() {
addTestView("resource2", text, TextUtils.equals(text, "lib-aar2"));
}

public void testResourceMerge3Included() {
String text = this.getResources().getString(R.string.app_name_aar_included);
addTestView("resource3included", text, TextUtils.equals(text, "lib-aar-included"));
}

public void testClassMerge2() {
String text = Aar2LibClass.TAG;
addTestView("lib class2", text, TextUtils.equals(text, Aar2LibClass.class.getSimpleName()));
}

public void testClassMerge3Included() {
String text = AarIncludedLibClass.TAG;
addTestView("lib class3 included", text, TextUtils.equals(text, AarIncludedLibClass.class.getSimpleName()));
}

public void testMainLibClassMerge() {
String text = String.valueOf(MainLibClass.test());
addTestView("main class", text, TextUtils.equals(text, "200"));
Expand Down
21 changes: 21 additions & 0 deletions example/included-build/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
buildscript {
repositories {
mavenCentral()
google()
jcenter()
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.2'
classpath 'com.github.kezong:fat-aar:1.3.6'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.72"
}
}

allprojects {
repositories {
mavenCentral()
google()
jcenter()
}
}
28 changes: 28 additions & 0 deletions example/included-build/lib-aar-included/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

group("com.kezong.demo.libaarincluded")

android {
compileSdkVersion 29

defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
consumerProguardFiles 'proguard-rules.pro'
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: '*.jar')
}
22 changes: 22 additions & 0 deletions example/included-build/lib-aar-included/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class com.kezong.demo.libaarincluded.AarIncludedLibClass {*;}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kezong.demo.libaarincluded">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

</manifest>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kezong.demo.libaarincluded;

import android.content.Context;

public class AarIncludedLibClass {

public static final String TAG = AarIncludedLibClass.class.getSimpleName();

public static String getLibName(Context ctx) {
return ctx.getResources().getString(R.string.app_name_aar_included);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@file:JvmName("KotlinTestIncluded")
package com.kezong.demo.libaarincluded

fun test2() : Int {
return 140
}

val param = 120
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

</LinearLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name_aar_included">lib-aar-included</string>
</resources>
1 change: 1 addition & 0 deletions example/included-build/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ':lib-aar-included'
5 changes: 3 additions & 2 deletions example/lib-main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {

defaultConfig {
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0.0"
}
Expand Down Expand Up @@ -81,6 +81,8 @@ dependencies {
embed project(path: ':lib-aar', configuration: 'default')
// aar dependency
embed project(path: ':lib-aar2', configuration: 'default')
// aar dependency from included build
embed module(group: "com.kezong.demo.libaarincluded", name: "lib-aar-included", configuration: 'default')
// local full aar dependency, just build in flavor1
flavor1Embed project(path: ':lib-aar-local', configuration: 'default')
// local full aar dependency, just build in debug
Expand All @@ -101,4 +103,3 @@ dependencies {
// don't want to embed in
implementation('androidx.appcompat:appcompat:1.2.0')
}

2 changes: 2 additions & 0 deletions example/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ includeBuild('../source') {
substitute module('com.github.kezong:fat-aar') using project(':')
}
}

includeBuild('included-build')
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class FatAarPlugin implements Plugin<Project> {
}

if (!match) {
def flavorArtifact = FlavorArtifact.createFlavorArtifact(project, variant, dependency)
def flavorArtifact = FlavorArtifact.createFlavorArtifact(project, configuration, variant, dependency)
if (flavorArtifact != null) {
artifactList.add(flavorArtifact)
}
Expand Down
65 changes: 51 additions & 14 deletions source/src/main/groovy/com/kezong/fataar/FlavorArtifact.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import com.android.build.gradle.api.LibraryVariant
import com.android.builder.model.ProductFlavor
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.ResolvedArtifact
import org.gradle.api.artifacts.ResolvedDependency
import org.gradle.api.artifacts.component.ComponentArtifactIdentifier
import org.gradle.api.artifacts.component.ComponentIdentifier
import org.gradle.api.artifacts.component.ProjectComponentIdentifier
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.initialization.IncludedBuild
import org.gradle.api.internal.artifacts.DefaultModuleVersionIdentifier
import org.gradle.api.internal.tasks.TaskDependencyContainer
import org.gradle.api.internal.tasks.TaskDependencyResolveContext
Expand All @@ -32,22 +37,54 @@ class FlavorArtifact {

private static final String CLASS_DefaultResolvedArtifact = "org.gradle.api.internal.artifacts.DefaultResolvedArtifact"

static ResolvedArtifact createFlavorArtifact(Project project, LibraryVariant variant, ResolvedDependency unResolvedArtifact) {
static ResolvedArtifact createFlavorArtifact(Project project, Configuration configuration, LibraryVariant variant, ResolvedDependency unResolvedArtifact) {
Project artifactProject = getArtifactProject(project, unResolvedArtifact)
TaskProvider bundleProvider = null;
try {
bundleProvider = getBundleTask(artifactProject, variant)
} catch (Exception ignore) {
FatUtils.logError("[$variant.name]Can not resolve :$unResolvedArtifact.moduleName")
return null
}
Task bundleTask = null

if (artifactProject != null) {
TaskProvider bundleProvider
try {
bundleProvider = getBundleTask(artifactProject, variant)
} catch (Exception ignore) {
FatUtils.logError("[$variant.name]Can not resolve :$unResolvedArtifact.moduleName")
return null
}

if (bundleProvider == null) {
return null
}

bundleTask = bundleProvider.get()
} else { // try included builds
ResolvedDependencyResult resolvedResult = configuration.incoming.resolutionResult.allDependencies.find { result ->
if (result instanceof ResolvedDependencyResult && result.selected.selectionReason.compositeSubstitution) {
return result.requested.group == unResolvedArtifact.moduleGroup
&& result.requested.module == unResolvedArtifact.moduleName
}
return false
}

if (resolvedResult != null) {
try {
ResolvedComponentResult selected = resolvedResult.selected
ProjectComponentIdentifier identifier = (ProjectComponentIdentifier) selected.id
IncludedBuild build = project.gradle.includedBuild(identifier.build.name)

String variantName = variant.buildType.name.capitalize()

if (bundleProvider == null) {
return null
bundleTask = VersionAdapter.getIncludedBuildBundleTask(build, identifier.projectName, variantName)
} catch (Exception ignore) {
FatUtils.logError("[$variant.name]Can not resolve :$unResolvedArtifact")
return null
}
} else {
FatUtils.logError("[$variant.name]Can not resolve :$unResolvedArtifact")
return null
}
}

ModuleVersionIdentifier identifier = createModuleVersionIdentifier(unResolvedArtifact)
File artifactFile = createArtifactFile(artifactProject, bundleProvider.get())
File artifactFile = createArtifactFile(project, bundleTask)
DefaultIvyArtifactName artifactName = createArtifactName(artifactFile)
Factory<File> fileFactory = new Factory<File>() {
@Override
Expand All @@ -60,11 +97,11 @@ class FlavorArtifact {
TaskDependencyContainer taskDependencyContainer = new TaskDependencyContainer() {
@Override
void visitDependencies(TaskDependencyResolveContext taskDependencyResolveContext) {
taskDependencyResolveContext.add(createTaskDependency(bundleProvider.get()))
taskDependencyResolveContext.add(createTaskDependency(bundleTask))
}
}
if (FatUtils.compareVersion(project.gradle.gradleVersion, "6.8.0") >= 0) {
Object fileCalculatedValue = Class.forName(CLASS_CalculatedValueContainer).newInstance(new DisplayName(){
Object fileCalculatedValue = Class.forName(CLASS_CalculatedValueContainer).newInstance(new DisplayName() {
@Override
String getCapitalizedDisplayName() {
return artifactFile.name
Expand All @@ -88,7 +125,7 @@ class FlavorArtifact {
.newInstance(identifier, artifactName, artifactIdentifier, taskDependencyContainer, fileFactory)
}
} else {
TaskDependency taskDependency = createTaskDependency(bundleProvider.get())
TaskDependency taskDependency = createTaskDependency(bundleTask)
return Class.forName(CLASS_DefaultResolvedArtifact)
.newInstance(identifier, artifactName, artifactIdentifier, taskDependency, fileFactory)
}
Expand Down
14 changes: 14 additions & 0 deletions source/src/main/groovy/com/kezong/fataar/VersionAdapter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.UnknownTaskException
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.initialization.IncludedBuild
import org.gradle.api.tasks.TaskProvider

import java.lang.reflect.Field
Expand Down Expand Up @@ -136,6 +137,19 @@ class VersionAdapter {
return bundleTask
}

static Task getIncludedBuildBundleTask(IncludedBuild build, String projectName, String variantName)
throws UnknownTaskException {
def taskPath = ":" + projectName + ":bundle" + variantName
Task bundleTask
try {
bundleTask = build.task(taskPath).resolveTask()
} catch (UnknownTaskException ignored) {
taskPath += "Aar"
bundleTask = build.task(taskPath).resolveTask()
}
return bundleTask
}

static String getAGPVersion() {
// AGP 3.6+
try {
Expand Down