目前,我知道获取Google广告ID(手动)的唯一方法是打开设置并转到帐户 - > Google - >广告。它显示在"您的广告ID"
下但是,我想通过命令行使用adb获取Google Advertising Id。我希望将其自动化以进行测试。
例如,这就是我获取android id的方式:
android-sdks/platform-tools/adb shell settings get secure android_id
是否有用于检索广告ID的等效命令?
提前感谢您的帮助。
答案 0 :(得分:6)
是否有用于检索广告ID的等效命令?
没有等效命令,但如果您使用grep
则可以。以下命令适用于 rooted 设备
adb shell grep adid_key /data/data/com.google.android.gms/shared_prefs/adid_settings.xml
答案 1 :(得分: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文档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广告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广告ID,以便于复制和粘贴!
The Advertising Id for this device is: deadbeef-4242-6969-1337-0123456789ab
<强>限制强>