build android 10 with 8G ram laptop

you may encounter problem when building android with low ram machine

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.base/java.util.Arrays.copyOf(Arrays.java:3793)
at com.intellij.util.ArrayUtil.realloc(ArrayUtil.java:84)
at com.intellij.lang.impl.TokenSequence$Builder.resizeLexemes(TokenSequence.java:114)
at com.intellij.lang.impl.TokenSequence$Builder.performLexing(TokenSequence.java:77)
at com.intellij.lang.impl.PsiBuilderImpl.performLexing(PsiBuilderImpl.java:187)
at com.intellij.lang.impl.PsiBuilderImpl.(PsiBuilderImpl.java:160)
at com.intellij.lang.impl.PsiBuilderImpl.(PsiBuilderImpl.java:114)
at com.intellij.lang.impl.PsiBuilderFactoryImpl.createBuilder(PsiBuilderFactoryImpl.java:37)
at com.intellij.lang.java.parser.JavaParserUtil.createBuilder(JavaParserUtil.java:152)
at com.intellij.psi.impl.source.JavaFileElementType.parseContents(JavaFileElementType.java:80)
at com.intellij.psi.impl.source.tree.LazyParseableElement.lambda$ensureParsed$0(LazyParseableElement.java:189)
at com.intellij.psi.impl.source.tree.LazyParseableElement$$Lambda$217/0x0000000100515040.run(Unknown Source)
at com.intellij.psi.impl.DebugUtil.performPsiModification(DebugUtil.java:564)
at com.intellij.psi.impl.source.tree.LazyParseableElement.ensureParsed(LazyParseableElement.java:188)
at com.intellij.psi.impl.source.tree.LazyParseableElement.getFirstChildNode(LazyParseableElement.java:237)
at com.intellij.psi.impl.source.tree.CompositeElement.countChildren(CompositeElement.java:432)
at com.intellij.psi.impl.source.tree.CompositeElement.getChildrenAsPsiElements(CompositeElement.java:394)
at com.intellij.psi.impl.source.PsiJavaFileBaseImpl.getClasses(PsiJavaFileBaseImpl.java:66)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$Companion.findClassInPsiFile(KotlinCliJavaFileManagerImpl.kt:263)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl$Companion.access$findClassInPsiFile(KotlinCliJavaFileManagerImpl.kt:259)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findPsiClassInVirtualFile(KotlinCliJavaFileManagerImpl.kt:229)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl.findClass(KotlinCliJavaFileManagerImpl.kt:124)
at org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade$CliFinder.findClass(KotlinJavaPsiFacade.java:363)
at org.jetbrains.kotlin.resolve.jvm.KotlinJavaPsiFacade.findClass(KotlinJavaPsiFacade.java:123)
at org.jetbrains.kotlin.load.java.JavaClassFinderImpl.findClass(JavaClassFinderImpl.kt:36)
at org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope$classes$1.invoke(LazyJavaPackageScope.kt:76)
at org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope$classes$1.invoke(LazyJavaPackageScope.kt:42)
at org.jetbrains.kotlin.storage.LockBasedStorageManager$MapBasedMemoizedFunction.invoke(LockBasedStorageManager.java:512)
at org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope.findClassifier(LazyJavaPackageScope.kt:142)
at org.jetbrains.kotlin.load.java.lazy.descriptors.LazyJavaPackageScope.getContributedClassifier(LazyJavaPackageScope.kt:132)
at org.jetbrains.kotlin.load.java.lazy.descriptors.JvmPackageScope.getContributedClassifier(JvmPackageScope.kt:52)
at org.jetbrains.kotlin.resolve.scopes.ChainedMemberScope.getContributedClassifier(ChainedMemberScope.kt:33)

have a check for your build machine

java -XshowSettings 2>&1  | grep Heap

https://cs.android.com/android/platform/superproject/+/master:build/soong/java/config/config.go;l=64?q=build%2Fsoong%2Fjava%2Fconfig%2Fconfig.go

change this line from 2048 to 1024

pctx.StaticVariable("JavacHeapSize", "1024M")
android metalava use a lot of memory

 

or you can also try adjust linux memory setting

cat /proc/sys/vm/overcommit_ratio
cat /proc/sys/vm/overcommit_memory

 

But I still fail to build AOSP and seems adding swap has no help for java max heap size

refer: https://stackoverflow.com/a/34858395/1117177


Do. Not. Do this.

Get more memory.

