peteratt / MultiFlavoredAndroidLibs

6 stars 2 forks source link

Does not work well with library flavors resources #1

Open ihitang opened 8 years ago

ihitang commented 8 years ago

Hi mate,

I've created dev, prod folders, added new string resources with lib_name="prod" and "dev" accordingly. Changed activity's layout to how me lib_name.

And get en error: Error:(8, 29) No resource found that matches the given name (at 'text' with value '@string/lib_name').

Here's a git patch:

Index: app/src/main/res/layout/activity_main.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/src/main/res/layout/activity_main.xml   (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ app/src/main/res/layout/activity_main.xml   (revision )
@@ -5,7 +5,7 @@
     android:paddingTop="@dimen/activity_vertical_margin"
     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

-    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
+    <TextView android:text="@string/lib_name" android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

 </RelativeLayout>
Index: mylib/src/main/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/main/res/values/strings.xml   (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ mylib/src/main/res/values/strings.xml   (revision )
@@ -1,3 +1,4 @@
 <resources>
     <string name="app_name">My Lib</string>
+    <string name="lib_name">default Lib</string>
 </resources>
Index: mylib/src/prod/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/prod/res/values/strings.xml   (revision )
+++ mylib/src/prod/res/values/strings.xml   (revision )
@@ -0,0 +1,4 @@
+<resources>
+    <string name="app_name">My Lib</string>
+    <string name="lib_name">prod Lib</string>
+</resources>
Index: mylib/src/dev/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/dev/res/values/strings.xml    (revision )
+++ mylib/src/dev/res/values/strings.xml    (revision )
@@ -0,0 +1,4 @@
+<resources>
+    <string name="app_name">My Lib</string>
+    <string name="lib_name">dev Lib</string>
+</resources>
peteratt commented 8 years ago

Thanks will take a look!

ihitang commented 8 years ago

Ok, I've fixed

It's a little bit tricky, but works well

Index: app/src/main/res/layout/activity_main.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/src/main/res/layout/activity_main.xml   (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ app/src/main/res/layout/activity_main.xml   (revision )
@@ -5,7 +5,7 @@
     android:paddingTop="@dimen/activity_vertical_margin"
     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

-    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
+    <TextView android:text="@string/lib_name" android:layout_width="wrap_content"
         android:layout_height="wrap_content" />

 </RelativeLayout>
Index: app/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/build.gradle    (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ app/build.gradle    (revision )
@@ -17,22 +17,31 @@
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
     }
+    productFlavors {
+        dev {
+            // Your dev tweaks here
+            minSdkVersion 23
-}
+        }
-
-configurations {
-    devDebugCompile
-    prodDebugCompile
-    devReleaseCompile
-    prodReleaseCompile
+        prod {
+            // Your prod tweaks here
+            minSdkVersion 16
-}
+        }
+    }
+}

 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
     compile 'com.android.support:appcompat-v7:23.0.1'

-    devDebugCompile project(path: ':mylib', configuration: 'devDebug')
-    prodDebugCompile project(path: ':mylib', configuration: 'prodDebug')
+    Map<String, Dependency> customDeps = new HashMap<String, Dependency>()

-    devReleaseCompile project(path: ':mylib', configuration: 'devRelease')
-    prodReleaseCompile project(path: ':mylib', configuration: 'prodRelease')
+    customDeps.put("devDebugCompile", dependencies.project(path: ":mylib", configuration: "devDebug"))
+    customDeps.put("prodDebugCompile", dependencies.project(path: ":mylib", configuration: "prodDebug"))
+
+    configurations.all() {config ->
+        Dependency d = customDeps.get(config.name)
+        if(d != null) {
+            config.dependencies.add(d)
+        }
+    }
 }
Index: mylib/src/main/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/main/res/values/strings.xml   (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ mylib/src/main/res/values/strings.xml   (revision )
@@ -1,3 +1,4 @@
 <resources>
     <string name="app_name">My Lib</string>
+    <string name="lib_name">default Lib</string>
 </resources>
Index: mylib/src/prod/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/prod/res/values/strings.xml   (revision )
+++ mylib/src/prod/res/values/strings.xml   (revision )
@@ -0,0 +1,4 @@
+<resources>
+    <string name="app_name">My Lib</string>
+    <string name="lib_name">prod Lib</string>
+</resources>
Index: mylib/src/dev/res/values/strings.xml
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/src/dev/res/values/strings.xml    (revision )
+++ mylib/src/dev/res/values/strings.xml    (revision )
@@ -0,0 +1,4 @@
+<resources>
+    <string name="app_name">My Lib</string>
+    <string name="lib_name">dev Lib</string>
+</resources>
Index: mylib/build.gradle
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- mylib/build.gradle  (revision 802844b15ffee6179ad687827661993a05a53b33)
+++ mylib/build.gradle  (revision )
@@ -4,6 +4,8 @@
     compileSdkVersion 23
     buildToolsVersion "23.0.1"

+    publishNonDefault true
+
     defaultConfig {
         minSdkVersion 16
         targetSdkVersion 23
peteratt commented 8 years ago

Thanks! Whenever I have some time I'll consolidate your changes.

zeinabmohamed commented 7 years ago

it doesn't work with me after apply this solution