Launcher icon Xamarin Android not found - android

I'm doing a Hello World in Xamarin Android, but when I try to change the launcher icon, it throws the following error: "No resource found that matches the given name (at 'icon' with value '#drawable/icon')."
Actually I don't have any "icon" in my resources, the icons' names are ic_launcher:
And my manifest looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="HelloWorldXamarinAndroid.HelloWorldXamarinAndroid" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="16" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:label="Hi android" android:icon="#drawable/ic_launcher"></application>
</manifest>
I've already cleaned the project and tried to rebuild it, can someone help me?
If I change all the images' names to "Icon.png" and the manifest with "#drawable/Icon", it works, my question is, why Xamarin doesn't work with other names
I found the problem, in Main Activity is set the icon too, so it's necessary to set the new name there too:
[Activity(Label = "HelloWorldXamarinAndroid", MainLauncher = true, Icon = "#drawable/ic_launcher")]
public class MainActivity : Activity
{
...
}

Right click the Droid project and select Android Manifest from the menu list. Under Application Icon you will see which icon the application is referencing.
Below is a picture of where the icon is being references.

Related

NLog log file on Xamarin - Android issues

I have been trying to use NLog from android, and it works whilst using a console based output. However when i try to target an external storage area/file nothing happens....there is no log file created...
My manifest contains the following lines:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
My Nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<targets>
<target name="logfile" xsi:type="File" fileName="storage\emulated\0\Download\log.txt" />
</targets>
<rules>
<!-- add your logging rules here -->
<logger name="*" minlevel="Error" writeTo="logfile" />
</rules>
</nlog>
My activity MainActivity.cs :
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Logger sm_logger = LogManager.GetCurrentClassLogger();
sm_logger.Debug("test output");
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
}
All of this code runs, no errors seem to be thrown, however no file is created either...using the console logger does infact output to the console however... Any help would be appreciated.
Note:
Im using both the emulator and a device, neither have a log file created.
Im building for android 7.1 level 25 of api.
The NLog.config file is bundled as a androidasset, and seems to load as it can find the targets etc
It is not enough to have
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
you also need to request the persmission with something like
ActivityCompat.RequestPermissions(Adapter.Activity, new String[] { Manifest.Permission.WriteExternalStorage }, 1234);
(which is incomplete, you also need to handle the permission result, but there are good resources out there on how to do that).
This needs to happen before the NLog FileTarget is created.

parsing an android manifest file with JDOM2

hope you're fine.
I have to parse an android manifest file to extract data like 'the minSdkVersion' used, after several seraches I found a code using JDOM.
Display data related to "uses-sdk" was expected but When running I got a null object.
Need Help
Thanks
Manifest Example
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.rssfeed"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
The code
import java.io.File;
import java.util.Iterator;
import java.util.List;
import org.jdom2.*;
import org.jdom2.input.SAXBuilder;
public class ManifestP2 {
static Document document;
static Element root;
static NeededTag tags;//Data receiver
public static void main(String[] args){
SAXBuilder builder = new SAXBuilder();
try{
document = builder.build(new File("AndroidManifest.xml"));
}catch(Exception e){
System.out.println(e.getMessage());
}
root = document.getRootElement();
tags = new NeededTag();
findTag();
System.out.println(tags.usesSdk.toString());
}
static void findTag(){
//get values for "uses-sdk" tag
Element sdk = root.getChild("uses-sdk");
tags.usesSdk.minSdk = sdk.getAttributeValue("android:minSdkVersion");
tags.usesSdk.targetSdk = sdk.getAttributeValue("android:targetSdkVersion");
}
}
The attributes in your document are in the android namespace. Namespaces in XML add a level of complexity, that's true, but they also ensure that you can have special types of documents, like your manifest, where you can store "meta" types of information inside the same document as regular data, while still guaranteeing that there won't be any name collisions.
That's a complicated way of saying that where you see android in your XML document, it refers to a namespace, not the attribute name.
So, for example, where you have:
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
that really means you have your own element uses-sdk and that element has 2 attributes, one called minSdkVersion, and the other called targetSdkVersion. Those attributes are in the namespace with the URI: http://schemas.android.com/apk/res/android.
To get those attributes off the element in JDOM, you have to ask for them by name, and in the right namespace.
To do that, you have the code:
Namespace ans = Namespace.getNamespace("android", "http://schemas.android.com/apk/res/android");
String min = sdk.getAttributeValue("minSdkVersion", ans);
String target = sdk.getAttributeValue("targetSdkVersion", ans);
You declare the namespace to search for (only the URI is important when searching, the android namespace prefix is there for convenience only)

Change ID App in Qt

Well.. I have a big problem my app id is "org.qtproject.example.test". I want to change to "com.mycompany.myapp". I was looking at the whole project and there is no way to change it. How do you do that?
change the package name in the Android manifest file.
manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.mycompany.myapp" android:versionCode="1" android:versionName="1.0.0"
Open the AndroidManifest.xml file of your project in Creator and it's the first setting in the manifest editor form.
Also, from QGuiApplication see the setOrganizationName(), setOrganizationDomain() and setApplicationName() functions.

