I'm trying to migrate my company's project from Maven to Gradle, so far I've been able to convert all POMs to the corresponding build.gradle files, but I ran across an issue in a sub module when building.
- Core-UI
---- Utils
---- FieldPanels
The sub module FieldPanel uses colors and other resources defined in Utils. I tried adding the Utils project as a dependency, but it did not work, what am I missing?
/home/development/AndroidStudioProjects/com.project/core-ui/common.android.fieldpanels/build/bundles/debug/res/layout/base_summary_step_layout.xml:2: error: Error: No resource found that matches the given name (at 'background' with value '@color/wizard_summary_bg').
Utils gradle.build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.2'
}
}
apply plugin: 'android-library'
android {
sourceSets {
main {
manifest {
srcFile 'AndroidManifest.xml'
}
res {
srcDirs = [
'res'
]
}
}
}
compileSdkVersion 15
buildToolsVersion '17.0'
dependencies {
compile group: 'com.company.ipos.android', name: 'commons.pos.micropos.api', version: '0.0.35-SNAPSHOT'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.5'
}
}
repositories {
maven {
credentials {
username mavenUser
password mavenPassword
}
url repoUrl
}
maven {
credentials {
username mavenUser
password mavenPassword
}
url libsReleasesUrl
}
maven {
credentials {
username mavenUser
password mavenPassword
}
url libsSnapshotsUrl
}
}
fieldPanels gradle.build
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.2'
}
}
apply plugin: 'android-library'
repositories {
maven {
credentials {
username mavenUser
password mavenPassword
}
url repoUrl
}
maven {
credentials {
username mavenUser
password mavenPassword
}
url libsReleasesUrl
}
maven {
credentials {
username mavenUser
password mavenPassword
}
url libsSnapshotsUrl
}
}
android {
dependencies {
compile group: 'com.company.ipos.android', name: 'commons.pos.micropos.api', version: '0.0.35-SNAPSHOT'
//compile group: 'com.company.ipos.android', name: 'common.android.utils', version: '0.0.35-SNAPSHOT'
compile project(":common.android.utils")
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.5'
}
sourceSets {
main {
manifest {
srcFile 'AndroidManifest.xml'
}
res {
srcDirs = [
'res',
]
}
}
}
compileSdkVersion 15
buildToolsVersion '17.0'
}
The subproject producer defines a task named buildInfo that generates a properties file containing build information e.g. the project version. You can then map the task provider to its output file and Gradle will automatically establish a task dependency. Example 2.
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well.
Taking your example of "2 library projects LA and LB and LB depends on LA" here are a couple of working examples.
Let's say LA has the following strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="la_resource">la resource</string>
</resources>
Let's say LB has the following strings.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="lb_resource">lb resource</string>
</resources>
Let's say we want to define a custom view named TestView in LB. TestView will reference la_resource from LA and lb_resource from LB. Here is the source for TestView
package com.example.myapplication3.lb;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
public class TestView extends TextView {
public TestView(Context context) {
super(context);
setText(context.getString(R.string.la_resource) + " " + context.getString(R.string.lb_resource));
}
public TestView(Context context, AttributeSet attrs) {
super(context, attrs);
setText(context.getString(R.string.la_resource) + " " + context.getString(R.string.lb_resource));
}
public TestView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setText(context.getString(R.string.la_resource) + " " + context.getString(R.string.lb_resource));
}
}
Let's say we want to define another custom view named TestView2 in LB. TestView2 will inflate an XML layout. The XML layout will reference la_resource from LA and lb_resource from LB. Here is the source for TestView2
package com.example.myapplication3.lb;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.TextView;
import org.w3c.dom.Text;
public class TestView2 extends FrameLayout {
public TestView2(Context context) {
super(context);
LayoutInflater.from(context).inflate(R.layout.test_view_2, this);
}
public TestView2(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.test_view_2, this);
}
public TestView2(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
LayoutInflater.from(context).inflate(R.layout.test_view_2, this);
}
}
And here is the source for test_view_2.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/la_resource"
android:padding="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lb_resource" />
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With