android / privacy-sandbox-samples

Apache License 2.0
130 stars 52 forks source link

Topics API is not available on emulator image TiramisuPrivacySandbox revision 9 #61

Closed okstartapp closed 1 year ago

okstartapp commented 1 year ago

I get the following exception while trying to run the app from TopicsJava project.

java.lang.IllegalStateException: Service is not available.
  at com.android.adservices.AndroidServiceBinder.getService(AndroidServiceBinder.java:146)
  at android.adservices.topics.TopicsManager.getService(TopicsManager.java:102)
  at android.adservices.topics.TopicsManager.getTopics(TopicsManager.java:132)
  at com.example.adservices.samples.topics.sampleapp.MainActivity.TopicGetter(MainActivity.java:94)
  at com.example.adservices.samples.topics.sampleapp.MainActivity.onResume(MainActivity.java:79)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1564)
  at android.app.Activity.performResume(Activity.java:8437)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4763)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4806)
  at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:54)
  at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
  at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
  at android.os.Handler.dispatchMessage(Handler.java:106)
  at android.os.Looper.loopOnce(Looper.java:201)
  at android.os.Looper.loop(Looper.java:288)
  at android.app.ActivityThread.main(ActivityThread.java:7872)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Steps that have been executed

adb shell device_config put adservices ppapi_app_allow_list \"*\"

This command doesn't work in my command line (MasOS 12.4), so I executed the first part: adb shell and only then I was able to execute device_config put adservices ppapi_app_allow_list \"*\".

adb shell setprop debug.adservices.disable_topics_enrollment_check true
adb shell setprop debug.adservices.topics_epoch_job_period_ms 300000

These two commands were executed without errors.

Then I executed the following script:

#!/bin/bash
# Per Topics data/seed app
for i in {0..10} # Number of apps to install
do
  # Build and install on device or emulator
  echo "${installationString:= installSampleapp${i}_PreviewDebug}"
  ./gradlew ${installationString}
  unset installationString
  # Run app 10 times
   for j in {1..10}
   do
      echo "Running app $i, instance number $j"
      # Start app's main Activity
    adb shell am start -n "com.example.adservices.samples.topics.sampleapp$i/com.example.adservices.samples.topics.sampleapp.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
      # "Press/Swipe" Home button
     adb shell input keyevent KEYCODE_HOME
  done
done

After that I can see sample apps installed on my emulator.

Once I launched one of these apps, I pressed the Launch Settings App button and turned on the switch Privacy Sandbox.

Environment

Android Studio Giraffe | 2022.3.1 Canary 11 Google Play ARM 64 v8a System Image, TiramisuPrivacySandbox, revision 9

jose4gonzalez commented 1 year ago

Could you try running these commands and then try to run the app again?

adb shell device_config put adservices ppapi_app_signature_allow_list "\"*\""

adb shell device_config put adservices adservice_system_service_enabled true

adb shell device_config put adservices adservice_enabled true

adb shell device_config put adservices adservice_enable_status true

adb shell device_config put adservices global_kill_switch false

adb shell device_config put adservices disable_sdk_sandbox false
cshmerling commented 1 year ago

Hey @okstartapp We released a new SDK, emulator, and device images including Privacy Sandbox support for UDC. You can find these as "UpsideDownCakePrivacySandbox" in Android Studio

okstartapp commented 1 year ago

Hey @okstartapp We released a new SDK, emulator, and device images including Privacy Sandbox support for UDC. You can find these as "UpsideDownCakePrivacySandbox" in Android Studio

Hello @cshmerling , I've updated everything to the most recent versions and I still get the same error. Moreover, I can see such error in logcat right before the exception:

adservices              com...ices.samples.topics.sampleapp  E  onNullBinding android.adservices.TOPICS_SERVICE
cshmerling commented 1 year ago

Thanks for checking back on this- it seems our documentation may be missing a step specifically to enable the Topics API on the device. Assuming you have enabled the Privacy Sandbox via the UI (by opening it via adb shell am start -n com.google.android.adservices.api/com.android.adservices.ui.settings.activities.AdServicesSettingsMainActivity The total commands to be run would be:

adb shell device_config set_sync_disabled_for_tests persistent 
adb shell device_config put adservices ppapi_app_allow_list \"\*\"
adb shell device_config put adservices ppapi_app_signature_allow_list \"\*\"
adb shell device_config put adservices adservice_system_service_enabled true
adb shell device_config put adservices adservice_enabled true
adb shell device_config put adservices adservice_enable_status true
adb shell device_config put adservices global_kill_switch false
adb shell device_config put adservices topics_kill_switch false

With the first and last commands being new. The first command will disable your device from resetting to the default where everything is disabled, and the last command will actually enable the Topics API.

Let me know if that works and I can see that our documentation gets updated to reflect this.

okstartapp commented 1 year ago

Now I can get the TopicsManager reference, but it returns me empty list of topics. I can see that there's no apps generating topics via the UI (see screenshots).

I also tried to use preinstalled apps such as YouTube and Chrome and then, according to the setting below, I waited for at least 5 minutes in order to give enough time to the system to generate some topics for me.

adb shell setprop debug.adservices.topics_epoch_job_period_ms 300000

But I still get empty list in onResult() callback.

Screenshot_20230731_143717 (1) Screenshot_20230731_143732 (1)

cshmerling commented 1 year ago

There a few more steps to be able to get Topics- namely only apps that integrate with Topics are considered, hence why none of the pre-installed apps are giving you any results. If you take a look at our (testing section)[https://github.com/android/privacy-sandbox-samples/tree/main/TopicsJava#testing] (specifically, towards the bottom we have the Note about running some sample apps), you will be able to see some Topics being inferred.

okstartapp commented 1 year ago

Eventually, I got positive results. Thank you @cshmerling !

Screenshot_20230801_112209