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

最近在写安卓系统的JavaDoc,给PowerManager.java写add example
很久没用过Android Studio,连jar包如何引用都忘记了,记录一笔。
首先将jar包放在如图所示的路径下:
在这里插入图片描述
然后对着jar包右键,Add As Library,导入成功。
在这里插入图片描述
需要测试的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
 * <p>
 * Runtime environment: System signature and minSdkVersion 23 <br/>
 * Permission: android:sharedUserId="android.uid.system"
 * and android.permission.DEVICE_POWER to AndroidManifest.xml.
 * <p>
 * <div class="jd-tagdata">
 * <h5 class="jd-tagtitle">Example</h5>
 * </div>
 * <pre>
 * OurPowerManager sPowerManager = OurPowerManager.getInstance(context);
 *
 * sPowerManager.goToSleep(time, reason, flags);
 * sPowerManager.wakeUp(time);
 * sPowerManager.resetBatteryAging();
 * sPowerManager.setBatteryLevelLow(level);
 * sPowerManager.rebootChargeMode();
 * </pre>
 */
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"/>

在这里插入图片描述
提示如果不是系统app就不能使用这个权限,继续修改AndroidManifest.xml,添加:

 android:sharedUserId="android.uid.system"

在这里插入图片描述
使用AS调试,点击app按钮,依然异常退出。查询得知系统app需要系统签名,我们公司有自己的系统签名,初学者可自己在网上找,这里给一个参考链接:https://blog.csdn.net/lijunweiqiang/article/details/81565501
这里我就不通过Android Studio来签名调试了,通过adb来。首先build apk,
在这里插入图片描述
生成的文件路径如下:
在这里插入图片描述
拷贝出来,通过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路径。
在这里插入图片描述
如果出现如图错误,需要卸载之前的安装包:adb uninstall com.example.testworkspaces,需要写完整的包名。
然后启动app。
命令如下:adb shell am start -n com.example.testworkspaces/.MainActivity
/前面是包名,后面是主活动类,如果怕写错可以去AndroidManifest.xml复制。
注意2处MainActivity前有个“.”,那你一定也要写,不然会报错,没有此活动。
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值