When to use which constructor for ComponentName in Android?

I am a little bit confused about the ComponentName class in Android.
There are different ways to get to a component name object, but I don't know when to use which... and why!
Example:
Application package is de.zordid.sampleapp
but widget provider class is de.zordid.sampleapp.widget.WidgetProvider
Using
ComponentName cn = new ComponentName("de.zordid.sampleapp.widget",
"WidgetProvider");
I got this component info: ComponentInfo{de.zordid.sampleapp.widget/WidgetProvider}, but I could not use this - the component is unknown!
But the JavaDoc says I should give the package and the class within that package - and that is what I did, didn't I??
Using
ComponentName cn = new ComponentName(context, WidgetProvider.class);
yields ComponentInfo{de.zordid.sampleapp/de.zordid.sampleapp.widget.WidgetProvider} - and that works fine!!
There is even another way to get a ComponentName - by context and a string.
Which one should be used where and when??
Thanks!
The ComponentName constructor taking two Strings can be used to refer to a component in another application. But, the first argument is not the package name of the class; it is the package name of the application---the package attribute of the manifest element in that application's AndroidManifest.xml. So your first example should be
ComponentName cn = new ComponentName("de.zordid.sampleapp",
"de.zordid.sampleapp.widget.WidgetProvider");
That constructor could certainly be used to refer to components in your own application, but since you already have hold of a Context from your own application you might as well use it and use one of the other constructors. In my opinion, the one taking a Class should be preferred whenever usable. You could use the one taking a String if you only know the class dynamically for some reason; in that case, it should take the fully-qualified class name as above.
Robert Tupelo-Schneck's answer is right about preferring objects against Strings. Here's how I see it with details on how all the different prefixes work.
To refer to your own components, use:
new ComponentName(getApplicationContext(), WidgetProvider.class);
To refer to some dynamically referenced component in your own app, use:
// values/strings.xml: <string name="provider">de.zordid.sampleapp.widget.WidgetProvider</string>
String fqcn = getResources().getString(R.string.provider);
new ComponentName(getApplicationContext(), fqcn);
This is useful when you want to use Android's resource qualifiers to decide which component to use, you can override the default string in values-*/strings.xml.
To refer to another application's component, use:
int componentFlags = GET_ACTIVITIES | GET_PROVIDERS | GET_RECEIVERS | GET_SERVICES;
PackageInfo otherApp = context.getPackageManager().getPackageInfo("com.other.app", componentFlags);
ComponentInfo info = otherApp.activities[i]; // or providers/receivers/...
new ComponentName(info.packageName, info.name);
#About .Names and <manifest package="
There may be some confusion here because I think historically Robert's statement was true:
it is the package name of the application---the package attribute of the manifest element in that application's AndroidManifest.xml
but not any more. Since the new Gradle build system was introduced there has been some changes around here, and then they changed it again in AGP 7.3, and made it mandatory in AGP 8.0.
If you have an android.defaultConfig.applicationId specified in your build.gradle that'll be the app package name, and then package attribute in manifest (or later namespace in build.gradle) is a separate thing when building your app. The first argument of ComponentName now refers to applicationId + applicationIdSuffix. The tricky thing is that after the final manifest merge and packaging the APK will have <manifest package=applicationId + applicationIdSuffix and all the .Names will be expanded to FQCNs.
Example app for learning name resolution
Here's an example structure based on the structure of one of my apps. Consider the following classes in a hypothetical app called "app":
net.twisterrob.app.android.App
net.twisterrob.app.android.GlideSetup
net.twisterrob.app.android.subpackage.SearchResultsActivity
net.twisterrob.app.android.subpackage.Activity
net.twisterrob.app.android.content.AppProvider
on the server side backend of the app and/or some shared model classes:
net.twisterrob.app.data.*
net.twisterrob.app.backend.*
net.twisterrob.app.web.*
in my Android helper library:
net.twisterrob.android.activity.AboutActivity
other libraries:
android.support.v4.content.FileProvider
This way everything is namespaced in net.twisterrob.app. The android app being just a single part of the whole inside it's own subpackage.
AndroidManifest.xml (irrelevant parts omitted)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.twisterrob.app.android">
<!--
`package` above defines the base package for .Names
to simplify reading/writing the manifest.
Notice that it's different than the `applicationId` in build.gradle
and can be independently changed in case you want to refactor your packages.
This way you can still publish the same app with the same name.
-->
<!-- Will be expanded to net.twisterrob.app.android.App in the manifest merging phase. -->
<application android:name=".App">
<!-- meta-data needs FQCNs because the merger can't know if you want to expand them or not.
Also notice that name and value both can contain class names, depending on what you use. -->
<meta-data android:name="net.twisterrob.app.android.GlideSetup" android:value="GlideModule" />
<meta-data android:name="android.app.default_searchable" android:value="net.twisterrob.app.android.subpackage.SearchResultsActivity" />
<!-- Will be expanded to net.twisterrob.app.android.subpackage.Activity in the manifest merging phase. -->
<activity android:name=".subpackage.Activity" />
<!-- Needs full qualification because it's not under the package defined on manifest element. -->
<activity android:name="net.twisterrob.android.activity.AboutActivity" />
<!-- Will be expanded to net.twisterrob.app.android.content.AppProvider in the manifest merging phase. -->
<provider android:name=".content.AppProvider" android:authorities="${applicationId}" />
<!-- Needs full qualification because it's not under the package defined on manifest element. -->
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.share" />
</application>
<!-- ${applicationId} will be replaced with what's defined in `build.gradle` -->
</manifest>
build.gradle
android {
defaultConfig {
// this is what will be used when you upload it to the Play Store
applicationId 'net.twisterrob.app'
// in later AGP versions, move manifest's package here:
// namespace 'net.twisterrob.app.android'
}
buildTypes {
debug {
// The neatest trick ever!
// Released application: net.twisterrob.app
// IDE built debug application: net.twisterrob.app.debug
// This will allow you to have your installed released version
// and sideloaded debug application at the same time working independently.
// All the ContentProvider authorities within a system must have a unique name
// so using ${applicationId} as authority will result in having two different content providers.
applicationIdSuffix '.debug'
}
}
}
To check out what your final manifest will look like after all the merging open build\intermediates\manifests\full\debug\AndroidManifest.xml.
Or you can use like this inside BroadcastReceiver :
ComponentName smsReceiver = new ComponentName(this, SMSReceiver.class);

