Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only install for the current user #57

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/android_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ if [ -z "$1" ];
then
cargo apk run -p ndk-examples --target x86_64-linux-android --example hello_world --no-logcat
else
adb install -r "$1/hello_world.apk"
user_id=$(adb shell am get-current-user)
if [ -z "$user_id" ];
then
adb install -r "$1/hello_world.apk"
else
adb install --user $user_id -r "$1/hello_world.apk"
fi
adb shell am start -a android.intent.action.MAIN -n "rust.example.hello_world/android.app.NativeActivity"
fi

Expand Down
18 changes: 12 additions & 6 deletions cargo-apk/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct ApkBuilder<'a> {
ndk: Ndk,
manifest: Manifest,
build_dir: PathBuf,
current_user: Option<String>,
build_targets: Vec<Target>,
device_serial: Option<String>,
}
Expand Down Expand Up @@ -44,6 +45,7 @@ impl<'a> ApkBuilder<'a> {
.detect_abi(device_serial.as_deref())
.unwrap_or(Target::Arm64V8a)]
};
let current_user = ndk.get_current_user(device_serial.as_deref());
let build_dir = dunce::simplified(cmd.target_dir())
.join(cmd.profile())
.join("apk");
Expand Down Expand Up @@ -132,6 +134,7 @@ impl<'a> ApkBuilder<'a> {
manifest,
build_dir,
build_targets,
current_user,
device_serial,
})
}
Expand Down Expand Up @@ -310,15 +313,18 @@ impl<'a> ApkBuilder<'a> {
}

pub fn run(&self, artifact: &Artifact, no_logcat: bool) -> Result<(), Error> {
let device_serial = self.device_serial.as_deref();
let user_id = self.current_user.as_deref();

let apk = self.build(artifact)?;
apk.reverse_port_forwarding(self.device_serial.as_deref())?;
apk.install(self.device_serial.as_deref())?;
apk.start(self.device_serial.as_deref())?;
let uid = apk.uidof(self.device_serial.as_deref())?;
apk.reverse_port_forwarding(device_serial)?;
apk.install(device_serial, user_id)?;
apk.start(device_serial)?;
let uid = apk.uidof(device_serial, user_id)?;

if !no_logcat {
self.ndk
.adb(self.device_serial.as_deref())?
.adb(device_serial)?
.arg("logcat")
.arg("-v")
.arg("color")
Expand All @@ -332,7 +338,7 @@ impl<'a> ApkBuilder<'a> {

pub fn gdb(&self, artifact: &Artifact) -> Result<(), Error> {
let apk = self.build(artifact)?;
apk.install(self.device_serial.as_deref())?;
apk.install(self.device_serial.as_deref(), self.current_user.as_deref())?;

let target_dir = self.build_dir.join(artifact.build_dir());
self.ndk.ndk_gdb(
Expand Down
22 changes: 20 additions & 2 deletions ndk-build/src/apk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,19 @@ impl Apk {
Ok(())
}

pub fn install(&self, device_serial: Option<&str>) -> Result<(), NdkError> {
pub fn install(
&self,
device_serial: Option<&str>,
user_id: Option<&str>,
) -> Result<(), NdkError> {
let mut adb = self.ndk.adb(device_serial)?;

adb.arg("install").arg("-r").arg(&self.path);

if let Some(uid) = user_id {
adb.arg("--user").arg(uid);
}

if !adb.status()?.success() {
return Err(NdkError::CmdFailed(adb));
}
Expand All @@ -309,14 +318,23 @@ impl Apk {
Ok(())
}

pub fn uidof(&self, device_serial: Option<&str>) -> Result<u32, NdkError> {
pub fn uidof(
&self,
device_serial: Option<&str>,
user_id: Option<&str>,
) -> Result<u32, NdkError> {
let mut adb = self.ndk.adb(device_serial)?;
adb.arg("shell")
.arg("pm")
.arg("list")
.arg("package")
.arg("-U")
.arg(&self.package_name);

if let Some(uid) = user_id {
adb.arg("--user").arg(uid);
}

let output = adb.output()?;

if !output.status.success() {
Expand Down
15 changes: 15 additions & 0 deletions ndk-build/src/ndk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,21 @@ impl Ndk {
Target::from_android_abi(abi.trim())
}

pub fn get_current_user(&self, device_serial: Option<&str>) -> Option<String> {
let mut adb = self.adb(device_serial).ok()?;

adb.arg("shell").arg("am").arg("get-current-user");
let output = adb.output().ok()?;

if !output.status.success() {
return None;
}

let stdout = String::from_utf8(output.stdout).ok()?;

stdout.lines().next().map(|s| s.to_owned())
}

pub fn adb(&self, device_serial: Option<&str>) -> Result<Command, NdkError> {
let mut adb = Command::new(self.adb_path()?);

Expand Down
Loading