如何使用adb获取广告ID?

问题描述 投票:4回答:2

目前,我知道获取Google广告ID(手动)的唯一方法是打开设置并转到帐户 - > Google - >广告。它显示在“您的广告ID”下

但是,我想通过命令行使用adb获取Google Advertising Id。我想将其自动化以进行测试。

例如,这是我获取android id的方式:

android-sdks/platform-tools/adb shell settings get secure android_id

是否有用于检索广告ID的等效命令?

感谢您的帮助。

android bash adb advertising
2个回答
7
投票

是否有用于检索广告ID的等效命令?

没有等效的命令但是如果你使用grep就有可能。以下命令适用于rooted设备

adb shell grep adid_key /data/data/com.google.android.gms/shared_prefs/adid_settings.xml

4
投票

我设计了一种迂回的方式来做到这一点,而无需根植你的手机。它需要一些Android UI测试和一些bash shell脚本的工作。所有这些代码都是由我编写的,如果您发现任何错误或改进建议,请告诉我。

基本上我们将显示“Google广告设置”的设置屏幕并使用UI Automator抓取文本,并使用bash shell脚本自动执行所有操作,包括使用grep来提取广告ID。

步骤1.创建测试项目

在Eclipse中创建一个名为GoogleAIDTest的新项目。在Java Build Path下,您需要添加JUnit3库,然后添加外部JAR uiautomator.jar和android.jar。您可以在Android Docs:UI Testing / Configure your development environment下找到有关此内容的更多详细信息

现在将com.testing.googleaidtest命名空间添加到项目中,然后添加一个名为GoogleAidTest的类。内容如下

package com.testing.googleaidtest;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class GoogleAidTest extends UiAutomatorTestCase {

public void testDemo() throws UiObjectNotFoundException {   


    final int AD_ID_LINEAR_LAYOUT_INDEX = 3;
    // Get the 4th Linear Layout (index 3) and find the RelativeLayout and the TextView within it
    UiObject childText = new UiObject(new UiSelector()
       .className("android.widget.LinearLayout").index(AD_ID_LINEAR_LAYOUT_INDEX)
       .childSelector(new UiSelector().className("android.widget.RelativeLayout"))
       .childSelector(new UiSelector().className("android.widget.TextView")));  

    assertTrue("Unable to find textView", 
            childText.exists());

    System.out.println("The content of this child is: " + childText.getText());

    // The content should be like this, with a \n as a separator between lines
    //Your advertising ID:
    //deadbeef-4242-6969-1337-0123456789ab

    int newlineIndex = childText.getText().indexOf('\n');
    if (newlineIndex != -1) {
        // Debug output to confirm there is a newline
        System.out.println("Index of newline is at index " + String.valueOf(newlineIndex));

        // Split the lines
        String[] parts = childText.getText().split("\n");

        // Confirm we have the second line 
        if (parts != null && parts[1] != null) {
            System.out.println("The Advertising Id for this device is: " + parts[1]);
        } else {
            System.out.println("Could not split the line!");
        }
    } else {
        System.out.println("Could not find the newline!");
    }

  } 

第2步。构建测试jar

您需要运行以下命令来创建“构建输出JAR所需的构建配置文件”

<android-sdk>/tools/android create uitest-project -n GoogleAidTest -t 1 -p <path to project>

准备好构建后,在GoogleAidTest项目的基目录中运行以下命令:

ant build

在您的bin目录中,您将拥有一个名为GoogleAidTest.jar的文件,将其复制到与下一部分中的脚本相同的目录中(或者您想要的任何位置,但您必须为您的环境调整以下脚本)

步骤3.制作名为“getgaid.sh”的shell脚本,以显示AdSettingsActivity并抓取Google Advertising Id

#!/bin/sh
# Only works on devices with API level 16 and above (due to uiautomator) 
##
# NOTE:
# This script requires that you build GoogleAidTest beforehand and put the jar file in current working directory

# Change adbPath to your own android sdk path
adbPath="$HOME/android-sdks/platform-tools/adb"

# Check for adb and GoogleAidTest.jar existence before test
if [ ! -f "$adbPath" ]; then 
    echo "adb not found!"
elif [ ! -f "GoogleAidTest.jar" ]; then
    echo "GoogleAidTest.jar not found!"
else
    # Start Ad Settings Activity
    $adbPath shell am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity

    # Push the GoogleAidTest jar to the phone, GoogleAidTest.jar should be in the current working directory
    $adbPath push GoogleAidTest.jar /data/local/tmp/

    # Run the test, using grep to pull out the relevant line
    $adbPath shell uiautomator runtest GoogleAidTest.jar -c com.testing.googleaidtest.GoogleAidTest | grep "Advertising Id"

    # Stop the Ad Settings Activity to clean up 
    $adbPath shell am force-stop com.google.android.gms
fi

步骤4.使脚本可执行

chmod +x getgaid.sh

步骤5.运行脚本

./getgaid.sh

广告的设置屏幕将简要显示在您的设备上。该脚本现在正在从UI中抓取广告ID文本。完成后,活动停止。

在命令行上,将显示Google Advertising Id,以便轻松复制和粘贴!

The Advertising Id for this device is: deadbeef-4242-6969-1337-0123456789ab

限制

  • 请注意,这仅适用于API级别为16的设备
  • 如果“广告”设置页面(AdsSettingsActivity)的UI发生更改,则测试脚本可能不再起作用。我使用UI Automator Viewer(在android-sdks / tools / uiautomatorviewer中找到)来找到控件的正确索引。请根据需要调整。
© www.soinside.com 2019 - 2024. All rights reserved.