-2

I have a simple button

<Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:onClick="trap" />

Initial code

package com.example.a21.ii;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void trap(View view){
        //click function
    }
}

After clicking it must change to this code

  package com.example.a21.ii;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        public void trap(View view){
        //click function
        }

        private void additionalFunction(){}

}

so how to change the own code? Any suggestion is appreciate and Please let me know if more details are need. Thanks

5
  • 1
    you can´t manipulate your code inside the apk programmatically. what´s the goal? I guess you misunderstood something.... Commented Jun 3, 2017 at 13:13
  • The goal is I don't have to use database. Commented Jun 3, 2017 at 13:20
  • If-else or switch-case or Delegates are the way to go here. Commented Jun 3, 2017 at 13:23
  • Looks like nobody knows! Commented Jun 3, 2017 at 13:26
  • 1
    "Looks like nobody knows!"...I can ensure you we do. It is definetely not possible to change the code inside your apk at runtime. Commented Jun 3, 2017 at 13:34

1 Answer 1

1

If you want to add an additional function, you cannot do it by changing the existing code.

The ANdroid APK is read-only. You cannot write to it, so you cannot change the .java files in the APK.

However:

You can create a class that loads the new methods (NOTE Database, shared prefs or files is required) and strips the function of them, and takes appropriate action without writing the methods to the APK.

Essentially, you want to execute the method without writing it to the APK, so you need to design a class that can execute these methods.

OR:

YOu can create an external class(File only) in internal or external storage. Then you write any methods to that class. Refer this SO question to see how you can load and execute code in external .java files.

Please note that this is a workaround. You cannot write to the APK, but you can write .java files to the system and load them and execute them at runtime.


Final note

The first option (Create a class to load methods and execute them internally in the app) is extremely hard. It may even be impossible. The second option (where you create a .java file externally and load it in) is probably the easiest option around. You save a file externally and add any methods you want, and create a new instance of the external class in the class you want these methods in. This is the only way (I know of) you can create files and make them editable.

And I cannot say it enough times:

The APK is read only!

You cannot write to the APK, but you can read from it. For an instance oyu can read assets, but you cannot write to the assets.


The only way to "change" the contents of an APK is to decode the old, add whatever changes you need and create a new apk. But the process of doing this requires root, and adding the entire system to an application that does something else will add a lot of bloat to your app. Further, it will then most likely be signed with a different keystore and may make it incompatible with the store version, which may remove the option for updates in the future.


And from your comments, I apparently have to say it one more time:

You cannot change the APK like changing a text file! The APK is like a zipped file. To change it, you have to unpack it and then edit it. Meaning no matter what programming language you are thinking of, you have to unpack the APK, change it and repack it. Which, as I mentioned above, has many side effects.

And as you mentioned in another comment, if you suddenly are wondering about windows and the function there, that is a whole other topic (and a whole different set of developers who have competence on the topic). Ask another question instead of having two very distinct questions in one

Sign up to request clarification or add additional context in comments.

11 Comments

So how can I read Apk? May I show it in here->Log.e("out","Here must be entire code in MainActivity.java")
Read APK as in assets, resources, etc. I don't know if you can print out every line of the classes
Everything is possible.I'll show you how to change APK
You cannot change the APK - it is read only. Using a tool like Lucky Patcher you can create a new APK, but that decodes teh old one, changes the code and wraps it in a new one. Which is an extremely hard way to do it
OK! I am completely surrendered!!! Do you know another programming language which I can be changing the existing code?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.