Android stdio进行系统app签名所遇到的问题

最近在写安卓系统的JavaDoc,给PowerManager.java写add example
很久没用过Android Studio,连jar包如何引用都忘记了,记录一笔。
首先将jar包放在如图所示的路径下:
Android stdio进行系统app签名所遇到的问题_第1张图片
然后对着jar包右键,Add As Library,导入成功。
Android stdio进行系统app签名所遇到的问题_第2张图片
需要测试的java文件如下:

package com.our.sdk.os;

import android.annotation.NonNull;
import android.content.Context;
import android.os.PowerManager;

/**
 * This class for controlling device screen switches
 * 

* Runtime environment: System signature and minSdkVersion 23
* Permission: android:sharedUserId="android.uid.system" * and android.permission.DEVICE_POWER to AndroidManifest.xml. *

*

*
Example
*
*
 * OurPowerManager sPowerManager = OurPowerManager.getInstance(context);
 *
 * sPowerManager.goToSleep(time, reason, flags);
 * sPowerManager.wakeUp(time);
 * sPowerManager.resetBatteryAging();
 * sPowerManager.setBatteryLevelLow(level);
 * sPowerManager.rebootChargeMode();
 * 
*/
public class OurPowerManager { private final static String TAG = OurPowerManager.class.getSimpleName(); private static OurPowerManager sInstance; private PowerManager mPowerManager; private OurPowerManager(@NonNull PowerManager powerManager) { mPowerManager = powerManager; } private OurPowerManager(@NonNull Context context) { mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); } /** * Gets an instance of OurPowerManager after creating one if needed * * @param powerManager the PowerManager object, power manager service * @return the OurPowerManager instance */ public static OurPowerManager getInstance(@NonNull PowerManager powerManager) { if (sInstance == null) { synchronized (OurPowerManager.class) { if (sInstance == null) { sInstance = new OurPowerManager(powerManager); } } } return sInstance; } /** * Gets an instance of OurPowerManager after creating one if needed * * @param context the Context object used to access application assets * @return the OurPowerManager instance */ public static OurPowerManager getInstance(@NonNull Context context) { if (sInstance == null) { synchronized (OurPowerManager.class) { if (sInstance == null) { sInstance = new OurPowerManager(context); } } } return sInstance; } /** * Sets device to go to sleep * * @param time a long value, the time when the request to go to sleep was * issued, in the {@link SystemClock#uptimeMillis()} time base. * This timestamp is used to correctly order the go to sleep * request with other power management functions.It should be * set to the timestamp of the input event that caused the * request to go to sleep * @param reason a integer value, reason for closing screen function * @param flags a integer value, flag values to modify the release behavior */ public void goToSleep(long time, int reason, int flags) { mPowerManager.goToSleep(time, reason, flags); } /** * Sets device to wake up from sleep * * @param time a long value, the time when the request to wake up was issued, * in the {@link SystemClock#uptimeMillis()} time base.This * timestamp is used to correctlyForces the deviceto go to sleep * order the wake up request with other power management * functions.It should be set to the timestamp of the input * event that caused the request to wake up */ public void wakeUp(long time) { mPowerManager.wakeUp(time); } /** * Reset batetry aging status * */ public void resetBatteryAging() { mPowerManager.resetBattAging(); } /** * Set low battery threshold * * @param level a integer value. When the battery capacity lower than the * level without any external power,{@link #ACTION_BATTERY_LOW} * Intent will be broadcast. * */ public void setBatteryLevelLow(int level) { mPowerManager.setBatteryLevelLow(level); } /** * Device reboot to charging mode * */ public void rebootChargeMode() { mPowerManager.rebootChargeMode(); } }

测试小demo如下(主要测试构造函数和gotosleep()熄屏,wakeup()亮屏),按下button熄屏,两秒后亮屏:

package com.example.testworkspaces;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.our.sdk.os.OurPowerManager;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button)findViewById(R.id.button);
        final OurPowerManager ourPowerManager = OurPowerManager.getInstance(getBaseContext());
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ourPowerManager.goToSleep(2,0,0);
                new Thread(){
                    public void run(){
                        try {
                            Thread.sleep(2000);
                        }catch (Exception e){
                            Log.d("OurManager","gotosleep");
                        }
                        ourPowerManager.wakeUp(2);
                    }
                }.start();
            }
        });
    }
}

直接用AndroidStudio build到设备上会报错:no android.permission.DEVICE_POWER
第一反应是修改AndroidManifest.xml添加权限:

 <uses-permission android:name="android.permission.DEVICE_POWER"/>

Android stdio进行系统app签名所遇到的问题_第3张图片
提示如果不是系统app就不能使用这个权限,继续修改AndroidManifest.xml,添加:

 android:sharedUserId="android.uid.system"

Android stdio进行系统app签名所遇到的问题_第4张图片
使用AS调试,点击app按钮,依然异常退出。查询得知系统app需要系统签名,我们公司有自己的系统签名,初学者可自己在网上找,这里给一个参考链接:https://blog.csdn.net/lijunweiqiang/article/details/81565501
这里我就不通过Android Studio来签名调试了,通过adb来。首先build apk,
Android stdio进行系统app签名所遇到的问题_第5张图片
生成的文件路径如下:
Android stdio进行系统app签名所遇到的问题_第6张图片
拷贝出来,通过shell命令进行系统签名,这里我执行的shell脚本命令如下:

java -jar signapk.jar platform.x509.pem platform.pk8 *.apk e_signed.apk

通过adb将签名后的apk安装到设备上。
安装命令:adb install F:\Work\singed\e_signed.apk,路径换成你自己的apk路径。
Android stdio进行系统app签名所遇到的问题_第7张图片
如果出现如图错误,需要卸载之前的安装包:adb uninstall com.example.testworkspaces,需要写完整的包名。
然后启动app。
命令如下:adb shell am start -n com.example.testworkspaces/.MainActivity
/前面是包名,后面是主活动类,如果怕写错可以去AndroidManifest.xml复制。
注意2处MainActivity前有个“.”,那你一定也要写,不然会报错,没有此活动。
Android stdio进行系统app签名所遇到的问题_第8张图片

你可能感兴趣的:(Android,System)