From 648c081b56449913aeeeeb6e8488fba063a3e335 Mon Sep 17 00:00:00 2001 From: Zhi Dou Date: Thu, 22 Jun 2023 19:42:48 +0000 Subject: [PATCH] A simple app uses aconfig flag Add a simple app uses aconfig. This app can demonstrate 1. declare aconfig flag 2. build aconfig flag in building system 3. generate aconfig java flag 4. use aconfig java flag in java code Test: atest AconfigDemo_ContentProvide_Test Bug: 280833426 Change-Id: Ieacbe4b2368a00872254dab1c87dc66c569fc30a --- samples/AconfigDemo/Android.bp | 42 +++++++++++++++++ samples/AconfigDemo/AndroidManifest.xml | 34 ++++++++++++++ .../AconfigDemo/aconfig_demo_flags.aconfig | 7 +++ samples/AconfigDemo/res/layout/main.xml | 32 +++++++++++++ .../demo/activity/AconfigDemoActivity.java | 45 +++++++++++++++++++ .../demo/activity/ContentProvider.java | 39 ++++++++++++++++ samples/AconfigDemo/tests/Android.bp | 15 +++++++ samples/AconfigDemo/tests/AndroidManifest.xml | 32 +++++++++++++ samples/AconfigDemo/tests/build.properties | 1 + .../demo/activity/ContentProviderTests.java | 17 +++++++ 10 files changed, 264 insertions(+) create mode 100644 samples/AconfigDemo/AndroidManifest.xml create mode 100644 samples/AconfigDemo/aconfig_demo_flags.aconfig create mode 100644 samples/AconfigDemo/res/layout/main.xml create mode 100644 samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java create mode 100644 samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java create mode 100644 samples/AconfigDemo/tests/Android.bp create mode 100644 samples/AconfigDemo/tests/AndroidManifest.xml create mode 100644 samples/AconfigDemo/tests/build.properties create mode 100644 samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/activity/ContentProviderTests.java diff --git a/samples/AconfigDemo/Android.bp b/samples/AconfigDemo/Android.bp index 7dbf6b648..8bd0e75d3 100644 --- a/samples/AconfigDemo/Android.bp +++ b/samples/AconfigDemo/Android.bp @@ -1,3 +1,45 @@ package { default_applicable_licenses: ["Android-Apache-2.0"], } + +android_app { + name: "AconfigDemoActivity", + manifest: "AndroidManifest.xml", + srcs: ["src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java"], + certificate: "platform", + sdk_version: "current", + static_libs: [ + "ContentProvider", + ], + optimize: { + enabled: true, + proguard_compatibility: false, + shrink: true, + optimize: false, + obfuscate: false, + shrink_resources: true, + }, +} + +aconfig_declarations { + name: "aconfig_demo_flags", + package: "com.example.android.aconfig.demo.activity", + srcs: ["aconfig_demo_flags.aconfig"], +} + + +java_aconfig_library { + name: "aconfig_demo_flags_lib", + aconfig_declarations: "aconfig_demo_flags", +} + +java_library { + name: "ContentProvider", + srcs: [ + "src/com/example/android/aconfig/demo/activity/ContentProvider.java", + ], + sdk_version: "current", + static_libs: [ + "aconfig_demo_flags_lib", + ], +} diff --git a/samples/AconfigDemo/AndroidManifest.xml b/samples/AconfigDemo/AndroidManifest.xml new file mode 100644 index 000000000..b2320b3f7 --- /dev/null +++ b/samples/AconfigDemo/AndroidManifest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + diff --git a/samples/AconfigDemo/aconfig_demo_flags.aconfig b/samples/AconfigDemo/aconfig_demo_flags.aconfig new file mode 100644 index 000000000..ecf699329 --- /dev/null +++ b/samples/AconfigDemo/aconfig_demo_flags.aconfig @@ -0,0 +1,7 @@ +package: "com.example.android.aconfig.demo.activity" + +flag { + name: "append_content" + namespace: "aconfig_demo_ns" + description: "This flag is buildtime flag" +} diff --git a/samples/AconfigDemo/res/layout/main.xml b/samples/AconfigDemo/res/layout/main.xml new file mode 100644 index 000000000..e54a53bd8 --- /dev/null +++ b/samples/AconfigDemo/res/layout/main.xml @@ -0,0 +1,32 @@ + + + + + + diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java new file mode 100644 index 000000000..523380f8c --- /dev/null +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/AconfigDemoActivity.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.aconfig.demo.activity; + +import android.app.Activity; +import android.os.Bundle; +import android.view.View; +import android.widget.TextView; +import android.view.WindowManager; + + +/** + * A minimal "Hello, World!" application. + */ +public class AconfigDemoActivity extends Activity { + /** + * Called with the activity is first created. + */ + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.main); + TextView simpleTextView = (TextView) findViewById(R.id.simpleTextView); + simpleTextView.setText("Show Flags: \n\n"); + + ContentProvider cp = new ContentProvider(); + simpleTextView.append(cp.getContent()); + } +} + diff --git a/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java b/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java new file mode 100644 index 000000000..f6d2599a7 --- /dev/null +++ b/samples/AconfigDemo/src/com/example/android/aconfig/demo/activity/ContentProvider.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2007 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.android.aconfig.demo.activity; + +/** + * A minimal "Hello, World!" application. + */ +public class ContentProvider { + + public ContentProvider() {}; + + public String getContent() { + + StringBuilder sBuffer = new StringBuilder(); + + if (Flags.appendContent()) { + sBuffer.append("The flag is ON!!\n"); + } else { + sBuffer.append("The flag is OFF!!\n"); + } + + return sBuffer.toString(); + } +} + diff --git a/samples/AconfigDemo/tests/Android.bp b/samples/AconfigDemo/tests/Android.bp new file mode 100644 index 000000000..12efabde1 --- /dev/null +++ b/samples/AconfigDemo/tests/Android.bp @@ -0,0 +1,15 @@ +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +android_test { + name: "AconfigDemo_ContentProvide_Test", + srcs: ["src/com/example/android/aconfig/demo/activity/ContentProviderTests.java"], + certificate: "platform", + static_libs: [ + "ContentProvider", + "junit", + "androidx.test.runner", + ], + manifest: "AndroidManifest.xml", +} \ No newline at end of file diff --git a/samples/AconfigDemo/tests/AndroidManifest.xml b/samples/AconfigDemo/tests/AndroidManifest.xml new file mode 100644 index 000000000..fcc480e90 --- /dev/null +++ b/samples/AconfigDemo/tests/AndroidManifest.xml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + diff --git a/samples/AconfigDemo/tests/build.properties b/samples/AconfigDemo/tests/build.properties new file mode 100644 index 000000000..e0c39def1 --- /dev/null +++ b/samples/AconfigDemo/tests/build.properties @@ -0,0 +1 @@ +tested.project.dir=.. diff --git a/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/activity/ContentProviderTests.java b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/activity/ContentProviderTests.java new file mode 100644 index 000000000..8c9d6aac3 --- /dev/null +++ b/samples/AconfigDemo/tests/src/com/example/android/aconfig/demo/activity/ContentProviderTests.java @@ -0,0 +1,17 @@ +package com.example.android.aconfig.demo.activity; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class ContentProviderTests { + + @Test + public void testFlag() { + assertFalse(Flags.appendContent()); + } +} \ No newline at end of file