Unable to resolve activity for: Intent

I am having a problem in running Android unit test. I got this error when I tried to run a simple test.
Here's the log:
Blockquote
java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.wsandroid.Activities/.SplashActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:371)
at android.test.InstrumentationTestCase.launchActivityWithIntent(InstrumentationTestCase.java:120)
at android.test.InstrumentationTestCase.launchActivity(InstrumentationTestCase.java:98)
at android.test.ActivityInstrumentationTestCase2.getActivity(ActivityInstrumentationTestCase2.java:87)
at com.wsandroid.test.activity.TestEULA.setUp(TestEULA.java:15)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
This error occurs for Android less than 2.2. It works fine for Android 2.2 emulator. Yet Android 2.2 emulator has a bug of sending a key twice even though we only press it one. Application to be tested runs on Android 2.2 platform.
Appreciate if anyone of you can help me.
Dzung.
This can also be cause by a missing
Make sure you have a corresponding entry in your manifest.
<activity android:name=".SplashActivity" ...
I had a similar problem with a simple test project for an app that was just a splash screen. I found that I had implemented the constructor wrong. My initial implementation of the constructor was this...
public SplashScreenTest(){
super("com.mycomp.myapp.SplashScreen", SplashScreen.class);
}
After some beating my head against the wall, I somehow decided to remove the SplashScreen from the pkg argument of super(). My successful implementation is now like this...
public SplashScreenTest() {
super("com.mycomp.myapp", SplashScreen.class);
}
I hope that this helps you or others solve the problem.
Try to check your Manifest.xml file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tablet.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="android.test.runner" />
</application>
<uses-sdk android:minSdkVersion="8" />
<!-- This line below! -->
<instrumentation android:targetPackage="com.tablet.tablet"
android:name="android.test.InstrumentationTestRunner" />
</manifest>
You need to check the following line:
<instrumentation android:targetPackage="com.tablet.tablet"
android:name="android.test.InstrumentationTestRunner" />
So the targetPackage must be the same as in your code.
I had specific similar problem while using the AndroidAnnotations lib.
Later, I found out it was due to forgetting to use the generated class (MyActivity_ instead of MyActivity).
In my case the problem was that TestFragmentActivity, meaning the Activity used in our test
extends ActivityInstrumentationTestCase2<TestFragmentActivity>
must be available in the package defined in Manifest.xml as targetPackage:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="de.my.androidhd" />
My solution was to move TestFragmentActivity into tested application package.
For the keys being sent twice issue, are you sure you're not now getting both the Down and Up actions? I had this issue when using Robotium, and generated this to make things easier:
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.widget.EditText;
import com.jayway.android.robotium.solo.Solo;
public static void type(Solo robot, EditText edit, String text) {
int index = 0;
//Find the index of this control, as Robotium doesn't seem to like R.id
for (int i = 0; i < robot.getCurrentEditTexts().size(); i++) {
if (robot.getCurrentEditTexts().get(i).getId() == edit.getId()) {
index = i;
}
}
robot.clickOnEditText(index);
KeyCharacterMap map = KeyCharacterMap.load(KeyCharacterMap.BUILT_IN_KEYBOARD);
KeyEvent[] events = map.getEvents(text.toCharArray());
for (int event = 0; event < events.length; event++) {
if (events[event].getAction() == KeyEvent.ACTION_DOWN) {
robot.sendKey(events[event].getKeyCode());
}
}
}
I've had two activities with same name in different packages. Issue was about importing from the wrong package. I spend much time on it maybe it will save someone some time.

Categories

Resources