programing

.apk 파일을 사용하여 Android 애플리케이션을 실행 (설치할뿐만 아니라)하는 방법은 무엇입니까?

goodcopy 2021. 1. 19. 08:05
반응형

.apk 파일을 사용하여 Android 애플리케이션을 실행 (설치할뿐만 아니라)하는 방법은 무엇입니까?


해당 응용 프로그램 cmd.exe.apk파일을 사용하여 특정 Android 응용 프로그램의 주요 활동을 시작할 수 있는 명령이 있습니까? Android 애플리케이션 만 설치하는이 명령을 알고 있습니다.

adb install myapp.apk

이 명령은 myapp에뮬레이터 에만 설치 되며 에뮬레이터에서이 애플리케이션을 수동으로 실행해야합니다 (아이콘을 한 번 클릭하여 수행).

내가하고 싶은 것은 응용 프로그램을 설치할뿐만 아니라 주요 활동을 시작하는 명령을 사용하는 것입니다 ( .apk파일 만 가지고 있으므로 패키지 이름이나 활동 이름이 무엇인지 알 수 없습니다).


한 번에 설치하고 실행할 수는 없지만 확실히 adb를 사용하여 이미 설치된 애플리케이션을 시작할 수 있습니다. 인 텐트를 실행하려면 adb shell am start를 사용하세요.하지만 애플리케이션에 올바른 인 텐트를 사용해야합니다. 몇 가지 예 :

adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings 

설정을 시작하고

adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity 

브라우저가 시작됩니다. 브라우저가 특정 페이지를 가리 키도록하려면 다음을 수행하십시오.

adb shell am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity http://www.google.co.uk

APK의 활동 이름을 모르는 경우 다음을 수행하십시오.

aapt d xmltree <path to apk> AndroidManifest.xml

출력 내용에는 다음과 같은 섹션이 포함됩니다.

   E: activity (line=32)
    A: android:theme(0x01010000)=@0x7f080000
    A: android:label(0x01010001)=@0x7f070000
    A: android:name(0x01010003)="com.anonymous.MainWindow"
    A: android:launchMode(0x0101001d)=(type 0x10)0x3
    A: android:screenOrientation(0x0101001e)=(type 0x10)0x1
    A: android:configChanges(0x0101001f)=(type 0x11)0x80
    E: intent-filter (line=33)
      E: action (line=34)
        A: android:name(0x01010003)="android.intent.action.MAIN"
        XE: (line=34)

그러면 기본 활동 (MainWindow)의 이름이 표시되며 이제 다음을 실행할 수 있습니다.

adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow

"adb run myapp.apk"에 해당하는 것을 찾고 있다면

답변에 표시된 스크립트를 사용할 수 있습니다.

(리눅스 및 맥 전용-Windows에서 cygwin 사용 가능)

Linux / mac 사용자는 다음과 같이 apk를 실행하는 스크립트를 만들 수도 있습니다.

다음 세 줄로 "adb-run.sh"라는 파일을 만듭니다.

pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act

그런 다음 "chmod + x adb-run.sh"를 실행하여 실행합니다.

이제 간단하게 다음을 수행 할 수 있습니다.

adb-run.sh myapp.apk

여기서의 이점은 패키지 이름이나 실행 가능한 활동 이름을 알 필요가 없다는 것입니다. 마찬가지로 "adb-uninstall.sh myapp.apk"를 만들 수 있습니다.

참고 : 경로에 aapt가 있어야합니다. SDK의 새 빌드 도구 폴더에서 찾을 수 있습니다.


GUI에서 앱을 시작하면 adb logcat해당 작업 / 카테고리 / 구성 요소가 표시 될 수 있습니다.

$ adb logcat
[...]
I / ActivityManager (1607) : START {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10200000 cmp = com.android.browser / .BrowserActivity } pid 1792에서
[...]


이것은 쉘 스크립트의 솔루션입니다.

apk="$apk_path"

1. apk 설치

adb install "$apk"
sleep 1

2. 패키지 이름 가져 오기

pkg_info=`aapt dump badging "$apk" | head -1 | awk -F " " '{print $2}'`
eval $pkg_info > /dev/null

3. 앱 시작

pkg_name=$name
adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1

나는 이것을 내 makefile에 넣습니다. adb install ...

adb shell monkey -p `cat .identifier` -c android.intent.category.LAUNCHER 1

이 작업을 수행하려면 앱의 번들 식별자가있는 .identifier 파일이 있어야합니다. com.company.ourfirstapp

활동 이름을 찾을 필요가 없습니다.


단일 명령을 사용하여 apk를 설치하고 실행하기 위해 터미널 별칭을 만들었습니다.

// I use ZSH, here is what I added to my .zshrc file (config file)
// at ~/.zshrc
// If you use bash shell, append it to ~/.bashrc

# Have the adb accessible, by including it in the PATH
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:path/to/android_sdk/platform-tools/"

# Setup your Android SDK path in ANDROID_HOME variable
export ANDROID_HOME=~/sdks/android_sdk

# Setup aapt tool so it accessible using a single command
alias aapt="$ANDROID_HOME/build-tools/27.0.3/aapt"

# Install APK to device
# Use as: apkinstall app-debug.apk
alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
# As an alternative to apkinstall, you can also do just ./gradlew installDebug

# Alias for building and installing the apk to connected device
# Run at the root of your project
# $ buildAndInstallApk
alias buildAndInstallApk='./gradlew assembleDebug && apkinstall ./app/build/outputs/apk/debug/app-debug.apk'

# Launch your debug apk on your connected device
# Execute at the root of your android project
# Usage: launchDebugApk
alias launchDebugApk="adb shell monkey -p `aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name' | cut -d \' -f 2` 1"

# ------------- Single command to build+install+launch apk------------#
# Execute at the root of your android project
# Use as: buildInstallLaunchDebugApk
alias buildInstallLaunchDebugApk="buildAndInstallApk && launchDebugApk"

참고 : 여기서는 일반적으로 경로에있는 디버그 APK를 빌드, 설치 및 실행합니다. ./app/build/outputs/apk/debug/app-debug.apk이 명령이 프로젝트의 루트에서 실행될 때

다른 apk를 설치하고 실행하려면 디버그 apk 경로를 자신의 apk 경로로 바꾸십시오.

Here is the gist for the same. I created this because I was having trouble working with Android Studio build reaching around 28 minutes, so I switched over to terminal builds which were around 3 minutes. You can read more about this here

Explanation:

The one alias that I think needs explanation is the launchDebugApk alias. Here is how it is broken down:

The part aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name basically uses the aapt tool to extract the package name from the apk.

Next, is the command: adb shell monkey -p com.package.name 1, which basically uses the monkey tool to open up the default launcher activity of the installed app on the connected device. The part of com.package.name is replaced by our previous command which takes care of getting the package name from the apk.


First to install your app:

adb install -r path\ProjectName.apk

The great thing about the -r is it works even if it wasn’t already installed.

To launch MainActivity, so you can launch it like:

adb shell am start -n com.other.ProjectName/.MainActivity

ReferenceURL : https://stackoverflow.com/questions/6109234/how-to-run-not-only-install-an-android-application-using-apk-file

반응형