forked from rdamir86/fat-aar-android
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Gradle8 support #3
Merged
Viacheslav01
merged 6 commits into
salute-developers:master
from
bugDim88:gradle8_support
Oct 27, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
610e989
fix for build task execute success
DvaBugrov 6777cda
add support for AGP 8.0
aasitnikov 10cda2e
fix weird issue with properties
aasitnikov aff99d5
fix when no embeds are defined
aasitnikov 51f4036
make transformAsm task depend on mergeClasses
aasitnikov 3c282c1
backward compatibility with gradle 7
DvaBugrov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
source/src/main/java/com/kezong/fataar/FatAarPluginHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package com.kezong.fataar; | ||
|
||
import com.android.build.api.instrumentation.AsmClassVisitorFactory; | ||
import com.android.build.api.instrumentation.ClassContext; | ||
import com.android.build.api.instrumentation.ClassData; | ||
import com.android.build.api.instrumentation.InstrumentationParameters; | ||
import com.android.build.api.instrumentation.InstrumentationScope; | ||
import com.android.build.api.variant.AndroidComponentsExtension; | ||
import com.android.build.api.variant.Variant; | ||
|
||
import org.gradle.api.Project; | ||
import org.gradle.api.provider.ListProperty; | ||
import org.gradle.api.provider.MapProperty; | ||
import org.gradle.api.provider.Property; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.Optional; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.commons.ClassRemapper; | ||
import org.objectweb.asm.commons.Remapper; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import kotlin.Unit; | ||
import kotlin.text.StringsKt; | ||
|
||
import static com.android.build.api.instrumentation.FramesComputationMode.COPY_FRAMES; | ||
|
||
public class FatAarPluginHelper { | ||
|
||
public static void registerAsmTransformation( | ||
Project project, | ||
MapProperty<String, List<AndroidArchiveLibrary>> variantPackagesProperty) { | ||
AndroidComponentsExtension<?, ?, Variant> components = | ||
project.getExtensions().getByType(AndroidComponentsExtension.class); | ||
components.onVariants(components.selector().all(), variant -> { | ||
variant.getInstrumentation().transformClassesWith( | ||
RClassAsmTransformerFactory.class, | ||
InstrumentationScope.PROJECT, | ||
params -> { | ||
params.getNamespace().set(variant.getNamespace()); | ||
params.getLibraryNamespaces().set(variantPackagesProperty.getting(variant.getName()) | ||
.map(list -> list.stream().map(it -> it.getPackageName()).collect(Collectors.toList())) | ||
); | ||
return Unit.INSTANCE; | ||
}); | ||
variant.getInstrumentation().setAsmFramesComputationMode(COPY_FRAMES); | ||
}); | ||
} | ||
|
||
public abstract static class RClassAsmTransformerFactory implements AsmClassVisitorFactory<RClassAsmTransformerFactory.Params> { | ||
|
||
public interface Params extends InstrumentationParameters { | ||
|
||
@Input | ||
Property<String> getNamespace(); | ||
|
||
@Input | ||
@Optional | ||
ListProperty<String> getLibraryNamespaces(); | ||
} | ||
|
||
@Override | ||
public ClassVisitor createClassVisitor(ClassContext classContext, ClassVisitor classVisitor) { | ||
Params params = getParameters().get(); | ||
String namespace = params.getNamespace().get(); | ||
List<String> libraryNamespaces = params.getLibraryNamespaces().orElse(Collections.emptyList()).get(); | ||
|
||
if (libraryNamespaces.isEmpty()) { | ||
return classVisitor; | ||
} | ||
|
||
String targetRClass = namespace.replace(".", "/") + "/R"; | ||
String targetRSubclass = namespace.replace(".", "/") + "/R$"; | ||
|
||
Set<String> libraryRClasses = libraryNamespaces.stream() | ||
.map(it -> it.replace(".", "/") + "/R") | ||
.collect(Collectors.toSet()); | ||
List<String> libraryRSubclasses = libraryNamespaces.stream() | ||
.map(it -> it.replace(".", "/") + "/R$") | ||
.collect(Collectors.toList()); | ||
|
||
Remapper remapper = new Remapper() { | ||
@Override | ||
public String map(String internalName) { | ||
if (internalName == null) { | ||
return null; | ||
} | ||
if (libraryRClasses.contains(internalName)) { | ||
return targetRClass; | ||
} | ||
for (String libraryRSubclass : libraryRSubclasses) { | ||
if (internalName.startsWith(libraryRSubclass)) { | ||
return StringsKt.replaceFirst(internalName, libraryRSubclass, targetRSubclass, false); | ||
} | ||
} | ||
return super.map(internalName); | ||
} | ||
}; | ||
return new ClassRemapper(classVisitor, remapper); | ||
} | ||
|
||
@Override | ||
public boolean isInstrumentable(ClassData classData) { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тут точно надо комменты влить?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
думаю да, будет как дока. Я это закоментил чтобы локально билдить.