Java and Swap Do. Not. Mix. Java and Swap are like "Crossing the Streams" Bad.

Part of the Java heap will be swapped out, then Java will GC, which will thrash the box terribly, and still part of the heap will be swapped out, and Java will GC again.

Rinse and repeat until you finally kill -9 Weblogic.

Get more memory, or do without. Swap is not an option.

Addenda:

You machine has "Virtual Memory" (VM). By adding swap, you extend that VM. When you have more VM than actual memory, then the machine will "swap out" inactive memory pages to the swap space, and then "swap in" pages that want to be active from swap space in to RAM. If you have a bunch of idle processes that rarely need time, this isn't super terrible.

But Java magnifies the problem. When you run out of physical RAM, some of that RAM is swapped out to disk. When some of that RAM is Java heap, then you have a big problem when the garbage collector kicks in. Because, in general, the GC touches every page of the heap. That means that all of the heaps pages need to be "active", and thus, "swapped in".

But if they don't fit, then you have a problem like the 8 puzzle. Where you're shifting memory in and out from disk. This is called thrashing.

A swapped JVM heap that is GC is the text book case of worst case thrashing.

The machine is letting you allocate the extra memory because it thinks you have more, since your Virtual Memory (physical RAM + swap) is greater

 

How to install google play on AOSP emulator

I want to know how google play install an app and why no dialog popup when google play install an app

currently PackageInstallActivity dialog will pop up even no any permission is required by the app which fdroid is installing

  1. download Gapps from https://opengapps.org/, e.g. I download open_gapps-arm64-9.0-stock-20191213
  2. mkdir -p device/xxx/apps
  3. add file device device/xxx/apps/Android.mk and edit as below
    LOCAL_PATH := $(call my-dir)

    include $(CLEAR_VARS)
    LOCAL_MODULE := Phonesky
    LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
    LOCAL_CERTIFICATE := PRESIGNED
    LOCAL_PRIVILEGED_MODULE := true
    LOCAL_MODULE_CLASS := APPS
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    LOCAL_DEX_PREOPT := true
    include $(BUILD_PREBUILT)

    include $(CLEAR_VARS)
    LOCAL_MODULE := GoogleServicesFramework
    LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
    LOCAL_CERTIFICATE := PRESIGNED
    LOCAL_PRIVILEGED_MODULE := true
    LOCAL_MODULE_CLASS := APPS
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    LOCAL_DEX_PREOPT := true
    include $(BUILD_PREBUILT)

    include $(CLEAR_VARS)
    LOCAL_MODULE := PrebuiltGmsCore
    LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
    LOCAL_CERTIFICATE := PRESIGNED
    LOCAL_PRIVILEGED_MODULE := true
    LOCAL_MODULE_CLASS := APPS
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    LOCAL_DEX_PREOPT := true
    include $(BUILD_PREBUILT)

    include $(CLEAR_VARS)
    LOCAL_MODULE := GmsCoreSetupPrebuilt
    LOCAL_SRC_FILES := $(LOCAL_MODULE).apk
    LOCAL_CERTIFICATE := PRESIGNED
    LOCAL_PRIVILEGED_MODULE := true
    LOCAL_MODULE_CLASS := APPS
    LOCAL_MODULE_TAGS := optional
    LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
    LOCAL_DEX_PREOPT := true
    include $(BUILD_PREBUILT)

  4. copy below 4 apps to device/xxx/apps/
    these 4 apps are required for login and install apps from google play
    Phonesky.apk, GoogleServicesFramework.apk, PrebuiltGmsCorePi.apk, GmsCoreSetupPrebuilt.apk
  5. update device/xxx/xxx_common.mk as below
    PRODUCT_PACKAGES += \
    libg1 \
    libhantro \
    – libcodec
    + libcodec \
    + Phonesky \
    + GoogleServicesFramework \
    + PrebuiltGmsCorePi \
    + GmsCoreSetupPrebuilt
  6. make and flash images to your device
  7. boot to Android OS and open google play, then you will see below messages
    0
  8. please read the part for custom rom users
    open https://g.co/AndroidDeviceRegistration
  9. adb shell ‘sqlite3 /data/data/com.google.android.gsf/databases/gservices.db “select * from main where name = \"android_id\";"‘
  10. 0
  11. and register your Google Service Framework Android ID
    you should can login google play after about 20 mins(according to my experience)

disable proximity sensor

In Android 10, there is a feature “Sensor off" which is to improve the privacy for you

