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

Allow library to be either use androidx or the support library. #26

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,31 @@ A Java annotation processor that builds a ResourceProvider class which contains
generateDrawableProvider = false,
generateStringProvider = false)
```


Using with Support Library
=========================
The default mode of this library is to generate code compatible with the Jetpack/AndroidX libraries. In order to use this with
the legacy support library, you must add the following argument to your annotation processor (example is for kapt)

```groovy
kapt {
arguments {
arg("resourceProvider.useSupportLibrary", "true")
}
}
```

Using with Jetpack
=========================
The default mode of this library is to generate code compatible with the Jetpack/AndroidX libraries. However, in order to be
backward compatible for the legacy version of the support libraries, it includes a string reference to the support library packages.
This will be flagged by the jetifier feature and will fail.

As such, you should add the android.jetifier.blacklist flag to your main gradle.properties file to exclude this library:

```text
android.useAndroidX=true
android.enableJetifier=true
android.jetifier.blacklist=resourceprovider-compiler-.*\\.jar
```
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ final class RpCodeGenerator {
private final AnnotationSpec suppressLint = AnnotationSpec.builder(ClassName.get("android.annotation", "SuppressLint"))
.addMember("value", "$L", "{\"StringFormatInvalid\", \"StringFormatMatches\"}")
.build();
private TypeName contextCompatClassName = get("androidx.core.content", "ContextCompat");

RpCodeGenerator(String packageName, List<String> rClassStringVars, List<String> rClassPluralVars, List<String> rClassDrawableVars,
List<String> rClassDimenVars, List<String> rClassIntegerVars, List<String> rClassColorVars,
private TypeName contextCompatClassName;

RpCodeGenerator(String packageName,
boolean useJetpack,
List<String> rClassStringVars,
List<String> rClassPluralVars,
List<String> rClassDrawableVars,
List<String> rClassDimenVars,
List<String> rClassIntegerVars,
List<String> rClassColorVars,
List<String> rClassIdVars) {
this.packageName = packageName;
this.rClassStringVars = rClassStringVars;
Expand All @@ -48,6 +54,10 @@ final class RpCodeGenerator {
this.rClassIntegerVars = rClassIntegerVars;
this.rClassColorVars = rClassColorVars;
this.rClassIdVars = rClassIdVars;

this.contextCompatClassName = useJetpack ?
get("androidx.core.content", "ContextCompat") :
get("android.support.v4.content", "ContextCompat");
}

TypeSpec generateResourceProviderClass(boolean generateIdProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public SourceVersion getSupportedSourceVersion() {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
String useSupportLibraryString = processingEnv.getOptions().get("resourceProvider.useSupportLibrary");
boolean useJetpack = !"true".equals(useSupportLibraryString);

for (Element annotatedElement : roundEnv.getElementsAnnotatedWith(RpApplication.class)) {

// annotation is only allowed on classes, so we can safely cast here
Expand Down Expand Up @@ -153,7 +156,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
});

generateCode(annotatedClass, rStringVars, rPluralVars, rDrawableVars, rDimenVars, rIntegerVars,
generateCode(annotatedClass, useJetpack, rStringVars, rPluralVars, rDrawableVars, rDimenVars, rIntegerVars,
rColorVars, rIdVars);
} catch (UnnamedPackageException | IOException e) {
messager.error(annotatedElement, "Couldn't generate class for %s: %s", annotatedClass,
Expand All @@ -177,12 +180,18 @@ private boolean isValidClass(TypeElement annotatedClass) {
return processingEnv.getTypeUtils().isAssignable(annotatedClass.asType(), applicationTypeElement.asType());
}

private void generateCode(TypeElement annotatedClass, List<String> rStringVars, List<String> rPluralVars,
List<String> rDrawableVars, List<String> rDimenVars, List<String> rIntegerVars,
List<String> rColorVars, List<String> rIdVars)
private void generateCode(TypeElement annotatedClass,
boolean useJetpack,
List<String> rStringVars,
List<String> rPluralVars,
List<String> rDrawableVars,
List<String> rDimenVars,
List<String> rIntegerVars,
List<String> rColorVars,
List<String> rIdVars)
throws UnnamedPackageException, IOException {
String packageName = getPackageName(processingEnv.getElementUtils(), annotatedClass);
RpCodeGenerator codeGenerator = new RpCodeGenerator(packageName, rStringVars, rPluralVars, rDrawableVars, rDimenVars,
RpCodeGenerator codeGenerator = new RpCodeGenerator(packageName, useJetpack, rStringVars, rPluralVars, rDrawableVars, rDimenVars,
rIntegerVars, rColorVars, rIdVars);

boolean generateIdProvider = rIdVars.size() > 0;
Expand Down