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

Support for prefixing resources #351

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ embed('com.facebook.fresco:fresco:1.11.0') {

**More usage see [example](./example).**

### Resource prefixing (experimental)
You can enable prefixing of library resources. This can be useful if your fat-aar has a lot of resources that cannot be renamed at the source level. The plugin will add the specified prefix to all internal library resources during build time.

```groovy
fataar {

/**
* Add prefix for all library resources. To prevent conflicts with a library clients.
* @since 1.3.7
*/
resourcePrefix = "lib_main_"
}
```

## About AAR File

AAR is a file format for android library.
Expand Down
Binary file modified example/app/libs/fat-aar-final.aar
Binary file not shown.
18 changes: 16 additions & 2 deletions example/app/src/main/java/com/fataar/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
Expand All @@ -30,13 +31,16 @@
import com.kezong.demo.libaar.AarFlavor;
import com.kezong.demo.libaar.AarLibClass;
import com.kezong.demo.libaar.KotlinTest2;
import com.kezong.demo.libaar.LibCountries;
import com.kezong.demo.libaar.TestActivity;
import com.kezong.demo.libaar2.Aar2LibClass;
import com.kezong.demo.libaarlocal.AarLocalLibClass;
import com.kezong.demo.libaarlocal2.AarLocal2LibClass;
import com.kezong.demo.lib.R.*;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

/**
* @author kezong on 2020/12/11.
Expand Down Expand Up @@ -65,6 +69,11 @@ protected void onCreate(Bundle savedInstanceState) {
testAssetsMerge();
testRemoteAar();
testFlavor();
testEnum();
}

private void testEnum() {
addTestView("enums", "lib flag ee", LibCountries.ESTONIA.getFlagRes() == R.drawable.lib_main_flag_ee);
}

private void testFlavor() {
Expand Down Expand Up @@ -162,12 +171,17 @@ public void testKotlinTopLevel2() {
}

public void testResourceMerge() {

for (Field field : R.string.class.getFields()) {
Log.d("R class name:", field.getName());
}

String text = new AarLibClass().getLibName(this);
addTestView("resource", text, TextUtils.equals(text, "lib-aar"));
addTestView("resource", text, TextUtils.equals(text, "lib-aar eng"));
}

public void testResourceMerge2() {
String text = this.getResources().getString(R.string.app_name_aar2);
String text = this.getResources().getString(R.string.lib_main_app_name_aar2);
addTestView("resource2", text, TextUtils.equals(text, "lib-aar2"));
}

Expand Down
4 changes: 2 additions & 2 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ buildscript {
}
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"
classpath 'com.github.kezong:fat-aar:1.3.7'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.30"
}
}

Expand Down
4 changes: 3 additions & 1 deletion example/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
android.useAndroidX=true
android.enableJetifier=true
android.enableJetifier=true
org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8

2 changes: 1 addition & 1 deletion example/lib-aar/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<application>
<activity
android:name="com.kezong.demo.libaar.TestActivity"
/>
android:theme="@style/LibAarTheme" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public class AarLibClass {
public String getLibName(Context ctx) {
return ctx.getResources().getString(R.string.app_name_aar);
}

public String getTestString(Context ctx) {
return ctx.getResources().getString(R.string.lib_aar_test_string1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.kezong.demo.libaar

import android.content.Context
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import java.io.Serializable

enum class LibCountries(
val code: String,
@StringRes val nameRes: Int,
@DrawableRes val flagRes: Int,
vararg phonePrefix: String,
) : Serializable {

ESTONIA("ee", R.string.country_ee, R.drawable.flag_ee, "+397"),
USA("us", R.string.country_us, R.drawable.flag_us, "+1");

fun getName(context: Context): String = context.getString(nameRes)

companion object {

@JvmStatic
fun findByCode(countryCode: String?) = values().find { it.code.equals(countryCode, true) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import androidx.databinding.DataBindingUtil;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kezong.demo.libaar.databinding.DatabindingBinding;

Expand All @@ -13,11 +17,30 @@ public class TestActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.LibAarTheme_Main);
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.databinding);
User user = new User();
user.setName("Hello World");
user.setSex("[success][dataBinding] male");
binding.setUser(user);

for (LibCountries country : LibCountries.values()) {
binding.container.addView(getCountryView(country));
}
}

private View getCountryView(LibCountries country) {
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
textView.setText(country.getName(this));
ImageView imageView = new ImageView(this);
imageView.setImageResource(country.getFlagRes());

layout.addView(textView);
layout.addView(imageView);

return layout;
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions example/lib-aar/src/main/res/drawable/lib_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/text_red" />
<corners android:radius="@dimen/corner_radius" />
</shape>
14 changes: 12 additions & 2 deletions example/lib-aar/src/main/res/layout/databinding.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="com.kezong.demo.libaar.User"/>
</data>
<LinearLayout
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/corner_paddings"
android:background="@drawable/lib_background"
android:textColor="@android:color/black"
android:text="@{user.name}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_red"
android:text="@{user.sex}"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_white"
android:background="@android:color/black"
android:text="@string/lib_aar_string_ref" />
</LinearLayout>
</layout>
14 changes: 9 additions & 5 deletions example/lib-aar/src/main/res/layout/test_layout.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2015 Baidu, Inc. All Rights Reserved.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lib_aar_test_string1"
android:background="@drawable/lib_background"
android:padding="@dimen/corner_paddings"
android:textColor="@color/text_white"/>
</LinearLayout>
6 changes: 6 additions & 0 deletions example/lib-aar/src/main/res/values-en/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name_aar">lib-aar eng</string>
<string name="lib_aar_test_string1">lib aar test string value eng</string>
<string name="lib_aar_string_ref">@string/lib_aar_test_string1</string>
</resources>
5 changes: 5 additions & 0 deletions example/lib-aar/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_white">@android:color/white</color>
<color name="text_red">#ff0000</color>
</resources>
5 changes: 5 additions & 0 deletions example/lib-aar/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="corner_radius">10dp</dimen>
<dimen name="corner_paddings">@dimen/corner_radius</dimen>
</resources>
4 changes: 4 additions & 0 deletions example/lib-aar/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<resources>
<string name="app_name_aar">lib-aar</string>
<string name="lib_aar_test_string1">lib aar test string value</string>
<string name="lib_aar_string_ref">@string/lib_aar_test_string1</string>
<string name="country_ee" translatable="false">Estonia</string>
<string name="country_us" translatable="false">USA</string>
</resources>
9 changes: 9 additions & 0 deletions example/lib-aar/src/main/res/values/style.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LibAarTheme" parent="android:Theme.Black">
<item name="android:background">@android:color/white</item>
</style>
<style name="LibAarTheme.Main" parent="LibAarTheme">
<item name="android:background">#f0f0f0</item>
</style>
</resources>
Binary file added example/lib-aar2/src/main/res/drawable/im_cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions example/lib-aar2/src/main/res/drawable/simple_drawable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>
7 changes: 7 additions & 0 deletions example/lib-aar2/src/main/res/layout/simple_layout.xml
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:background="@color/my_test_color_ref"
android:orientation="vertical"
android:theme="@style/OtherTheme" />
5 changes: 5 additions & 0 deletions example/lib-aar2/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="my_test_color">@android:color/black</color>
<color name="my_test_color_ref">@color/my_test_color</color>
</resources>
1 change: 1 addition & 0 deletions example/lib-aar2/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name_aar2">lib-aar2</string>
<string name="lib_string_1">lib string 1</string>
</resources>
9 changes: 9 additions & 0 deletions example/lib-aar2/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="android:Theme.Black">
<item name="android:background">@color/my_test_color</item>
</style>
<style name="OtherTheme" parent="MyTheme">
<item name="android:background">@color/my_test_color_ref</item>
</style>
</resources>
19 changes: 17 additions & 2 deletions example/lib-main/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ repositories {
}

android {
compileSdkVersion 29
compileSdkVersion 31

defaultConfig {
minSdkVersion 16
targetSdkVersion 27
targetSdkVersion 31
versionCode 1
versionName "1.0.0"
}
Expand Down Expand Up @@ -44,6 +44,15 @@ android {
exclude 'lib/armeabi/*.so'
exclude 'lib/arm64-v8a/*.so'
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11
}
}

afterEvaluate {
Expand Down Expand Up @@ -71,6 +80,12 @@ fataar {
* @since 1.3.0
*/
transitive = true

/**
* Add prefix for all library resources. To prevent conflicts with a library clients.
* @since 1.3.7
*/
resourcePrefix = "lib_main_"
}

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion source/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PUBLISH_GROUP_ID=com.github.kezong
PUBLISH_ARTIFACT_ID=fat-aar
PUBLISH_VERSION=1.3.6
PUBLISH_VERSION=1.3.7
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ class FatAarExtension {
* @since 1.3.0
*/
boolean transitive = false

/**
* Add prefix for all library resources. To prevent conflicts with a library clients.
* @since 1.3.7
*/
String resourcePrefix = ""
}
Loading