简体   繁体   English

在 Cordova config.xml 中更改 Android 活动名称

[英]Changing Android activity name in Cordova config.xml

I have an old Android Cordova app whose manifest has <activity android:name=".MyActivity" .我有一个旧的 Android Cordova 应用程序,其清单具有<activity android:name=".MyActivity" This has already been published to the Play store, so I can no longer change the name .这已经发布到 Play 商店,所以我不能再更改名称

I'm trying to migrate to the Cordova CLI workflow, but the Android app it generates has android:name="MainActivity" .我正在尝试迁移到 Cordova CLI 工作流,但它生成的 Android 应用程序具有android:name="MainActivity" How can I customize this in config.xml, so it generates the right name?如何在 config.xml 中自定义它,以便生成正确的名称?

I found an undocumented preference "android-activityName".我发现了一个未记录的偏好“android-activityName”。 I tried setting this to ".MyActivity", and removing/adding the android platform.我尝试将其设置为“.MyActivity”,并删除/添加 android 平台。 But with this setting, it no longer created the activity class at all, and I got an error "Error: No Java files found which extend CordovaActivity".但是使用此设置,它根本不再创建活动类,并且出现错误“错误:未找到扩展 CordovaActivity 的 Java 文件”。

Recently I also faced a similar situation.What I did was I changed the id of the widget tag inside config.xml to reflect the package name where I put my MyActivity class which extends CordovaActivity .Make sure you have deleted the unwanted MainActivity class that cordova generates(if you are putting your MyActivity class in the same package as MainActivity class).最近我也遇到了类似的情况。我所做的是更改了 config.xml中小部件标签的 id 以反映我放置扩展CordovaActivity 的MyActivity类的包名称。确保您已删除了不需要的MainActivity类,cordova生成(如果您将 MyActivity 类与 MainActivity 类放在同一个包中)。 And also your MyActivity class should be extending CordovaActivity (important) .Then open config.xml and change the widget tag as given below.而且您的 MyActivity 类应该扩展 CordovaActivity(重要) 。然后打开 config.xml 并更改小部件标记,如下所示。

//Change the id value to the package name where you put your MyActivity class
<widget id="com.yourCompanyName.yourPackageName" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">

After doing this clean the project using cordova clean command and then rebuild the project.Additionally you can use android-packageName property to widget tag to give android specific package name and ios-CFBundleIdentifier for ios specific name.执行此操作后,使用cordova clean命令清理项目,然后重建项目。此外,您可以使用android-packageName属性来为小部件标记提供android 特定的包名称和ios-CFBundleIdentifier为ios 特定名称。

I had a similar problem and this was my solution in bash.我有一个类似的问题,这是我在 bash 中的解决方案。 I had the wrong Activity name and the wrong java file name, so I needed to change both.我有错误的 Activity 名称和错误的 java 文件名,所以我需要更改两者。

I created the following bash function: First I replace all MainActivity references with NewActivityName .我创建了以下bash函数:首先,我将所有MainActivity引用替换为NewActivityName Then I look for the java file and rename it.然后我查找java文件并重命名它。

function rename_the_activity {
    platformDir="/path/to/platformdir"
    grep -rl 'MainActivity' $platformDir | sort | uniq | xargs perl -e "s/MainActivity/NewActivityName/" -pi
    activity=$(find $platformDir -name 'MainActivity.java')
    newactivity="${activity/MainActivity/NewActivityName}"
    mv $activity $newactivity
    print_blue "Renamed activity class MainActivity to NewActivityName"
}

我已经使用cordova-custom-config插件处理过这个问题,将它添加到我的config.xml中:

<custom-preference name="android-manifest/application/activity/@android:name" value=".MyActivity" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM