You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何限制CN1安卓构建应用在Android Studio及Genymotion模拟器运行

限制Codename One安卓应用在模拟器中运行的方法

要阻止CN1构建的安卓应用在Android Studio、Genymotion等模拟器中运行,核心思路是通过原生安卓代码检测模拟器特征,并在应用启动时触发限制逻辑,具体步骤如下:

1. 定义CN1原生接口

创建一个Java接口,用于在CN1代码中调用安卓原生的检测逻辑:

public interface EmulatorDetector extends NativeInterface {
    boolean isRunningOnEmulator();
}

2. 实现安卓端检测逻辑

在CN1项目的android目录下创建原生实现类,通过识别模拟器的系统特征来判断环境:

import com.codename1.system.NativeLookup;
import android.os.Build;
import android.provider.Settings;

public class EmulatorDetectorImpl implements EmulatorDetector {
    @Override
    public boolean isRunningOnEmulator() {
        boolean isEmulator = false;
        
        // 检测Build属性中的模拟器标识
        if (Build.BRAND.contains("generic") 
            || Build.DEVICE.contains("generic") 
            || Build.MODEL.contains("Emulator") 
            || Build.MODEL.contains("Android SDK built for x86") 
            || Build.MANUFACTURER.contains("Genymotion") 
            || "google_sdk".equals(Build.PRODUCT)) {
            isEmulator = true;
        }
        
        // 检测系统指纹特征
        String fingerprint = Build.FINGERPRINT;
        if (fingerprint != null && (fingerprint.contains("generic") || fingerprint.contains("emulator"))) {
            isEmulator = true;
        }
        
        // 检测模拟器常见的固定Android ID
        String androidId = Settings.Secure.getString(
            android.content.ContextWrapper.getBaseContext().getContentResolver(), 
            Settings.Secure.ANDROID_ID
        );
        if ("9774d56d682e549c".equals(androidId)) {
            isEmulator = true;
        }
        
        return isEmulator;
    }

    @Override
    public boolean isSupported() {
        return true;
    }
}

3. 在CN1主逻辑中触发检测

在应用启动的入口代码中调用检测接口,若判定为模拟器则退出应用:

EmulatorDetector detector = NativeLookup.create(EmulatorDetector.class);
if (detector != null && detector.isSupported() && detector.isRunningOnEmulator()) {
    Dialog.show("提示", "本应用不支持在模拟器中运行", "确定", null);
    Display.getInstance().exitApplication();
}

4. 配置原生接口编译

在CN1项目的codenameone_settings.properties文件中添加配置,确保编译时包含原生实现类:

android.nativeInterfaces=com.yourpackage.EmulatorDetectorImpl

(注意替换com.yourpackage为你实际的包名)

补充说明

  • 上述检测逻辑能覆盖绝大多数主流模拟器(Android Studio模拟器、Genymotion等),但无法做到100%拦截所有定制化模拟器,若需要更严格检测,可添加更多特征(如检查虚拟硬件标识、模拟器专属文件等)。
  • 避免过度依赖单一检测条件,建议组合多种特征提升准确率。

内容的提问来源于stack exchange,提问作者Eric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 00:28:19