your android device will become deaf and blind when “Sensor off" turned on, android device cannot hear the voice, cannot open camera, cannot detect proximity, etc…

The framework API is here https://cs.android.com/android/platform/superproject/+/master:out/soong/.intermediates/frameworks/base/framework-minus-apex/android_common/xref28/srcjars.xref/frameworks/base/core/java/android/hardware/ISensorPrivacyManager.java;l=222;bpv=1;bpt=1

and only Application signed with platform key can access this API

if app not signed with platform key call this function, it will crash and see below error message

Accessing hidden method Landroid/hardware/SensorPrivacyManager;->setSensorPrivacy(Z)V (blacklist, linking, denied)

 

below are some key point of code for a sample app which has ability to enable/disable sensor off

Android.mk

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

LOCAL_MODULE_TAGS := samples

# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)

LOCAL_PACKAGE_NAME := demo

LOCAL_SDK_VERSION := current

LOCAL_CERTIFICATE := platform

LOCAL_DEX_PREOPT := false

LOCAL_PRODUCT_MODULE := true

include $(BUILD_PACKAGE)

SensorPrivacyManager.java this file help you build pass since android.hardware.SensorPrivacyManager is not exist in SDK

package android.hardware;

public class SensorPrivacyManager {
    public void setSensorPrivacy(boolean enable) {
    }
}

HelloActivity.java

package com.idtech.SensorDisableDemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.util.Log;
import android.widget.Button;
import android.hardware.SensorPrivacyManager;

/**
 * A minimal "Hello, World!" application.
 */
public class HelloActivity extends Activity {
    private static final String TAG = "(Idtech)";
    private static final java.lang.String DESCRIPTOR = "sensor_privacy";
    private static final int FUNC_SUSPEND = 1;
    private static final int FUNC_RESUME = 2;
    Button mButton;
    Button mButton2;
    private SensorPrivacyManager mSensorPrivacyManager;
    /**
     * Called with the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the layout for this activity.  You can find it
        // in res/layout/hello_activity.xml
        View view = getLayoutInflater().inflate(R.layout.hello_activity, null);
        setContentView(view);
        mButton = (Button)findViewById(R.id.Button);
        mButton2 = (Button)findViewById(R.id.Button2);

        Log.i(TAG, "Client oncreate ");
        // Get an instance of Sensor Privacy Service.
        mSensorPrivacyManager = (SensorPrivacyManager)getSystemService(DESCRIPTOR);
        mButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                mSensorPrivacyManager.setSensorPrivacy(true);
            }
        });
        mButton2.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                mSensorPrivacyManager.setSensorPrivacy(false);
            }
        });
    }
}

 

 

 

 

Android fail to adjust audio volume

actually, it’s AOSP design

volume is either max or muted when HDMI out

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/audio/AudioService.java;l=507?q=mFixedVolumeDevices&ss=android

    // Devices for which the volume is fixed (volume is either max or muted)
    Set<Integer> mFixedVolumeDevices = new HashSet<>(Arrays.asList(
            AudioSystem.DEVICE_OUT_HDMI,
            AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET,
            AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET,
            AudioSystem.DEVICE_OUT_HDMI_ARC,
            AudioSystem.DEVICE_OUT_SPDIF,
            AudioSystem.DEVICE_OUT_AUX_LINE));

Add setup wizard to AOSP

https://android.googlesource.com/platform/packages/apps/Provision

基本上就是在這裡面加上 UI 就可以了

重點注意:

  1. 要放到 /system/priv-app or /vendor/priv-app or /product/priv-app
  2. 只能有一個 provision app

基本上只要AndroidManifest.xml 有宣告這些 intent filter 之後就可以達到 setup wizard 的效果了,開機被優先啟動(啟動流程結束之後要把 setup wizard 自己 disabled 掉,避免下次開機又出現 setup wizard )

有多個 privision app 的話會看到這個錯誤訊息,Package Manger Service 會檢查 setup wizard 只能有一個

There should probably be exactly one setup wizard; found 2: matches

Android 10 gestural navigation, edge back gesture

edge back function was enabled in this file frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java

https://cs.android.com/android/platform/superproject/+/master:frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/EdgeBackGestureHandler.java;l=253;bpv=0;bpt=1

suppress Android ANR, crash dialog

    <!-- List of comma separated package names for which we the system will not show crash, ANR,
         etc. dialogs. -->
    <string translatable="false" name="config_appsNotReportingCrashes"></string>