diff --git a/android-sdk/build.gradle b/android-sdk/build.gradle index f0d2955d..0dbdbe37 100644 --- a/android-sdk/build.gradle +++ b/android-sdk/build.gradle @@ -29,6 +29,7 @@ android { versionName version_name testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' buildConfigField "String", "CLIENT_VERSION", "\"$version_name\"" + // these rules will be merged to app's proguard rules consumerProguardFiles '../proguard-rules.txt' } diff --git a/android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerEventHandlerTest.java b/android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerEventHandlerTest.java new file mode 100644 index 00000000..9bfae9c9 --- /dev/null +++ b/android-sdk/src/androidTest/java/com/optimizely/ab/android/sdk/OptimizelyManagerEventHandlerTest.java @@ -0,0 +1,68 @@ +/**************************************************************************** + * Copyright 2022, Optimizely, Inc. and contributors * + * * + * 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.optimizely.ab.android.sdk; + +import static junit.framework.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import android.content.Context; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; +import com.optimizely.ab.event.EventHandler; +import com.optimizely.ab.event.LogEvent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import java.util.concurrent.TimeUnit; + +@RunWith(AndroidJUnit4.class) +public class OptimizelyManagerEventHandlerTest { + + private String minDatafileWithEvent = "{\n" + + "experiments: [ ],\n" + + "version: \"2\",\n" + + "audiences: [ ],\n" + + "groups: [ ],\n" + + "attributes: [ ],\n" + + "projectId: \"123\",\n" + + "accountId: \"6365361536\",\n" + + "events: [{\"experimentIds\": [\"8509139139\"], \"id\": \"8505434668\", \"key\": \"test_event\"}],\n" + + "revision: \"1\"\n" + + "}"; + + @Test + public void eventClientNameAndVersion() throws Exception { + EventHandler mockEventHandler = mock(EventHandler.class); + + Context context = InstrumentationRegistry.getInstrumentation().getTargetContext(); + OptimizelyManager optimizelyManager = OptimizelyManager.builder() + .withSDKKey("any-sdk-key") + .withEventDispatchInterval(0, TimeUnit.SECONDS) + .withEventHandler(mockEventHandler) + .build(context); + + OptimizelyClient optimizelyClient = optimizelyManager.initialize(context, minDatafileWithEvent); + optimizelyClient.track("test_event", "tester"); + + ArgumentCaptor argument = ArgumentCaptor.forClass(LogEvent.class); + verify(mockEventHandler, timeout(5000)).dispatchEvent(argument.capture()); + assertEquals(argument.getValue().getEventBatch().getClientName(), "android-sdk"); + assertEquals(argument.getValue().getEventBatch().getClientVersion(), BuildConfig.CLIENT_VERSION); + } + +} diff --git a/android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java b/android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java index e46192c1..9e8c3ae1 100644 --- a/android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java +++ b/android-sdk/src/main/java/com/optimizely/ab/android/sdk/OptimizelyManager.java @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright 2017-2021, Optimizely, Inc. and contributors * + * Copyright 2017-2022, Optimizely, Inc. and contributors * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * @@ -86,6 +86,7 @@ public class OptimizelyManager { @Nullable private OptimizelyStartListener optimizelyStartListener; @NonNull private final List defaultDecideOptions; + private String sdkVersion = null; OptimizelyManager(@Nullable String projectId, @Nullable String sdkKey, @@ -122,6 +123,13 @@ public class OptimizelyManager { this.userProfileService = userProfileService; this.notificationCenter = notificationCenter; this.defaultDecideOptions = defaultDecideOptions; + + try { + sdkVersion = BuildConfig.CLIENT_VERSION; + logger.info("SDK Version: {}", sdkVersion); + } catch (Exception e) { + logger.warn("Error getting BuildConfig version"); + } } @VisibleForTesting @@ -580,8 +588,8 @@ private OptimizelyClient buildOptimizely(@NonNull Context context, @NonNull Stri builder.withDatafile(datafile); } - builder.withClientEngine(clientEngine) - .withClientVersion(BuildConfig.CLIENT_VERSION); + // override client sdk name/version to be included in events + builder.withClientInfo(clientEngine, sdkVersion); if (errorHandler != null) { builder.withErrorHandler(errorHandler); diff --git a/build.gradle b/build.gradle index d900f3cf..d12c51b4 100644 --- a/build.gradle +++ b/build.gradle @@ -19,13 +19,11 @@ buildscript { ext.kotlin_version = '1.4.0' - def version_name = System.getenv('TRAVIS_TAG') - if (version_name != null) { - rootProject.ext.version_name = version_name - } else { - rootProject.ext.version_name = 'debugVersion' + ext.version_name = System.getenv('TRAVIS_TAG') + if (version_name == null || version_name.isEmpty()) { + ext.version_name = 'debugVersion' } - ext.is_release_version = !rootProject.ext.version_name.endsWith("SNAPSHOT") + ext.is_release_version = !version_name.endsWith("SNAPSHOT") repositories { jcenter() @@ -63,7 +61,7 @@ ext { build_tools_version = "30.0.3" min_sdk_version = 14 target_sdk_version = 29 - java_core_ver = "3.10.1" + java_core_ver = "3.10.2" android_logger_ver = "1.3.6" jacksonversion= "2.11.2" annotations_ver = "1.0.0" @@ -99,7 +97,8 @@ task testAllModulesTravis () { dependsOn(':android-sdk:connectedAndroidTest', ':android-sdk:test', ':event-handler:connectedAndroidTest', ':event-handler:test', ':datafile-handler:connectedAndroidTest', ':datafile-handler:test', - ':user-profile:connectedAndroidTest', ':shared:connectedAndroidTest') + ':user-profile:connectedAndroidTest', + ':shared:connectedAndroidTest') } // Publish to MavenCentral