Search code examples
androidapkandroid-sourceandroid-10.0android-make

APK built using Android.mk mm command not running in device after install


I am following https://wladimir-tm4pda.github.io/porting/build_cookbook.html and have modified Building a simple APK

The Android.mk file is at the top level.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_STATIC_ANDROID_LIBRARIES += \
    android-arch-lifecycle-extensions \
    android-support-v7-recyclerview \
    android-support-v7-appcompat \
    android-support-constraint-layout \

# Build all java files in the java subdirectory
#LOCAL_SRC_FILES := $(call all-subdir-java-files)
#Commented line just made a ~17kB apk file
LOCAL_SRC_FILES := $(call all-java-files-under, app/src/main/java)
LOCAL_RESOURCE_DIR += $(LOCAL_PATH)/app/src/main/res

# Name of the APK to build
LOCAL_PACKAGE_NAME := LocalPackage

LOCAL_SDK_VERSION := current
#Otherwise build failing

# Tell it to build an APK
include $(BUILD_PACKAGE)

I have included it in Android 10 AOSP and built using mm command in terminal.

The output apk is 1MB big (should be 5 MB) and after doing adb install -r -t -f LocalPackage.apk it does not show up in the apps list. When built in Android Studio it is working fine.

My aim is to make this app a system app but first step is making mm build succeed. How to rectify this?

enter image description here

enter image description here

UPDATE:

I now changed the location of Android.mk file to where AndroidManifest.xml is present and also changed my Android.mk.

ifeq ($(TARGET_FWK_SUPPORTS_FULL_VALUEADDS),true)
ifeq ($(TARGET_BUILD_JAVA_SUPPORT_LEVEL),platform)

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

res_dir := res $(LOCAL_PATH)/res

LOCAL_MODULE_TAGS := optional
#LOCAL_SDK_VERSION := current

LOCAL_SRC_FILES := $(call all-java-files-under, java)

LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dir))
LOCAL_USE_AAPT2 := true
LOCAL_JAVA_LIBRARIES := com.google.android.material_material \

LOCAL_STATIC_ANDROID_LIBRARIES := \
    androidx.core_core \
    androidx.appcompat_appcompat \
    androidx.fragment_fragment \
    android-support-constraint-layout \

LOCAL_DEX_PREOPT := false
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true
#LOCAL_PRODUCT_MODULE := true
LOCAL_PACKAGE_NAME := mediadecode
LOCAL_PRIVATE_PLATFORM_APIS := true

include $(BUILD_PACKAGE)

endif
endif

Now I am getting a 2MB app and the app shows up on the device after install but crashes as soon as I run it with the error

--------- beginning of crash
04-21 21:43:20.852  4319  4319 E AndroidRuntime: FATAL EXCEPTION: main
04-21 21:43:20.852  4319  4319 E AndroidRuntime: Process: com.example.mediadecode, PID: 4319
04-21 21:43:20.852  4319  4319 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mediadecode/com.example.mediadecode.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.mediadecode.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.mediadecode-vTqIZ9aZv0FsEDQ8xzj4GA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.mediadecode-vTqIZ9aZv0FsEDQ8xzj4GA==/lib/arm64, /system/lib64, /system/product/lib64]]
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3195)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:107)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:214)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:7397)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
04-21 21:43:20.852  4319  4319 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.mediadecode.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.mediadecode-vTqIZ9aZv0FsEDQ8xzj4GA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.mediadecode-vTqIZ9aZv0FsEDQ8xzj4GA==/lib/arm64, /system/lib64, /system/product/lib64]]
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:196)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.Instrumentation.newActivity(Instrumentation.java:1251)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3183)
04-21 21:43:20.852  4319  4319 E AndroidRuntime:        ... 11 more

The contents of the apk formed are enter image description here

The contents on building with Gradle is enter image description here

How can I build my apk with mm and run it successfully?


Solution

  • I was able to use Soong compilation for my Kotlin Android app. I used the following Android.bp file.

    android_app {
      name: "Camera2Video2",
      manifest: "AndroidManifest.xml",
      privileged: true,
      platform_apis: true,
      certificate: "platform",
    
      srcs: [
        "java/com/example/android/camera2/video/*.kt",
        "java/com/example/android/camera2/video/fragments/*.kt",
        "java/com/example/android/camera2/video/overlay/*.kt",
        "java/com/example/android/camera2/video/utils/*.kt",
      ],
    
      resource_dirs: [
        "res/",
      ],
    
      static_libs: [
        "androidx-constraintlayout_constraintlayout",
        "androidx.appcompat_appcompat",
        "androidx.localbroadcastmanager_localbroadcastmanager",
        "com.google.android.material_material",
        "androidx.exifinterface_exifinterface",
        "androidx.core_core",
        "androidx.preference_preference",
        "androidx.fragment_fragment",
      ],
    
      libs: [
        "telephony-common",
      ],
      optimize: {
            enabled: false,
      },
      dex_preopt: {
        enabled: false,
      },
    }
    
    

    I also tried compiling my Android app after converting it fully to Java. Here is the Android.mk file

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    res_dir := res $(LOCAL_PATH)/res
    
    LOCAL_MODULE_TAGS := optional
    #LOCAL_SDK_VERSION := current
    
    LOCAL_SRC_FILES := $(call all-java-files-under, java)
    
    LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dir))
    LOCAL_USE_AAPT2 := true
    
    LOCAL_JAVA_LIBRARIES := com.google.android.material_material \
    
    LOCAL_STATIC_ANDROID_LIBRARIES := \
            androidx.appcompat_appcompat \
            androidx-constraintlayout_constraintlayout \
            androidx.preference_preference \
            androidx.fragment_fragment \
            androidx.core_core \
    
    #LOCAL_DEX_PREOPT := false
    LOCAL_CERTIFICATE := platform
    LOCAL_PRIVILEGED_MODULE := true
    #LOCAL_PRODUCT_MODULE := true
    LOCAL_PACKAGE_NAME := QMedia
    LOCAL_PRIVATE_PLATFORM_APIS := true
    
    include $(BUILD_PACKAGE)