Skip to content

Commit

Permalink
Merge pull request #118 from gradle/tt/support-data-binding-in-android
Browse files Browse the repository at this point in the history
Add viewBinding and dataBinding support for Android Applications
  • Loading branch information
tresat authored Sep 5, 2024
2 parents 56e1a7a + dd6b97b commit 5806a17
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.gradle.api.experimental.android

import org.gradle.test.fixtures.AbstractSpecification

class AndroidApplicationSpec extends AbstractSpecification {
def 'can create an android application using viewBinding that generates an activity binding class'() {
given:
buildFile << """
androidApplication {
jdkVersion = 17
compileSdk = 34
namespace = "org.example.android.application"
viewBinding {
enabled = true
}
dependencies {
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.10.0")
}
}
"""

file("src/main/res/layout/activity_main.xml").text = """<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
</androidx.coordinatorlayout.widget.CoordinatorLayout>
"""

file("src/main/java/org/example/android/application/MainActivity.java").text = """
package org.example.android.application;
import androidx.appcompat.app.AppCompatActivity;
import org.example.android.application.databinding.ActivityMainBinding;
class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
}
"""

file("src/main/AndroidManifest.xml").text = """<?xml version="1.0" encoding="utf-8"?>
<manifest/>
"""

file("gradle.properties").text = """
android.useAndroidX=true
"""

expect:
succeeds(":build")
}

def 'can create an android application using dataBinding'() {
given:
buildFile << """
androidApplication {
jdkVersion = 17
compileSdk = 34
namespace = "org.example.android.application"
dataBinding {
enabled = true
}
}
"""

file("src/main/AndroidManifest.xml").text = """<?xml version="1.0" encoding="utf-8"?>
<manifest/>
"""

expect:
succeeds(":build")
}

def setup() {
settingsFile << """
plugins {
id("org.gradle.experimental.android-ecosystem")
}
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}
rootProject.name = "example"
"""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import com.android.build.api.dsl.ApplicationBaseFlavor;
import org.gradle.api.Action;
import org.gradle.api.experimental.android.AndroidSoftware;
import org.gradle.api.experimental.android.extensions.DataBinding;
import org.gradle.api.experimental.android.extensions.DependencyGuard;
import org.gradle.api.experimental.android.extensions.Firebase;
import org.gradle.api.experimental.android.extensions.ViewBinding;
import org.gradle.api.experimental.android.nia.DimensionStrategy;
import org.gradle.api.experimental.android.nia.Flavors;
import org.gradle.api.provider.Property;
Expand Down Expand Up @@ -97,4 +99,20 @@ default void flavors(Action<? super Flavors> action) {
default void missingDimensionStrategy(Action<? super DimensionStrategy> action) {
action.execute(getMissingDimensionStrategy());
}

@Nested
ViewBinding getViewBinding();

@Configuring
default void viewBinding(Action<? super ViewBinding> action) {
action.execute(getViewBinding());
}

@Nested
DataBinding getDataBinding();

@Configuring
default void dataBinding(Action<? super DataBinding> action) {
action.execute(getDataBinding());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public void apply(Project project) {
dslModel.getBuildTypes().getRelease().getApplicationIdSuffix().convention((String) null);

dslModel.getFlavors().getEnabled().convention(false);
dslModel.getViewBinding().getEnabled().convention(false);
dslModel.getDataBinding().getEnabled().convention(false);

// Register an afterEvaluate listener before we apply the Android plugin to ensure we can
// run actions before Android does.
Expand Down Expand Up @@ -70,6 +72,9 @@ private void linkDslModelToPlugin(Project project, AndroidApplication dslModel)
return null;
});

android.getBuildFeatures().setViewBinding(dslModel.getViewBinding().getEnabled().get());
android.getBuildFeatures().setDataBinding(dslModel.getDataBinding().getEnabled().get());

// TODO:DG All this configuration should be moved to the NiA project
if (NiaSupport.isNiaProject(project)) {
NiaSupport.configureNiaApplication(project, dslModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.gradle.api.experimental.android.extensions;

import org.gradle.api.provider.Property;
import org.gradle.declarative.dsl.model.annotations.Restricted;

@Restricted
public interface DataBinding {
@Restricted
Property<Boolean> getEnabled();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.gradle.api.experimental.android.extensions;

import org.gradle.api.provider.Property;
import org.gradle.declarative.dsl.model.annotations.Restricted;

@Restricted
public interface ViewBinding {
@Restricted
Property<Boolean> getEnabled();
}

0 comments on commit 5806a17

Please sign in to comment.