N
N
Nordman992021-09-24 11:52:27
Java
Nordman99, 2021-09-24 11:52:27

Why are there errors in Java code?

I study Android studio according to manuals from the Internet, in one of the articles, by the way, quite fresh, Android and 10 and 11 are considered there.
An example of writing your own Java code is considered, to add it to any existing APK, parsed using apktools, and then compiling it using the native Javac compiler javac.

Here is a piece of code:

package com.example.test;

import android.content.Context;
import android.widget.Toast;
public class Payload {
    public static void run(Activity activity) {
        Toast.makeText(context, "Hello world!", Toast.LENGTH_LONG).show();
    }
}

I compile with the command:
javac -classpath "c:\Program Files\Android\Android Studio\plugins\android
\lib\android.jar" c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example\test\test.java

Well, nothing is compiled right away, the output from executing the command:
c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example
\test\payload.java:4: error: package android.widget does not exist
import android.widget.Toast;
                     ^
c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example
\test\test.java:6: error: cannot find symbol
    public static void run(Activity activity) {
                           ^
  symbol:   class Activity
  location: class Payload
c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example
\test\payload.java:7: error: cannot find symbol
        Toast.makeText(Context, "Hello World!", Toast.LENGTH_LONG).show();
                       ^
  symbol:   variable Context
  location: class Payload
c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example
\test\payload.java:7: error: cannot find symbol
        Toast.makeText(Context, "Hello World!", Toast.LENGTH_LONG).show();
                                                    ^
  symbol:   variable Toast
  location: class Payload
c:\Users\User\AndroidStudioProjects\Test\app\src\main\java\com\example
\test\test.java:7: error: cannot find symbol
        Toast.makeText(Context, "Hello World!", Toast.LENGTH_LONG).show();
        ^
  symbol:   variable Toast
  location: class Test
6 errors

those. this code, according to the author, is independent and independent of the project of the APK where it is embedded, but something does not work.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-09-24
@GavriKos

If you have just started, then forget about this perversion:

to add it to any already existing APK parsed with apktools

Do it completely yourself.

G
gazievDima, 2021-09-26
@gazievDima

You have just started training and are already trying to do something that many have not done in a long career, you better start with the basics.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question